harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From py...@apache.org
Subject svn commit: r463746 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/io/ test/java/org/apache/harmony/luni/tests/java/io/ test/java/tests/api/java/io/
Date Fri, 13 Oct 2006 17:32:02 GMT
Author: pyang
Date: Fri Oct 13 10:32:01 2006
New Revision: 463746

URL: http://svn.apache.org/viewvc?view=rev&rev=463746
Log:
Fix InputStreamReader's bug, remove duplicated test case of InputStreamReaderTest

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/luni/tests/java/io/InputStreamReaderTest.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/InputStreamReaderTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/InputStreamReader.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/InputStreamReader.java?view=diff&rev=463746&r1=463745&r2=463746
==============================================================================
--- 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
Fri Oct 13 10:32:01 2006
@@ -22,7 +22,10 @@
 import java.nio.CharBuffer;
 import java.nio.charset.Charset;
 import java.nio.charset.CharsetDecoder;
+import java.nio.charset.CoderResult;
 import java.nio.charset.CodingErrorAction;
+import java.nio.charset.MalformedInputException;
+import java.nio.charset.UnmappableCharacterException;
 import java.security.AccessController;
 import java.util.HashMap;
 
@@ -64,7 +67,9 @@
 		this.in = in;
 		String encoding = AccessController
 				.doPrivileged(new PriviAction<String>("file.encoding", "ISO8859_1")); //$NON-NLS-1$//$NON-NLS-2$
-		decoder = Charset.forName(encoding).newDecoder();
+		decoder = Charset.forName(encoding).newDecoder().onMalformedInput(
+                CodingErrorAction.REPLACE).onUnmappableCharacter(
+                CodingErrorAction.REPLACE);
 		chars.limit(0);
 	}
 
@@ -129,7 +134,9 @@
 	public InputStreamReader(InputStream in, Charset charset) {
 		super(in);
 		this.in = in;
-		decoder = charset.newDecoder();
+		decoder = charset.newDecoder().onMalformedInput(
+                CodingErrorAction.REPLACE).onUnmappableCharacter(
+                CodingErrorAction.REPLACE);
 		chars.limit(0);
 	}
 
@@ -171,7 +178,8 @@
 	/*
 	 * helper for getEncoding()
 	 */
