Both the RandomAccessFile and InputStream objects have the ReadBytes method. If I want to read a binary file stored in my device into an array of bytes, which object is better?
I am also confused by the fact that ReadBytes doesn't guarantee to always return the number of bytes requested. Where does this randomness come from? Are both RandomAccessFile and InputStream vulnerable to this problem when reading a local binary file?
If you want to read a complete file into an array then you should use Bit.InputStreamToBytes:
B4X:
Dim b() As Byte = Bits.InputStreamToBytes(File.OpenInput(...))
It reads all the data sequentially and returns an array with the data.
am also confused by the fact that ReadBytes doesn't guarantee to always return the number of bytes requested. Where does this randomness come from? Are both RandomAccessFile and InputStream vulnerable to this problem when reading a local binary file?
1. This can be considered a low level optimization of the native API. The system can return less bytes and thus avoid loading more data to the internal buffers. This is more critical in network channels.
2. Yes.