sun.nio.cs.StreamDecoder readBytes() possible issue

Hendrik Dev hendrikdev22 at
Tue Nov 4 20:10:35 UTC 2014

Javadoc for[] 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

So it should maybe look more like this:

assert rem > 0;
int n = 0;
do {
  n =, bb.arrayOffset() + pos, rem);
} while (n == 0);
if (n < 0)
    return n;
assert (n <= rem) : "n = " + n + ", rem = " + rem;

Am i wrong?


Hendrik Saly (salyh, hendrikdev22)
PGP: 0x22D7F6EC

More information about the core-libs-dev mailing list