-	static class HistoricalNamesUtil {
+	@SuppressWarnings("nls")
+    static class HistoricalNamesUtil {
 		private static HashMap<String, String> historicalNames = new HashMap<String, String>();
 		static {
 			historicalNames.put("Big5-HKSCS", "Big5_HKSCS");
@@ -440,7 +448,19 @@
 			} else {
 				bytes.limit(read);
 			}
-			decoder.decode(bytes, chars, endOfInput);
+			CoderResult result = decoder.decode(bytes, chars, endOfInput);
+            if(result.isMalformed()){
+                throw new MalformedInputException(result.length());
+            }else if(result.isUnmappable()){
+                throw new UnmappableCharacterException(result.length());
+            }
+            if(endOfInput){
+             // FIXME: should flush at first, but seems ICU has a bug that 
+             // it will throw IAE if some malform/unmappable bytes found during
+             // decoding
+             //   result = decoder.flush(chars);
+                decoder.reset();
+            }
 			bytes.clear();
 		} while (read > 0 && chars.position() == 0);
 		chars.flip();

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/io/InputStreamReaderTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/io/InputStreamReaderTest.java?view=diff&rev=463746&r1=463745&r2=463746
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/io/InputStreamReaderTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/io/InputStreamReaderTest.java
Fri Oct 13 10:32:01 2006
@@ -18,7 +18,6 @@
 
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
-import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.UnsupportedEncodingException;
 
@@ -37,28 +36,7 @@
     /**
      * @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;
@@ -72,55 +50,4 @@
 
     }
 
-    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;
-        }
-
-        @Override
-        public int read() {
-            if (count == 0) {
-                return -1;
-            }
-            count--;
-            return bytes[bytes.length - count];
-        }
-
-        @Override
-        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;
-        }
-
-        @Override
-        public int available() {
-            return count;
-        }
-    }
 }

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/InputStreamReaderTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/InputStreamReaderTest.java?view=diff&rev=463746&r1=463745&r2=463746
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/InputStreamReaderTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/InputStreamReaderTest.java
Fri Oct 13 10:32:01 2006
@@ -25,8 +25,11 @@
 import java.io.InputStreamReader;
 import java.io.OutputStreamWriter;
 import java.io.UnsupportedEncodingException;
+import java.nio.ByteBuffer;
 import java.nio.charset.Charset;
 import java.nio.charset.CharsetDecoder;
+import java.nio.charset.CodingErrorAction;
+import java.nio.charset.MalformedInputException;
 import java.util.Arrays;
 
 import junit.framework.TestCase;
@@ -49,6 +52,7 @@
 	public String fileString = "Test_All_Tests\nTest_java_io_BufferedInputStream\nTest_java_io_BufferedOutputStream\nTest_java_io_ByteArrayInputStream\nTest_java_io_ByteArrayOutputStream\nTest_java_io_DataInputStream\n";
 
 	static class LimitedByteArrayInputStream extends ByteArrayInputStream {
+        
 		// A ByteArrayInputStream that only returns a single byte per read
 		byte[] bytes;
 
@@ -158,9 +162,39 @@
      * Regression for Harmony-411
      */
     public void testRead1() throws IOException {
+        // if the decoder is constructed by InputStreamReader itself, the decoder's 
+        // default error action is REPLACE
         InputStreamReader isr = new InputStreamReader(new ByteArrayInputStream(
                 new byte[] { -32, -96 }), "UTF-8");
         assertEquals("read() return incorrect value", 65533, isr.read());
+        
+        InputStreamReader isr2 = new InputStreamReader(new ByteArrayInputStream(
+                new byte[] { -32, -96 }), Charset.forName("UTF-8"));
+        assertEquals("read() return incorrect value", 65533, isr2.read());
+        
+        // if the decoder is passed in, keep its status intacted
+        CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
+        decoder.onMalformedInput(CodingErrorAction.REPORT);
+        InputStreamReader isr3 = new InputStreamReader(new ByteArrayInputStream(
+                new byte[] { -32, -96 }), decoder);
+        try{
+           isr3.read();
+           fail("Should throw MalformedInputException");
+        }catch(MalformedInputException e){
+            //expected
+        }
+        
+        CharsetDecoder decoder2 = Charset.forName("UTF-8").newDecoder();
+        decoder2.onMalformedInput(CodingErrorAction.IGNORE);
+        InputStreamReader isr4 = new InputStreamReader(new ByteArrayInputStream(
+                new byte[] { -32, -96 }), decoder2);
+        assertEquals("read() return incorrect value", -1, isr4.read());
+        
+        CharsetDecoder decoder3 = Charset.forName("UTF-8").newDecoder();
+        decoder3.onMalformedInput(CodingErrorAction.REPLACE);
+        InputStreamReader isr5 = new InputStreamReader(new ByteArrayInputStream(
+                new byte[] { -32, -96 }), decoder3);
+        assertEquals("read() return incorrect value", 65533, isr5.read());
     }
 
 	/*
@@ -344,7 +378,7 @@
 			fail();
 		} catch (NullPointerException e) {
 		}
-		InputStreamReader reader2 = new InputStreamReader(in, decoder);
+        InputStreamReader reader2 = new InputStreamReader(in, decoder);
 		assertEquals(Charset.forName(reader2.getEncoding()), decoder.charset());
 		reader2.close();
 	}
@@ -452,7 +486,7 @@
 	/**
 	 * @tests java.io.InputStreamReader#read()
 	 */
-	public void test_read() {
+	public void test_read() throws IOException{
 		// Test for method int java.io.InputStreamReader.read()
 		try {
 			int c = is.read();
@@ -465,55 +499,32 @@
 		} catch (IOException e) {
 			fail("Exception during read test : " + e.getMessage());
 		}
-
-		try {
-			LimitedByteArrayInputStream in = new LimitedByteArrayInputStream(0);
-			InputStreamReader reader = new InputStreamReader(in, "UTF-16BE");
-			assertTrue("Incorrect byte UTF-16BE", reader.read() == '\u6172');
-		} catch (UnsupportedEncodingException e) {
-			// Can't test without the converter
-			System.out.println(e);
-		} catch (IOException e) {
-			fail("UTF-16BE unexpected 1: " + e);
-		}
-		try {
-			LimitedByteArrayInputStream in = new LimitedByteArrayInputStream(0);
-			InputStreamReader reader = new InputStreamReader(in, "UTF-16LE");
-			assertTrue("Incorrect byte UTF-16BE", reader.read() == '\u7261');
-		} catch (UnsupportedEncodingException e) {
-			// Can't test without the converter
-		} catch (IOException e) {
-			fail("UTF-16BE unexpected 2: " + e);
-		}
-		try {
-			LimitedByteArrayInputStream in = new LimitedByteArrayInputStream(1);
-			InputStreamReader reader = new InputStreamReader(in, "UTF-16");
-			assertTrue("Incorrect byte UTF-16BE", reader.read() == '\u7261');
-		} catch (UnsupportedEncodingException e) {
-			// Can't test without the converter
-		} catch (IOException e) {
-			fail("UTF-16BE unexpected 3: " + e);
-		}
-
-		try {
-			LimitedByteArrayInputStream in = new LimitedByteArrayInputStream(2);
-			InputStreamReader reader = new InputStreamReader(in, "ISO2022JP");
-			int ch = reader.read();
-			assertTrue("Incorrect byte ISO2022JP 1: " + ch, ch == '\u4e5d');
-			ch = reader.read();
-			assertTrue("Incorrect byte ISO2022JP 2: " + ch, ch == '\u7b2c');
-		} catch (UnsupportedEncodingException e) {
-			// Can't test without the converter
-			System.out.println(e);
-		} catch (IOException e) {
-			fail("ISO2022JP unexpected: " + e);
-		}
-
+        
+        // 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());
 	}
 
 	/**
-	 * @tests java.io.InputStreamReader#read(char[], int, int)
-	 */
+     * @tests java.io.InputStreamReader#read(char[], int, int)
+     */
 	public void test_read$CII() {
 		// Test for method int java.io.InputStreamReader.read(char [], int, int)
 		try {
@@ -541,4 +552,6 @@
 			fail("Exception during ready test : " + e.getMessage());
 		}
 	}
+    
+    
 }



Mime
View raw message