Javadoc for java.io.InputStream.read(byte[] b, int off , int len) says: "Reads up to len bytes of data from the input stream into an array of bytes. An attempt is made to read as many as len bytes, but a smaller number may be read. The number of bytes actually read is returned as an integer."
So maybe the stream can return 0 but there is still data (if not data then -1 is assumed)? If so then: sun.nio.cs.StreamDecoder readBytes() does thow an exception if zero bytes are read instead of looping (https://github.com/openjdk-mirror/jdk7u-jdk/blob/master/src/share/classes/sun/nio/cs/StreamDecoder.java#L287) So it should maybe look more like this: assert rem > 0; int n = 0; do { n = in.read(bb.array(), bb.arrayOffset() + pos, rem); } while (n == 0); if (n < 0) return n; assert (n <= rem) : "n = " + n + ", rem = " + rem; Am i wrong? Thanks Hendrik -- Hendrik Saly (salyh, hendrikdev22) @hendrikdev22 PGP: 0x22D7F6EC