bug-classpath
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Bug classpath/29526] New: NIO Class gnu.java.nio.ChannelInputStream pro


From: ron at ronsoft dot com
Subject: [Bug classpath/29526] New: NIO Class gnu.java.nio.ChannelInputStream produces spurious EOS indications
Date: 20 Oct 2006 19:37:30 -0000

The gnu.java.nio.ChannelInputStream implementation class, which adapts a
ReadableByteChannel to an InputStream, is broken.  The return value (line 77)
fails to use a 0xFF mask to prevent sign extension of the byte value being
returned.  If binary data is being read and the data stream contains a hex 0xFF
value, then an integer value of -1 is returned (because the byte value is
sign-extended) and the caller sees this as end-of-stream.

In addition, the implementation of this class is very inefficient for
transferring more than a trivial amount of data.

Included below is a more robust and efficient implementation of
ChannelInputStream.  Note that the complementary class ChannelOutputStream
would also benefit from a similar block-transfer implementation.

============

public final class ChannelInputStream extends InputStream
{
        private final ReadableByteChannel channel;

        public ChannelInputStream (ReadableByteChannel channel)
        {
                this.channel = channel;
        }

        public int read (byte b[]) throws IOException
        {
                return read (b, 0, b.length);
        }

        public int read (byte bytes[], int off, int len) throws IOException
        {
                if (len == 0) return 0;

                if ((off < 0) || (off > bytes.length) || (len < 0) ||
                        ((off + len) > bytes.length) || ((off + len) < 0))
                {
                        throw new IndexOutOfBoundsException();
                }

                ByteBuffer buffer = ByteBuffer.wrap (bytes, off, len);

                buffer.position (off);
                buffer.limit (Math.min (off + len, buffer.capacity()));

                return (channel.read (buffer));
        }

        public int read() throws IOException
        {
                byte [] buf = new byte [1];
                int rc = read (buf, 0, 1);

                if (rc == -1) return -1;

                return buf [0] & 255;
        }
}


-- 
           Summary: NIO Class gnu.java.nio.ChannelInputStream produces
                    spurious EOS indications
           Product: classpath
           Version: 0.92
            Status: UNCONFIRMED
          Severity: critical
          Priority: P3
         Component: classpath
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: ron at ronsoft dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=29526





reply via email to

[Prev in Thread] Current Thread [Next in Thread]