Index: src/test/java/common/org/apache/harmony/nio/tests/java/nio/channels/ChannelsTest.java =================================================================== --- src/test/java/common/org/apache/harmony/nio/tests/java/nio/channels/ChannelsTest.java (revision 470208) +++ src/test/java/common/org/apache/harmony/nio/tests/java/nio/channels/ChannelsTest.java (working copy) @@ -24,15 +24,21 @@ import java.io.OutputStream; import java.io.Reader; import java.io.Writer; +import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.channels.Channels; import java.nio.channels.ClosedChannelException; +import java.nio.channels.IllegalBlockingModeException; import java.nio.channels.ReadableByteChannel; +import java.nio.channels.ServerSocketChannel; +import java.nio.channels.SocketChannel; import java.nio.channels.WritableByteChannel; import java.nio.charset.Charset; import java.nio.charset.UnsupportedCharsetException; +import tests.support.Support_PortManager; + import junit.framework.TestCase; /** @@ -640,4 +646,38 @@ testWriter.flush(); this.assertFileSizeSame(tmpFile, this.writebufSize / 2 + 1); } + + /** + * @tests java.nio.channesl.Channels#newReader(ReadableByteChannel channel, + * String charsetName) + */ + public void test_newReader_LReadableByteChannel_LString() + throws IOException { + InetSocketAddress localAddr = new InetSocketAddress("127.0.0.1", + Support_PortManager.getNextPort()); + ServerSocketChannel ssc = ServerSocketChannel.open(); + ssc.socket().bind(localAddr); + + SocketChannel sc = SocketChannel.open(); + sc.connect(localAddr); + sc.configureBlocking(false); + assertFalse(sc.isBlocking()); + + ssc.accept().close(); + ssc.close(); + assertFalse(sc.isBlocking()); + + Reader reader = Channels.newReader(sc, "UTF16"); + int i = reader.read(); + assertEquals(-1, i); + + try { + Channels.newInputStream(sc).read(); + fail("should throw IllegalBlockingModeException"); + } catch (IllegalBlockingModeException e) { + // expected + } + + sc.close(); + } } Index: src/main/java/java/nio/channels/Channels.java =================================================================== --- src/main/java/java/nio/channels/Channels.java (revision 471650) +++ src/main/java/java/nio/channels/Channels.java (working copy) @@ -108,7 +108,7 @@ public static Reader newReader(ReadableByteChannel channel, CharsetDecoder decoder, int minBufferCapacity) { return new ByteChannelReader( - new ReadableByteChannelInputStream(channel), decoder, + new ReaderInputStream(channel), decoder, minBufferCapacity); } @@ -178,74 +178,115 @@ // Wrapper classes // ------------------------------------------------------------------- - /* - * Wrapper class used for newInputStream(ReadableByteChannel channel) - */ - private static class ReadableByteChannelInputStream extends InputStream { + private static class ChannelInputStream extends InputStream { - private ReadableByteChannel channel; + protected ReadableByteChannel channel; - /* - * @param someChannel - */ - public ReadableByteChannelInputStream(ReadableByteChannel aChannel) { - super(); - channel = aChannel; - } + public ChannelInputStream(ReadableByteChannel aChannel) { + super(); + channel = aChannel; + } - /* - * @see java.io.InputStream#read() - */ - public synchronized int read() throws IOException { - byte[] oneByte = new byte[1]; - int n = read(oneByte); - if (n == 1) { - // reads a single byte 0-255 - return oneByte[0] & 0xff; - } - return -1; - } + /* + * @see java.io.InputStream#read() + */ + @Override + public synchronized int read() throws IOException { + byte[] oneByte = new byte[1]; + int n = read(oneByte); + if (n == 1) { + // reads a single byte 0-255 + return oneByte[0] & 0xff; + } + return -1; + } - /* - * @see java.io.InputStream#read(byte[], int, int) - */ - public synchronized int read(byte[] target, int offset, int length) - throws IOException { - // avoid int overflow, check null target - if (length + offset > target.length || length < 0 || offset < 0) { - throw new ArrayIndexOutOfBoundsException(); - } - if (0 == length) { - return 0; - } - if (channel instanceof SelectableChannel) { - if (!((SelectableChannel) channel).isBlocking()) { - throw new IllegalBlockingModeException(); - } - } - ByteBuffer buffer = ByteBuffer.wrap(target, offset, length); - return channel.read(buffer); + /* + * @see java.io.InputStream#close() + */ + @Override + public synchronized void close() throws IOException { + channel.close(); + } + } - } + /* + * Wrapper class used for newInputStream(ReadableByteChannel channel) + */ + private static class ReadableByteChannelInputStream extends + ChannelInputStream { - /* - * @see java.io.InputStream#close() - */ - public synchronized void close() throws IOException { - channel.close(); - } - } + /* + * @param someChannel + */ + public ReadableByteChannelInputStream(ReadableByteChannel aChannel) { + super(aChannel); + } + /* + * @see java.io.InputStream#read(byte[], int, int) + */ + @Override + public synchronized int read(byte[] target, int offset, int length) + throws IOException { + // avoid int overflow, check null target + if (length + offset > target.length || length < 0 || offset < 0) { + throw new ArrayIndexOutOfBoundsException(); + } + if (0 == length) { + return 0; + } + if (channel instanceof SelectableChannel) { + if (!((SelectableChannel) channel).isBlocking()) { + throw new IllegalBlockingModeException(); + } + } + ByteBuffer buffer = ByteBuffer.wrap(target, offset, length); + return channel.read(buffer); + } + } + + /* + * Wrapper class used for newReader(ReadableByteChannel channel, + * CharsetDecoder decoder, int minBufferCapacity) + */ + private static class ReaderInputStream extends ChannelInputStream { + + /* + * @param someChannel + */ + public ReaderInputStream(ReadableByteChannel aChannel) { + super(aChannel); + } + + /* + * @see java.io.InputStream#read(byte[], int, int) + */ + @Override + public synchronized int read(byte[] target, int offset, int length) + throws IOException { + // avoid int overflow, check null target + if (length + offset > target.length || length < 0 || offset < 0) { + throw new ArrayIndexOutOfBoundsException(); + } + if (0 == length) { + return 0; + } + ByteBuffer buffer = ByteBuffer.wrap(target, offset, length); + return channel.read(buffer); + } + } + /* - * Wrapper class used for newOutputStream(WritableByteChannel channel) - */ + * Wrapper class used for newOutputStream(WritableByteChannel channel) + */ private static class WritableByteChannelOutputStream extends OutputStream { private WritableByteChannel channel; /* - * @param someChannel - */ + * @param someChannel + */ public WritableByteChannelOutputStream(WritableByteChannel aChannel) { super(); channel = aChannel;