java - inconsistent BufferedInputStream read(byte[]) behaviour -


My understanding of BufferedInputStream.read (byte []) is that the reading starts with the job status, and reads until the byte array is completed or the end of the stream.

I'm calling the readInt method below on BufferedInputStream.

  public class XDRInputStream {Private InputStream Stream; Private byte [] buffer 4 = new byte [4]; // fixed size buffer private byte [] buffer 8 = new byte [8]; Public XDRInputStream (InputStream Stream) {this.stream = stream; } Public InputStream getInternalStream () {Return stream; } Public int readInt () throws IOException {if (stream.read (buffer4)! = -1) {return ((buffer 4 [0] & lx; & lt; 24) | ((Buffer 4 [1] and 0x FF) <<16) | ((Buffer 4 [2] & amp; 0xFF) & lt; <8) | ((Buffer 4 [3] and 0x FF)); } And (new) EOFException ("end of stream"); }}   

When I track execution in Eclipse debugger, then stream.read (buffer4) calls - regardless of the initial status value - usually The result in the value is that the position is being set to 4, and 4 bytes are four bytes before the input input stream. What is read (Byte []) quietly resets the stream in some circumstances, and if so, when? This behavior seems intent (this is not my code), and when it works in this way, the program works fine.

The problem I am facing is sometimes only on Windows, and only when there is an error message in the input stream (as a result of a fault socket upstream, in this case), this situation This does not seem to be reset, the purpose of which is to read and return the incorrect value in the readings in the method incorrect value

We use the same code on Solaris, and when I step on this platform -Dhru The Bgr did not work properly program under Solaris and bug I try to fix it does not occur. I do not know what is some platform-specific problem with currents?

Thank you.

As a rule of thumb, you are being fully read from the stream in a single shot is. If you see, you will see that the reading (byte []) return is many bytes read, which can be an entire array length or less. Or even 0 if there is no data yet

So you have to make sure that you actually read 4 bytes.

Comments