harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mloe...@apache.org
Subject svn commit: r398803 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/io/InputStreamReader.java test/java/org/apache/harmony/tests/java/io/InputStreamReaderTest.java
Date Tue, 02 May 2006 04:40:56 GMT
Author: mloenko
Date: Mon May  1 21:40:51 2006
New Revision: 398803

URL: http://svn.apache.org/viewcvs?rev=398803&view=rev
Log:
fix and regression test for HARMONY-166
method read() in InputStreamReader failed to read one character

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/InputStreamReader.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/io/InputStreamReaderTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/InputStreamReader.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/InputStreamReader.java?rev=398803&r1=398802&r2=398803&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/InputStreamReader.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/InputStreamReader.java
Mon May  1 21:40:51 2006
@@ -422,23 +422,23 @@
 	private void fillBuf() throws IOException {
 		chars.clear();
 		int read = 0;
-		try {
-			read = in.read(bytes.array());
-		} catch (IOException e) {
-			chars.limit(0);
-			throw e;
-		}
-		if (read == -1) {
-			chars.limit(0);
-			return;
-		}
-		bytes.limit(read);
-		boolean endOfInput = read < BUFFER_SIZE;
-		CoderResult result = decoder.decode(bytes, chars, endOfInput);
-		if (result.isError()) {
-			throw new IOException(result.toString());
-		}
-		bytes.clear();
+		do {
+			try {
+				read = in.read(bytes.array());
+			} catch (IOException e) {
+				chars.limit(0);
+				throw e;
+			}
+			boolean endOfInput = false;
+			if (read == -1) {
+				bytes.limit(0);
+				endOfInput = true;
+			} else {
+				bytes.limit(read);
+			}
+			decoder.decode(bytes, chars, endOfInput);
+			bytes.clear();
+		} while (read > 0 && chars.position() == 0);
 		chars.flip();
 	}
 

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/io/InputStreamReaderTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/io/InputStreamReaderTest.java?rev=398803&r1=398802&r2=398803&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/io/InputStreamReaderTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/io/InputStreamReaderTest.java
Mon May  1 21:40:51 2006
@@ -17,29 +17,48 @@
 
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.UnsupportedEncodingException;
 
 import junit.framework.TestCase;
 
 public class InputStreamReaderTest extends TestCase {
-    public void testGetEncoding_StreamClosed() {
+    public void testGetEncoding_StreamClosed() throws IOException {
         InputStreamReader in = null;
         byte b[] = new byte[5];
-        try {
-            in = new InputStreamReader(new ByteArrayInputStream(b), "UTF-16BE");
-        } catch (UnsupportedEncodingException e) {
-            fail("Should not throw UnsupportedEncodingException");
-        }
-        try {
-            in.close();
-        } catch (IOException e) {
-            fail("Should not throw IOException");
-        }
+        in = new InputStreamReader(new ByteArrayInputStream(b), "UTF-16BE");
+        in.close();
         String result = in.getEncoding();
         assertEquals(null, result);
     }
 
+    /**
+     * @tests java.io.InputStreamReader#read()
+     */
+    public void testRead() throws IOException {
+        // Regression for HARMONY-166
+        InputStream in;
+        InputStreamReader reader;
+
+        in = new LimitedByteArrayInputStream(0);
+        reader = new InputStreamReader(in, "UTF-16BE");
+        assertEquals("Incorrect byte UTF-16BE", '\u6172', reader.read());
+
+        in = new LimitedByteArrayInputStream(0);
+        reader = new InputStreamReader(in, "UTF-16LE");
+        assertEquals("Incorrect byte UTF-16BE", '\u7261', reader.read());
+
+        in = new LimitedByteArrayInputStream(1);
+        reader = new InputStreamReader(in, "UTF-16");
+        assertEquals("Incorrect byte UTF-16BE", '\u7261', reader.read());
+
+        in = new LimitedByteArrayInputStream(2);
+        reader = new InputStreamReader(in, "ISO2022JP");
+        assertEquals("Incorrect byte ISO2022JP 1", '\u4e5d', reader.read());
+        assertEquals("Incorrect byte ISO2022JP 2", '\u7b2c', reader.read());
+    }
+
     public void testGetEncoding_NotHistorical() {
         InputStreamReader in = null;
         try {
@@ -51,4 +70,53 @@
         assertEquals(result, "UnicodeBigUnmarked");
 
     }
-}
\ No newline at end of file
+
+    static class LimitedByteArrayInputStream extends ByteArrayInputStream {
+
+        // A ByteArrayInputStream that only returns a single byte per read
+        byte[] bytes;
+
+        int count;
+
+        public LimitedByteArrayInputStream(int type) {
+            super(new byte[0]);
+            switch (type) {
+                case 0:
+                    bytes = new byte[] { 0x61, 0x72 };
+                    break;
+                case 1:
+                    bytes = new byte[] { (byte) 0xff, (byte) 0xfe, 0x61, 0x72 };
+                    break;
+                case 2:
+                    bytes = new byte[] { '\u001b', '$', 'B', '6', 'e', 'B',
+                            'h', '\u001b', '(', 'B' };
+                    break;
+            }
+            count = bytes.length;
+        }
+
+        public int read() {
+            if (count == 0) {
+                return -1;
+            }
+            count--;
+            return bytes[bytes.length - count];
+        }
+
+        public int read(byte[] buffer, int offset, int length) {
+            if (count == 0) {
+                return -1;
+            }
+            if (length == 0) {
+                return 0;
+            }
+            buffer[offset] = bytes[bytes.length - count];
+            count--;
+            return 1;
+        }
+
+        public int available() {
+            return count;
+        }
+    }
+}



Mime
View raw message