You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by te...@apache.org on 2006/11/07 16:10:30 UTC

svn commit: r472130 - in /incubator/harmony/enhanced/classlib/trunk/modules: luni/src/main/java/java/util/ luni/src/test/java/tests/api/java/util/ nio/src/main/java/java/nio/channels/ nio/src/test/java/common/org/apache/harmony/nio/tests/java/nio/chann...

Author: tellison
Date: Tue Nov  7 07:10:29 2006
New Revision: 472130

URL: http://svn.apache.org/viewvc?view=rev&rev=472130
Log:
Apply patch HARMONY-2076 ([classlib][luni]a bug of j.u.Scanner)

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Scanner.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/ScannerTest.java
    incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/channels/Channels.java
    incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/common/org/apache/harmony/nio/tests/java/nio/channels/ChannelsTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Scanner.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Scanner.java?view=diff&rev=472130&r1=472129&r2=472130
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Scanner.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Scanner.java Tue Nov  7 07:10:29 2006
@@ -292,12 +292,7 @@
             throw new IllegalArgumentException(org.apache.harmony.luni.util.Msg
                     .getString("KA009")); //$NON-NLS-1$
         }
-        try {
-            input = new InputStreamReader(Channels.newInputStream(src),
-                    charsetName);
-        } catch (UnsupportedEncodingException e) {
-            throw new IllegalArgumentException(e.getMessage());
-        }
+        input = Channels.newReader(src, charsetName);
         initialization();
     }
 

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/ScannerTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/ScannerTest.java?view=diff&rev=472130&r1=472129&r2=472130
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/ScannerTest.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/ScannerTest.java Tue Nov  7 07:10:29 2006
@@ -34,6 +34,7 @@
 import java.nio.CharBuffer;
 import java.nio.channels.FileChannel;
 import java.nio.channels.ReadableByteChannel;
+import java.nio.channels.ServerSocketChannel;
 import java.nio.channels.SocketChannel;
 import java.nio.charset.Charset;
 import java.util.Arrays;
@@ -44,6 +45,8 @@
 import java.util.regex.MatchResult;
 import java.util.regex.Pattern;
 
+import tests.support.Support_PortManager;
+
 import junit.framework.TestCase;
 
 public class ScannerTest extends TestCase {
@@ -5673,4 +5676,31 @@
             // do nothing
         }
     }
+    
+    /**
+     * @tests java.util.Scanner#Scanner(ReadableByteChannel)
+     */   
+    public void test_Constructor_LReadableByteChannel()
+			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());
+
+		Scanner s = new Scanner(sc);
+		while (s.hasNextInt()) {
+			s.nextInt();
+		}
+
+		sc.close();
+	}
 }

Modified: incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/channels/Channels.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/channels/Channels.java?view=diff&rev=472130&r1=472129&r2=472130
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/channels/Channels.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/channels/Channels.java Tue Nov  7 07:10:29 2006
@@ -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 ReadableByteChannel channel;
-
-        /*
-         * @param someChannel
-         */
-        public ReadableByteChannelInputStream(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(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);
+    private static class ChannelInputStream extends InputStream {
 
-        }
+		protected ReadableByteChannel channel;
 
-        /*
-         * @see java.io.InputStream#close()
-         */
-        public synchronized void close() throws IOException {
-            channel.close();
-        }
-    }
+		public ChannelInputStream(ReadableByteChannel aChannel) {
+			super();
+			channel = aChannel;
+		}
+
+		/*
+		 * @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#close()
+		 */
+		@Override
+		public synchronized void close() throws IOException {
+			channel.close();
+		}
+	}
+
+	/*
+	 * Wrapper class used for newInputStream(ReadableByteChannel channel)
+	 */
+	private static class ReadableByteChannelInputStream extends
+			ChannelInputStream {
+
+		/*
+		 * @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;

Modified: incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/common/org/apache/harmony/nio/tests/java/nio/channels/ChannelsTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/common/org/apache/harmony/nio/tests/java/nio/channels/ChannelsTest.java?view=diff&rev=472130&r1=472129&r2=472130
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/common/org/apache/harmony/nio/tests/java/nio/channels/ChannelsTest.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/common/org/apache/harmony/nio/tests/java/nio/channels/ChannelsTest.java Tue Nov  7 07:10:29 2006
@@ -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();
+	}
 }