I’m using the following to read a packet of data from a DataInputStream (wrapped around a socket).
DataInputStream ins = ....;
boolean cleanBreak = true;
try {
synchronized (readLock) {
// read: message length
int ml = ins.readInt();
cleanBreak = false;
// read: message data
byte[] msg = IO.readBytes(ins, ml);
}
} catch (IOException e) {
final boolean eof = e instanceof EOFException && cleanBreak;
...
Using the cleanBreak boolean, I want to determine whether an EOF occurs in the middle of a packet (abruptly) or nicely between two packets. Currently this works when the EOF is in the data part, but not if it’s in the header (the int), e.g. if only 2 more bytes are left when reading the header.
How can I do that?
One way would be to inline readInt:
And adapt it with a special check for the first byte.