help -- extra byte in inputStream
I have a large distributed application which communicates to remote nodes by transmitting byte arrays (serialized objects). I lay out the lengh of each array as a four byte integer -- followed by the byte array itself.
Each writer and reader is run under a separate thread on each side (four threads per socket). All IO is blocking.
My problem is that several times a day random nodes seem to read an extra byte -- which displaces everything and forces me to reset the sockets and recover. The phenomenum is load related, but not dependant. My code is dirt-simple:
public static byte[] readall(InputStream i , int cnt) throws java.io.IOException {
byte[] b = new byte[cnt];
int offset = 0;
int residual = cnt;
int chunk = 0;
while (offset < cnt - 1) {
try {
chunk = i.read(b, offset, residual);
} catch (java.lang.IndexOutOfBoundsException e) {
System.out.println("Out of bounds, chunk = " + chunk + ", offset = " + offset + ", resid = " + residual);
}
if (chunk == residual)
break;// we're done
if (chunk >= 0 ) {
offset += chunk;// move the chains
residual -= chunk; // countdown
} else
throw new java.io.IOException("End of Stream on " + i + " - " + chunk + " returned from read");
}
return b;
}
public static int readInt(InputStream i) throws java.io.IOException {
byte[] b = readall(i,4);
int ij = (int) (((b[0] & 0xff) << 24) | ((b[1] & 0xff) << 16) | ((b[2] & 0xff) << 8) | (b[3] & 0xff));
return ij;
}
public static void writeInt(OutputStream o, int i) throws java.io.IOException {
byte[] b = new byte[4];
b[0] = (byte)(0xff & (i >> 24));
b[1] = (byte)(0xff & (i >> 16));
b[2] = (byte)(0xff & (i >> 8));
b[3] = (byte)(0xff & i);
o.write(b);
}
public static void writeString(OutputStream o, String s) throws java.io.IOException {
byte[] b = s.getBytes();
writeInt(o,b.length);
o.write(b);
}
public static byte[] readArray(InputStream i) throws java.io.IOException {
int length = readInt(i);
if (length > 100000000)
throw new java.io.IOException("Outrageously large Object Size");
byte[] b = readall(i,length);
return b;
}
public static void writeArray(OutputStream o, byte[] b) throws java.io.IOException {
writeInt(o,b.length);
o.write(b,0,b.length);
}
The writer (client side) does this:
tcpServer.writeArray(bout, oshb);
tcpServer.writeArray(bout, osdb);
bout.flush();
and the reader (server side) does this:
byte[] oshb = readArray(bin);
byte[] osdb = readArray(bin);
And, a dozen or so times a day a get an extra byte that throws it out of whack.
I've not enabled Urgent Data on the read side -- I've set no special options.
Any ideas?

