Bug in Channels.newChannel() ?

Alan Bateman Alan.Bateman at oracle.com
Sun Mar 27 06:07:42 PDT 2011


Rémi Forax wrote:
> This snippet prints 2 and 0
>
>     ByteArrayInputStream input = new ByteArrayInputStream(new byte[] 
> {1, 2});
>     ReadableByteChannel channel = Channels.newChannel(input);
>
>     ByteBuffer buffer = ByteBuffer.allocate(2);
>     System.out.println(channel.read(buffer));
>     System.out.println(channel.read(buffer));
>
> but should prints 2 and -1 because we reach the end of the stream.
>
> Rémi
>
The second read is with a buffer that has 0 bytes remaining and so there 
isn't any read from the underlying input stream. It would be the same 
thing as reading from the input stream with a zero length byte array.

-Alan.


More information about the nio-dev mailing list