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

Vitaly Davidovich vitalyd at gmail.com
Tue Nov 4 20:17:28 UTC 2014


There's more detail in the InputStream.read javadoc, specifically (emphasis
mine):

This method blocks until input data is available, end of file is detected,
> or an exception is thrown.
>


> If len is zero, then no bytes are read and 0 is returned; *otherwise,
> there is an attempt to read at least one byte. If no byte is available
> because the stream is at end of file, the value -1 is returned; otherwise,
> at least one byte is read and stored into b. *


On Tue, Nov 4, 2014 at 3:10 PM, Hendrik Dev <hendrikdev22 at gmail.com> wrote:

> 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
>



More information about the core-libs-dev mailing list