My understanding of BufferedInputStream.read(byte[]) is that the read operation starts from pos, and reads until either the byte array is full or end of stream occurs.
I am calling the readInt method below on a BufferedInputStream.
public class XDRInputStream {
private InputStream stream;
private byte[] buffer4 = new byte[4]; // fixed size buffers
private byte[] buffer8 = 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 ((buffer4[0] & 0xFF) << 24)
| ((buffer4[1] & 0xFF) << 16)
| ((buffer4[2] & 0xFF) << 8)
| ((buffer4[3] & 0xFF));
} else {
throw new EOFException("End of stream");
}
}
When I track execution in the Eclipse debugger, the stream.read(buffer4)
call - regardless of the starting pos value - usually results in the value of pos being set to 4, and the 4 bytes read are the first four bytes from the input stream. Does the read(byte[]) call quietly reset the stream in certain circumstances, and if so, when? This seems to be intended behaviour (this is not my code), and when it operates this way, the program functions fine.
The problem I am experiencing is that sometimes, only on Windows and only when the input stream contains specific contents (an error message resulting from a dropped socket upstream, in this case), this resetting of pos does NOT occur when it seems to be intended to, which results in the method reading from an incorrect position in the stream and returning an incorrect value.
We use the same code on Solaris, and while I have not done a debugger step-through on this platform, under Solaris the program works fine and the bug I'm attempting to fix does not occur. Could there be some platform-specific issue with streams I'm not aware of?
Thanks.