I am using a BufferedInputStream and reading some data fields from a binary file. I have a header that contains a field I must read to determine the number of bytes I need read for the next message. So the amount of data I need to read is always variable. I tried reading a the entire number of bytes for the message, at one time but this sometimes fails reading the entire size of the message.
So right now I am just looping and reading 1 byte at a time until I have the whole message of data. This seems to work but does seem to be the most efficient way of reading a big file of data(50 meg file).
What is the best way to read variable amount of data from streamed binary file?
Thanks!!
int numRead= 0;
int total =0;
int readSize =56; // actually read from header
while(numRead>=0 && total < readSize){
// reading one byte at a time
//is there any way to read bigger chunks reliably
// since file is large
numRead= this.in.read(inBytes,total ,1);
if(numRead>0){
total += numRead;
}
}