harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ton...@apache.org
Subject svn commit: r646264 [5/13] - in /harmony/enhanced/classlib/branches/java6: depends/build/ depends/build/platform/ depends/files/ modules/accessibility/ modules/annotation/ modules/applet/ modules/archive/ modules/archive/src/main/java/java/util/jar/ mo...
Date Wed, 09 Apr 2008 11:02:07 GMT
Modified: harmony/enhanced/classlib/branches/java6/modules/nio_char/src/main/java/org/apache/harmony/niochar/charset/UTF_8.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/nio_char/src/main/java/org/apache/harmony/niochar/charset/UTF_8.java?rev=646264&r1=646263&r2=646264&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/nio_char/src/main/java/org/apache/harmony/niochar/charset/UTF_8.java (original)
+++ harmony/enhanced/classlib/branches/java6/modules/nio_char/src/main/java/org/apache/harmony/niochar/charset/UTF_8.java Wed Apr  9 04:01:14 2008
@@ -26,8 +26,7 @@
 
 public class UTF_8 extends Charset {
 
-
-    // The next table contains information about UTF-8 charset and 
+    // The next table contains information about UTF-8 charset and
     // correspondence of 1st byte to the length of sequence
     // For information please visit http://www.ietf.org/rfc/rfc3629.txt
     //
@@ -41,66 +40,58 @@
     // 1111ouuu  1ouuzzzz  1oyyyyyy  1oxxxxxx   000uuuuu zzzzyyyy yyxxxxxx
 
     private static final int remainingBytes[] = {
-        // oxxxxxxx
-         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
-         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
-         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
-         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
-         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
-         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
-         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
-         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
-        // 1owwwwww
-        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-        // 11oyyyyy
-         1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
-         1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
-        // 111ozzzz
-         2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
-        // 1111ouuu
-         3,  3,  3,  3,  3,  3,  3,  3,
-        // > 11110111
-        -1, -1, -1, -1, -1, -1, -1, -1
-    };
+            // 1owwwwww
+            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+            // 11oyyyyy
+            1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+            1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+            // 111ozzzz
+            2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+            // 1111ouuu
+            3, 3, 3, 3, 3, 3, 3, 3,
+            // > 11110111
+            -1, -1, -1, -1, -1, -1, -1, -1 };
 
     private static final int remainingNumbers[] = {
-        0,       //  0                 1                 2                3
-        12416,   // (11o00000b <<  6)+(1o000000b)
-        925824,  // (111o0000b << 12)+(1o000000b <<  6)+(1o000000b)
-        63447168 // (1111o000b << 18)+(1o000000b << 12)+(1o000000b << 6)+(1o000000b)
+                   0, //                0                 1                2           3
+                4224, // (01o00000b <<  6)+(1o000000b)
+              401536, // (011o0000b << 12)+(1o000000b <<  6)+(1o000000b)
+            29892736  // (0111o000b << 18)+(1o000000b << 12)+(1o000000b << 6)+(1o000000b)
     };
-    
+
     public UTF_8(String canonicalName, String[] aliases) {
         super(canonicalName, aliases);
     }
 
+    @Override
+    @SuppressWarnings("nls")
     public boolean contains(Charset cs) {
         return cs.name().equalsIgnoreCase("UTF-8")
-            || cs.name().equalsIgnoreCase("US-ASCII")
-            || cs.name().equalsIgnoreCase("KOI8-R")
-            || cs.name().equalsIgnoreCase("windows-1250")
-            || cs.name().equalsIgnoreCase("windows-1251")
-            || cs.name().equalsIgnoreCase("windows-1252")
-            || cs.name().equalsIgnoreCase("windows-1253")
-            || cs.name().equalsIgnoreCase("windows-1254")
-            || cs.name().equalsIgnoreCase("windows-1257")
-            || cs.name().equalsIgnoreCase("ISO-8859-1")
-            || cs.name().equalsIgnoreCase("ISO-8859-2")
-            || cs.name().equalsIgnoreCase("ISO-8859-4")
-            || cs.name().equalsIgnoreCase("ISO-8859-5")
-            || cs.name().equalsIgnoreCase("ISO-8859-7")
-            || cs.name().equalsIgnoreCase("ISO-8859-9")
-            || cs.name().equalsIgnoreCase("ISO-8859-10")
-            || cs.name().equalsIgnoreCase("ISO-8859-13")
-            || cs.name().equalsIgnoreCase("ISO-8859-14")
-            || cs.name().equalsIgnoreCase("ISO-8859-15")
-            || cs.name().equalsIgnoreCase("ISO-8859-16")
-            || cs.name().equalsIgnoreCase("UTF-16")
-            || cs.name().equalsIgnoreCase("UTF-16LE")
-            || cs.name().equalsIgnoreCase("UTF-16BE");
+                || cs.name().equalsIgnoreCase("US-ASCII")
+                || cs.name().equalsIgnoreCase("KOI8-R")
+                || cs.name().equalsIgnoreCase("windows-1250")
+                || cs.name().equalsIgnoreCase("windows-1251")
+                || cs.name().equalsIgnoreCase("windows-1252")
+                || cs.name().equalsIgnoreCase("windows-1253")
+                || cs.name().equalsIgnoreCase("windows-1254")
+                || cs.name().equalsIgnoreCase("windows-1257")
+                || cs.name().equalsIgnoreCase("ISO-8859-1")
+                || cs.name().equalsIgnoreCase("ISO-8859-2")
+                || cs.name().equalsIgnoreCase("ISO-8859-4")
+                || cs.name().equalsIgnoreCase("ISO-8859-5")
+                || cs.name().equalsIgnoreCase("ISO-8859-7")
+                || cs.name().equalsIgnoreCase("ISO-8859-9")
+                || cs.name().equalsIgnoreCase("ISO-8859-10")
+                || cs.name().equalsIgnoreCase("ISO-8859-13")
+                || cs.name().equalsIgnoreCase("ISO-8859-14")
+                || cs.name().equalsIgnoreCase("ISO-8859-15")
+                || cs.name().equalsIgnoreCase("ISO-8859-16")
+                || cs.name().equalsIgnoreCase("UTF-16")
+                || cs.name().equalsIgnoreCase("UTF-16LE")
+                || cs.name().equalsIgnoreCase("UTF-16BE");
     }
 
     public CharsetDecoder newDecoder() {
@@ -118,60 +109,53 @@
         }
 
         protected CoderResult decodeLoop(ByteBuffer in, CharBuffer out) {
-            int outRemaining = out.remaining(); 
+            int outRemaining = out.remaining();
             int pos = in.position();
             int limit = in.limit();
-            if(in.hasArray() && out.hasArray()) {
-                byte[] bArr;
-                char[] cArr;
-            	bArr = in.array();
-            	cArr = out.array();
-                int outPos = out.position();
-                int x = pos;
-                try {
-                	for(x=pos; x<pos+in.remaining(); x++) {
-                        if (outRemaining == 0) {
-                        	in.position(x-1); out.position(outPos);
-                            return CoderResult.OVERFLOW;
-                        }
-
-                        int jchar = (bArr[x] & 0xFF);
+            if (in.hasArray() && out.hasArray()) {
+                final byte[] bArr = in.array();
+                final char[] cArr = out.array();
+                final int inIndexLimit = limit + in.arrayOffset();
+
+                int inIndex = pos + in.arrayOffset();
+                int outIndex = out.position() + out.arrayOffset();
+
+                // if someone would change the limit in process,
+                // he would face consequences
+                for (; inIndex < inIndexLimit && outRemaining > 0; inIndex++) {
+                    int jchar = bArr[inIndex];
+                    if (jchar < 0) {
+                        jchar = jchar & 0x7F;
                         int tail = remainingBytes[jchar];
+
                         if (tail == -1) {
-                        	in.position(x); out.position(outPos);
+                            in.position(inIndex - in.arrayOffset());
+                            out.position(outIndex - out.arrayOffset());
                             return CoderResult.unmappableForLength(1);
                         }
-                        if (limit - x < 1 + tail) {
-                        	in.position(x); out.position(outPos);
-                            return CoderResult.UNDERFLOW;
+                        if (inIndexLimit - inIndex < 1 + tail) {
+                            break;
                         }
 
-                        if (tail > 0) {
-                            int nextByte;
-                            for (int i = 0; i < tail; i++) {
-                                nextByte = bArr[x+i+1] & 0xFF;
-                                if ((nextByte & 0xC0) != 0x80) {
-                                	in.position(x+i); out.position(outPos);
-                                    return CoderResult.malformedForLength(1 + i);
-                                }
-                                jchar = (jchar << 6) + nextByte;
+                        for (int i = 0; i < tail; i++) {
+                            int nextByte = bArr[inIndex + i + 1] & 0xFF;
+                            if ((nextByte & 0xC0) != 0x80) {
+                                in.position(inIndex - in.arrayOffset());
+                                out.position(outIndex - out.arrayOffset());
+                                return CoderResult.malformedForLength(1 + i);
                             }
-                            jchar -= remainingNumbers[tail];
-                            x += tail;
+                            jchar = (jchar << 6) + nextByte;
                         }
-
-                        cArr[outPos++] = (char) jchar;
-                        outRemaining--;
-                	}
-                	in.position(x); out.position(outPos);
-                	if(outRemaining == 0 && in.hasRemaining()) {
-                		return CoderResult.OVERFLOW;
-                	}
-                    return CoderResult.UNDERFLOW;
-                } finally {
-                    in.position(x);
+                        jchar -= remainingNumbers[tail];
+                        inIndex += tail;
+                    }
+                    cArr[outIndex++] = (char) jchar;
+                    outRemaining--;
                 }
-            	
+                in.position(inIndex - in.arrayOffset());
+                out.position(outIndex - out.arrayOffset());
+                return (outRemaining == 0 && inIndex < inIndexLimit) ? CoderResult.OVERFLOW
+                        : CoderResult.UNDERFLOW;
             } else {
                 try {
                     while (pos < limit) {
@@ -179,21 +163,23 @@
                             return CoderResult.OVERFLOW;
                         }
 
-                        int jchar = (in.get() & 0xFF);
-                        int tail = remainingBytes[jchar];
-                        if (tail == -1) {
-                            return CoderResult.unmappableForLength(1);
-                        }
-                        if (limit - pos < 1 + tail) {
-                            return CoderResult.UNDERFLOW;
-                        }
+                        int jchar = in.get();
+                        if (jchar < 0) {
+                            jchar = jchar & 0x7F;
+                            int tail = remainingBytes[jchar];
+                            if (tail == -1) {
+                                return CoderResult.unmappableForLength(1);
+                            }
+                            if (limit - pos < 1 + tail) {
+                                return CoderResult.UNDERFLOW;
+                            }
 
-                        if (tail > 0) {
                             int nextByte;
                             for (int i = 0; i < tail; i++) {
                                 nextByte = in.get() & 0xFF;
                                 if ((nextByte & 0xC0) != 0x80) {
-                                    return CoderResult.malformedForLength(1 + i);
+                                    return CoderResult
+                                            .malformedForLength(1 + i);
                                 }
                                 jchar = (jchar << 6) + nextByte;
                             }
@@ -201,7 +187,6 @@
                             pos += tail;
                         }
                         pos++;
-
                         out.put((char) jchar);
                         outRemaining--;
                     }
@@ -223,20 +208,21 @@
             int outRemaining = out.remaining();
             int pos = in.position();
             int limit = in.limit();
-            if(in.hasArray() && out.hasArray()) {
+            if (in.hasArray() && out.hasArray()) {
                 byte[] bArr;
                 char[] cArr;
                 int x = pos;
-            	bArr = out.array();
-            	cArr = in.array();
+                bArr = out.array();
+                cArr = in.array();
                 int outPos = out.position();
                 int rem = in.remaining();
-            	for(x=pos; x<pos+rem; x++) {
+                for (x = pos; x < pos + rem; x++) {
                     int jchar = (cArr[x] & 0xFFFF);
-                    
+
                     if (jchar <= 0x7F) {
                         if (outRemaining < 1) {
-                        	in.position(x); out.position(outPos);
+                            in.position(x);
+                            out.position(outPos);
                             return CoderResult.OVERFLOW;
                         }
                         bArr[outPos++] = (byte) (jchar & 0xFF);
@@ -244,7 +230,8 @@
                     } else if (jchar <= 0x7FF) {
 
                         if (outRemaining < 2) {
-                        	in.position(x); out.position(outPos);
+                            in.position(x);
+                            out.position(outPos);
                             return CoderResult.OVERFLOW;
                         }
                         bArr[outPos++] = (byte) (0xC0 + ((jchar >> 6) & 0x1F));
@@ -255,36 +242,40 @@
 
                         // in has to have one byte more.
                         if (limit <= x + 1) {
-                        	in.position(x); out.position(outPos);
+                            in.position(x);
+                            out.position(outPos);
                             return CoderResult.UNDERFLOW;
                         }
 
                         if (outRemaining < 4) {
-                        	in.position(x); out.position(outPos);
+                            in.position(x);
+                            out.position(outPos);
                             return CoderResult.OVERFLOW;
                         }
 
                         // The surrogate pair starts with a low-surrogate.
                         if (jchar >= 0xDC00) {
-                        	in.position(x); out.position(outPos);
+                            in.position(x);
+                            out.position(outPos);
                             return CoderResult.malformedForLength(1);
                         }
 
-                        int jchar2 = cArr[x+1] & 0xFFFF;
-                        
+                        int jchar2 = cArr[x + 1] & 0xFFFF;
+
                         // The surrogate pair ends with a high-surrogate.
                         if (jchar2 < 0xDC00) {
-                        	in.position(x); out.position(outPos);
+                            in.position(x);
+                            out.position(outPos);
                             return CoderResult.malformedForLength(1);
                         }
 
-                        // Note, the Unicode scalar value n is defined 
+                        // Note, the Unicode scalar value n is defined
                         // as follows:
-                        //   n = (jchar-0xD800)*0x400+(jchar2-0xDC00)+0x10000
+                        // n = (jchar-0xD800)*0x400+(jchar2-0xDC00)+0x10000
                         // Where jchar is a high-surrogate,
-                        //       jchar2 is a low-surrogate.
+                        // jchar2 is a low-surrogate.
                         int n = (jchar << 10) + jchar2 + 0xFCA02400;
-                        
+
                         bArr[outPos++] = (byte) (0xF0 + ((n >> 18) & 0x07));
                         bArr[outPos++] = (byte) (0x80 + ((n >> 12) & 0x3F));
                         bArr[outPos++] = (byte) (0x80 + ((n >> 6) & 0x3F));
@@ -295,7 +286,8 @@
                     } else {
 
                         if (outRemaining < 3) {
-                        	in.position(x); out.position(outPos);
+                            in.position(x);
+                            out.position(outPos);
                             return CoderResult.OVERFLOW;
                         }
                         bArr[outPos++] = (byte) (0xE0 + ((jchar >> 12) & 0x0F));
@@ -304,17 +296,18 @@
                         outRemaining -= 3;
                     }
                     if (outRemaining == 0) {
-                    	in.position(x+1); out.position(outPos);
+                        in.position(x + 1);
+                        out.position(outPos);
                         return CoderResult.OVERFLOW;
                     }
 
-            	    
-            	}
-            	if(rem!=0) {
-                	in.position(x); out.position(outPos);
-            	}
+                }
+                if (rem != 0) {
+                    in.position(x);
+                    out.position(outPos);
+                }
             } else {
-            	try {
+                try {
                     while (pos < limit) {
                         if (outRemaining == 0) {
                             return CoderResult.OVERFLOW;
@@ -356,17 +349,17 @@
                             }
 
                             int jchar2 = (in.get() & 0xFFFF);
-                            
+
                             // The surrogate pair ends with a high-surrogate.
                             if (jchar2 < 0xDC00) {
                                 return CoderResult.malformedForLength(1);
                             }
 
-                            // Note, the Unicode scalar value n is defined 
+                            // Note, the Unicode scalar value n is defined
                             // as follows:
-                            //   n = (jchar-0xD800)*0x400+(jchar2-0xDC00)+0x10000
+                            // n = (jchar-0xD800)*0x400+(jchar2-0xDC00)+0x10000
                             // Where jchar is a high-surrogate,
-                            //       jchar2 is a low-surrogate.
+                            // jchar2 is a low-surrogate.
                             int n = (jchar << 10) + jchar2 + 0xFCA02400;
 
                             out.put((byte) (0xF0 + ((n >> 18) & 0x07)));
@@ -392,8 +385,8 @@
                     in.position(pos);
                 }
             }
-            
-                return CoderResult.UNDERFLOW;
+
+            return CoderResult.UNDERFLOW;
         }
 
     }

Modified: harmony/enhanced/classlib/branches/java6/modules/nio_char/src/test/java/org/apache/harmony/nio_char/tests/java/nio/charset/CharsetEncoderTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/nio_char/src/test/java/org/apache/harmony/nio_char/tests/java/nio/charset/CharsetEncoderTest.java?rev=646264&r1=646263&r2=646264&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/nio_char/src/test/java/org/apache/harmony/nio_char/tests/java/nio/charset/CharsetEncoderTest.java (original)
+++ harmony/enhanced/classlib/branches/java6/modules/nio_char/src/test/java/org/apache/harmony/nio_char/tests/java/nio/charset/CharsetEncoderTest.java Wed Apr  9 04:01:14 2008
@@ -16,6 +16,7 @@
 
 package org.apache.harmony.nio_char.tests.java.nio.charset;
 
+import java.io.IOException;
 import java.nio.BufferOverflowException;
 import java.nio.ByteBuffer;
 import java.nio.CharBuffer;
@@ -29,29 +30,29 @@
 
 public class CharsetEncoderTest extends TestCase {
 
-	/**
-	 * @tests java.nio.charset.CharsetEncoder.CharsetEncoder(
-	 *        java.nio.charset.Charset, float, float)
-	 */
-	public void test_ConstructorLjava_nio_charset_CharsetFF() {
-		// Regression for HARMONY-141
-		try {
-			Charset cs = Charset.forName("UTF-8"); //$NON-NLS-1$
-			new MockCharsetEncoderForHarmony141(cs, 1.1f, 1);
-			fail("Assert 0: Should throw IllegalArgumentException."); //$NON-NLS-1$
-		} catch (IllegalArgumentException e) {
-			// expected
-		}
-
-		try {
-			Charset cs = Charset.forName("ISO8859-1"); //$NON-NLS-1$
-			new MockCharsetEncoderForHarmony141(cs, 1.1f, 1,
-					new byte[] { 0x1a });
-			fail("Assert 1: Should throw IllegalArgumentException."); //$NON-NLS-1$
-		} catch (IllegalArgumentException e) {
-			// expected
-		}
-	}
+    /**
+     * @tests java.nio.charset.CharsetEncoder.CharsetEncoder(
+     *        java.nio.charset.Charset, float, float)
+     */
+    public void test_ConstructorLjava_nio_charset_CharsetFF() {
+        // Regression for HARMONY-141
+        try {
+            Charset cs = Charset.forName("UTF-8"); //$NON-NLS-1$
+            new MockCharsetEncoderForHarmony141(cs, 1.1f, 1);
+            fail("Assert 0: Should throw IllegalArgumentException."); //$NON-NLS-1$
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+
+        try {
+            Charset cs = Charset.forName("ISO8859-1"); //$NON-NLS-1$
+            new MockCharsetEncoderForHarmony141(cs, 1.1f, 1,
+                    new byte[] { 0x1a });
+            fail("Assert 1: Should throw IllegalArgumentException."); //$NON-NLS-1$
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+    }
 
     /**
      * @tests java.nio.charset.CharsetEncoder.CharsetEncoder(
@@ -69,21 +70,21 @@
 
     public static class MockCharsetEncoderForHarmony141 extends CharsetEncoder {
 
-		protected MockCharsetEncoderForHarmony141(Charset cs,
-				float averageBytesPerChar, float maxBytesPerChar) {
-			super(cs, averageBytesPerChar, maxBytesPerChar);
-		}
-
-		public MockCharsetEncoderForHarmony141(Charset cs,
-				float averageBytesPerChar, float maxBytesPerChar,
-				byte[] replacement) {
-			super(cs, averageBytesPerChar, maxBytesPerChar, replacement);
-		}
-
-		protected CoderResult encodeLoop(CharBuffer in, ByteBuffer out) {
-			return null;
-		}
-	}
+        protected MockCharsetEncoderForHarmony141(Charset cs,
+                float averageBytesPerChar, float maxBytesPerChar) {
+            super(cs, averageBytesPerChar, maxBytesPerChar);
+        }
+
+        public MockCharsetEncoderForHarmony141(Charset cs,
+                float averageBytesPerChar, float maxBytesPerChar,
+                byte[] replacement) {
+            super(cs, averageBytesPerChar, maxBytesPerChar, replacement);
+        }
+
+        protected CoderResult encodeLoop(CharBuffer in, ByteBuffer out) {
+            return null;
+        }
+    }
 
     public static class MockCharsetEncoderForHarmony491 extends CharsetEncoder {
 
@@ -104,66 +105,85 @@
     /*
      * Test malfunction encode(CharBuffer)
      */
-	public void test_EncodeLjava_nio_CharBuffer() throws Exception {
-		MockMalfunctionCharset cs = new MockMalfunctionCharset("mock", null);
-		try {
-			cs.encode(CharBuffer.wrap("AB"));
-			fail("should throw CoderMalfunctionError");// NON-NLS-1$
-		} catch (CoderMalfunctionError e) {
-			// expected
-		}
-	}
-
-	/*
-	 * Mock charset class with malfunction decode & encode.
-	 */
-	static final class MockMalfunctionCharset extends Charset {
-
-		public MockMalfunctionCharset(String canonicalName, String[] aliases) {
-			super(canonicalName, aliases);
-		}
-
-		public boolean contains(Charset cs) {
-			return false;
-		}
-
-		public CharsetDecoder newDecoder() {
-			return Charset.forName("UTF-8").newDecoder();
-		}
-
-		public CharsetEncoder newEncoder() {
-			return new MockMalfunctionEncoder(this);
-		}
-	}
-
-	/*
-	 * Mock encoder. encodeLoop always throws unexpected exception.
-	 */
-	static class MockMalfunctionEncoder extends java.nio.charset.CharsetEncoder {
-
-		public MockMalfunctionEncoder(Charset cs) {
-			super(cs, 1, 3, new byte[] { (byte) '?' });
-		}
-
-		protected CoderResult encodeLoop(CharBuffer in, ByteBuffer out) {
-			throw new BufferOverflowException();
-		}
-	}
-
-	/*
-	 * Test reserve bytes encode(CharBuffer,ByteBuffer,boolean)
-	 */
-	public void test_EncodeLjava_nio_CharBufferLjava_nio_ByteBufferB() {
-		CharsetEncoder encoder = Charset.forName("utf-8").newEncoder();
-		CharBuffer in1 = CharBuffer.wrap("\ud800");
-		CharBuffer in2 = CharBuffer.wrap("\udc00");
-		ByteBuffer out = ByteBuffer.allocate(4);
-		encoder.reset();
-		CoderResult result = encoder.encode(in1, out, false);
-		assertEquals(4, out.remaining());
-		assertTrue(result.isUnderflow());
-		result = encoder.encode(in2, out, true);
-		assertEquals(4, out.remaining());
-		assertTrue(result.isMalformed());
-	}
+    public void test_EncodeLjava_nio_CharBuffer() throws Exception {
+        MockMalfunctionCharset cs = new MockMalfunctionCharset("mock", null);
+        try {
+            cs.encode(CharBuffer.wrap("AB"));
+            fail("should throw CoderMalfunctionError");// NON-NLS-1$
+        } catch (CoderMalfunctionError e) {
+            // expected
+        }
+    }
+
+    /*
+     * Mock charset class with malfunction decode & encode.
+     */
+    static final class MockMalfunctionCharset extends Charset {
+
+        public MockMalfunctionCharset(String canonicalName, String[] aliases) {
+            super(canonicalName, aliases);
+        }
+
+        public boolean contains(Charset cs) {
+            return false;
+        }
+
+        public CharsetDecoder newDecoder() {
+            return Charset.forName("UTF-8").newDecoder();
+        }
+
+        public CharsetEncoder newEncoder() {
+            return new MockMalfunctionEncoder(this);
+        }
+    }
+
+    /*
+     * Mock encoder. encodeLoop always throws unexpected exception.
+     */
+    static class MockMalfunctionEncoder extends java.nio.charset.CharsetEncoder {
+
+        public MockMalfunctionEncoder(Charset cs) {
+            super(cs, 1, 3, new byte[] { (byte) '?' });
+        }
+
+        protected CoderResult encodeLoop(CharBuffer in, ByteBuffer out) {
+            throw new BufferOverflowException();
+        }
+    }
+
+    /*
+     * Test reserve bytes encode(CharBuffer,ByteBuffer,boolean)
+     */
+    public void test_EncodeLjava_nio_CharBufferLjava_nio_ByteBufferB() {
+        CharsetEncoder encoder = Charset.forName("utf-8").newEncoder();
+        CharBuffer in1 = CharBuffer.wrap("\ud800");
+        CharBuffer in2 = CharBuffer.wrap("\udc00");
+        ByteBuffer out = ByteBuffer.allocate(4);
+        encoder.reset();
+        CoderResult result = encoder.encode(in1, out, false);
+        assertEquals(4, out.remaining());
+        assertTrue(result.isUnderflow());
+        result = encoder.encode(in2, out, true);
+        assertEquals(4, out.remaining());
+        assertTrue(result.isMalformed());
+    }
+
+    /**
+     * @tests {@link java.nio.charset.Charset#encode(java.nio.CharBuffer)
+     */
+    public void testUtf8Encoding() throws IOException {
+        byte[] orig = new byte[] { (byte) 0xed, (byte) 0xa0,
+                (byte) 0x80 };
+        String s = new String(orig, "UTF-8");
+        assertEquals(1, s.length());
+        assertEquals(55296, s.charAt(0));
+        Charset.forName("UTF-8").encode(CharBuffer.wrap(s));
+//        ByteBuffer buf = <result> 
+//        for (byte o : orig) {
+//            byte b = 0;
+//            buf.get(b);
+//            assertEquals(o, b);
+//        }
+    }
+
 }

Modified: harmony/enhanced/classlib/branches/java6/modules/nio_char/src/test/java/tests/api/java/nio/charset/ASCCharsetDecoderTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/nio_char/src/test/java/tests/api/java/nio/charset/ASCCharsetDecoderTest.java?rev=646264&r1=646263&r2=646264&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/nio_char/src/test/java/tests/api/java/nio/charset/ASCCharsetDecoderTest.java (original)
+++ harmony/enhanced/classlib/branches/java6/modules/nio_char/src/test/java/tests/api/java/nio/charset/ASCCharsetDecoderTest.java Wed Apr  9 04:01:14 2008
@@ -42,7 +42,7 @@
 	// assertEquals(decoder.maxCharsPerByte(), 2, 0.001);
 	// }
 
-	ByteBuffer getUnmappedByteBuffer() throws UnsupportedEncodingException {
+	ByteBuffer getUnmappedByteBuffer() {
 		// FIXME: different here
 		return null;
 		// ByteBuffer buffer = ByteBuffer.allocate(8);
@@ -53,11 +53,11 @@
 
 	}
 
-	ByteBuffer getMalformByteBuffer() throws UnsupportedEncodingException {
+	ByteBuffer getMalformedByteBuffer() {
 		// FIXME: different here
 		ByteBuffer buffer = ByteBuffer.allocate(8);
 		buffer.put((byte) -1);
-		buffer.put(unibytes);
+		buffer.put(getByteBuffer());
 		buffer.flip();
 		return buffer;
 

Modified: harmony/enhanced/classlib/branches/java6/modules/nio_char/src/test/java/tests/api/java/nio/charset/CharsetDecoderTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/nio_char/src/test/java/tests/api/java/nio/charset/CharsetDecoderTest.java?rev=646264&r1=646263&r2=646264&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/nio_char/src/test/java/tests/api/java/nio/charset/CharsetDecoderTest.java (original)
+++ harmony/enhanced/classlib/branches/java6/modules/nio_char/src/test/java/tests/api/java/nio/charset/CharsetDecoderTest.java Wed Apr  9 04:01:14 2008
@@ -33,752 +33,833 @@
  */
 public class CharsetDecoderTest extends TestCase {
 
-	static final String unistr = " buffer";// \u8000\u8001\u00a5\u3000\r\n";
+    protected static final int MAX_BYTES = 3;
 
-	byte[] unibytes = new byte[] { 32, 98, 117, 102, 102, 101, 114 };
+    protected static final double AVER_BYTES = 0.5;
 
-	protected static final int MAX_BYTES = 3;
+    // default charset
+    private static final Charset MOCKCS = new CharsetEncoderTest.MockCharset(
+            "mock", new String[0]);
+
+    Charset cs = MOCKCS;
+
+    // default decoder
+    protected static CharsetDecoder decoder;
+
+    String bom = "";
+
+    protected void setUp() throws Exception {
+        super.setUp();
+        decoder = cs.newDecoder();
+    }
+
+    protected void tearDown() throws Exception {
+        super.tearDown();
+    }
+
+    // FIXME: give up this tests
+    // /*
+    // * test default value
+    // */
+    // public void testDefaultCharsPerByte() {
+    // assertTrue(decoder.averageCharsPerByte() == AVER_BYTES);
+    // assertTrue(decoder.maxCharsPerByte() == MAX_BYTES);
+    // }
+
+    public void testDefaultValues() {
+        assertSame(cs, decoder.charset());
+        try {
+            decoder.detectedCharset();
+            fail("should unsupported");
+        } catch (UnsupportedOperationException e) {
+        }
+        try {
+            assertTrue(decoder.isCharsetDetected());
+            fail("should unsupported");
+        } catch (UnsupportedOperationException e) {
+        }
+        assertFalse(decoder.isAutoDetecting());
+        assertSame(CodingErrorAction.REPORT, decoder.malformedInputAction());
+        assertSame(CodingErrorAction.REPORT, decoder
+                .unmappableCharacterAction());
+        assertEquals(decoder.replacement(), "\ufffd");
+    }
+
+    /*
+     * test constructor
+     */
+    public void testCharsetDecoder() {
+        // default value
+        decoder = new MockCharsetDecoder(cs, (float) AVER_BYTES, MAX_BYTES);
+
+        // normal case
+        CharsetDecoder ec = new MockCharsetDecoder(cs, 1, MAX_BYTES);
+        assertSame(ec.charset(), cs);
+        assertEquals(1.0, ec.averageCharsPerByte(), 0.0);
+        assertTrue(ec.maxCharsPerByte() == MAX_BYTES);
+
+        /*
+         * ------------------------ Exceptional cases -------------------------
+         */
+        // Normal case: null charset
+        ec = new MockCharsetDecoder(null, 1, MAX_BYTES);
+        assertNull(ec.charset());
+        assertEquals(1.0, ec.averageCharsPerByte(), 0.0);
+        assertTrue(ec.maxCharsPerByte() == MAX_BYTES);
+
+        ec = new MockCharsetDecoder(new CharsetEncoderTest.MockCharset("mock",
+                new String[0]), 1, MAX_BYTES);
+
+        // Commented out since the comment is wrong since MAX_BYTES > 1
+        // // OK: average length less than max length
+        // ec = new MockCharsetDecoder(cs, MAX_BYTES, 1);
+        // assertTrue(ec.averageCharsPerByte() == MAX_BYTES);
+        // assertTrue(ec.maxCharsPerByte() == 1);
+
+        // Illegal Argument: zero length
+        try {
+            ec = new MockCharsetDecoder(cs, 0, MAX_BYTES);
+            fail("should throw IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+        }
+        try {
+            ec = new MockCharsetDecoder(cs, 1, 0);
+            fail("should throw IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+        }
+
+        // Illegal Argument: negative length
+        try {
+            ec = new MockCharsetDecoder(cs, -1, MAX_BYTES);
+            fail("should throw IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+        }
+        try {
+            ec = new MockCharsetDecoder(cs, 1, -1);
+            fail("should throw IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+        }
+    }
+
+    /*
+     * test onMalformedInput
+     */
+    public void testOnMalformedInput() {
+        assertSame(CodingErrorAction.REPORT, decoder.malformedInputAction());
+        try {
+            decoder.onMalformedInput(null);
+            fail("should throw null pointer exception");
+        } catch (IllegalArgumentException e) {
+        }
+        decoder.onMalformedInput(CodingErrorAction.IGNORE);
+        assertSame(CodingErrorAction.IGNORE, decoder.malformedInputAction());
+    }
+
+    /*
+     * test unmappableCharacter
+     */
+    public void testOnUnmappableCharacter() {
+        assertSame(CodingErrorAction.REPORT, decoder
+                .unmappableCharacterAction());
+        try {
+            decoder.onUnmappableCharacter(null);
+            fail("should throw null pointer exception");
+        } catch (IllegalArgumentException e) {
+        }
+        decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
+        assertSame(CodingErrorAction.IGNORE, decoder
+                .unmappableCharacterAction());
+    }
+
+    /*
+     * test replaceWith
+     */
+    public void testReplaceWith() {
+        try {
+            decoder.replaceWith(null);
+            fail("should throw null pointer exception");
+        } catch (IllegalArgumentException e) {
+        }
+        try {
+            decoder.replaceWith("");
+            fail("should throw null pointer exception");
+        } catch (IllegalArgumentException e) {
+        }
+        try {
+            decoder.replaceWith("testReplaceWith");
+            fail("should throw illegal argument exception");
+        } catch (IllegalArgumentException e) {
+        }
+
+        decoder.replaceWith("a");
+        assertSame("a", decoder.replacement());
+    }
+
+    /*
+     * Class under test for CharBuffer decode(ByteBuffer)
+     */
+    public void testDecodeByteBuffer() throws CharacterCodingException {
+        implTestDecodeByteBuffer();
+    }
+
+    void implTestDecodeByteBuffer() throws CharacterCodingException {
+        // Null pointer
+        try {
+            decoder.decode(null);
+            fail("should throw null pointer exception");
+        } catch (NullPointerException e) {
+        }
+
+        // empty input buffer
+        CharBuffer out = decoder.decode(ByteBuffer.allocate(0));
+        assertCharBufferValue("", out);
+
+        // normal case
+        ByteBuffer in = getByteBuffer();
+        out = decoder.decode(in);
+        assertEquals(0, out.position());
+        assertEquals(getString().length(), out.limit());
+        assertEquals(getString().length(), out.remaining());
+        assertCharBufferValue(getString(), out);
+
+        // normal read only case
+        in = getByteBuffer().asReadOnlyBuffer();
+        out = decoder.decode(in);
+        assertEquals(out.position(), 0);
+        assertEquals(out.limit(), getString().length());
+        assertEquals(out.remaining(), getString().length());
+        assertCharBufferValue(getString(), out);
+    }
+
+    public void testDecodeByteBufferException()
+            throws CharacterCodingException, UnsupportedEncodingException {
+        CharBuffer out;
+        ByteBuffer in;
+        String replaceStr = decoder.replacement() + getString();
+
+        // MalformedException:
+        decoder.onMalformedInput(CodingErrorAction.REPORT);
+        decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
+        in = getMalformedByteBuffer();
+        if (in != null) {
+            try {
+                CharBuffer buffer = decoder.decode(in);
+                assertTrue(buffer.remaining() > 0);
+                fail("should throw MalformedInputException");
+            } catch (MalformedInputException e) {
+            }
+
+            decoder.reset();
+            in.rewind();
+            decoder.onMalformedInput(CodingErrorAction.IGNORE);
+            out = decoder.decode(in);
+            assertCharBufferValue(getString(), out);
+
+            decoder.reset();
+            in.rewind();
+            decoder.onMalformedInput(CodingErrorAction.REPLACE);
+            out = decoder.decode(in);
+            assertCharBufferValue(replaceStr, out);
+        }
+
+        // Unmapped Exception:
+        decoder.onMalformedInput(CodingErrorAction.REPORT);
+        decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
+        in = getUnmappedByteBuffer();
+        if (in != null) {
+            try {
+                decoder.decode(in);
+                fail("should throw UnmappableCharacterException");
+            } catch (UnmappableCharacterException e) {
+            }
+
+            decoder.reset();
+            in.rewind();
+            decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
+            out = decoder.decode(in);
+            assertCharBufferValue(getString(), out);
+
+            decoder.reset();
+            in.rewind();
+            decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
+            out = decoder.decode(in);
+            assertCharBufferValue(replaceStr, out);
+        }
+
+        // RuntimeException
+        try {
+            decoder.decode(getExceptionByteArray());
+            fail("should throw runtime exception");
+        } catch (RuntimeException e) {
+        }
+    }
+
+    /*
+     * Class under test for CoderResult decode(ByteBuffer, CharBuffer, boolean)
+     */
+    public void testDecodeByteBufferCharBuffer() {
+        implTestDecodeByteBufferCharBuffer(getByteBuffer());
+    }
+
+    public void testDecodeByteBufferCharBufferReadOnly() {
+        implTestDecodeByteBufferCharBuffer(getByteBuffer());
+    }
+
+    void implTestDecodeByteBufferCharBuffer(ByteBuffer in) {
+        CharBuffer out = CharBuffer.allocate(100);
+
+        // Null pointer
+        decoder.reset();
+        try {
+            decoder.decode(null, out, true);
+            fail("NullPointerException expected");
+        } catch (NullPointerException e) {
+        }
+        try {
+            decoder.decode(in, null, true);
+            fail("NullPointerException expected");
+        } catch (NullPointerException e) {
+        }
+
+        // normal case, one complete operation
+        decoder.reset();
+        in.rewind();
+        out.rewind();
+        assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, true));
+        assertEquals(out.limit(), 100);
+        assertEquals(out.position(), getString().length());
+        assertEquals(out.remaining(), 100 - getString().length());
+        assertEquals(out.capacity(), 100);
+        assertCharBufferValue(getString(), out);
+        decoder.flush(out);
+
+        // normal case, one complete operation, but call twice, first time set
+        // endOfInput to false
+        decoder.reset();
+        in.rewind();
+        out.clear();
+        assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, false));
+        assertEquals(out.limit(), 100);
+        assertEquals(out.position(), getString().length());
+        assertEquals(out.remaining(), 100 - getString().length());
+        assertEquals(out.capacity(), 100);
+        assertCharBufferValue(getString(), out);
+
+        decoder.reset();
+        in.rewind();
+        out.clear();
+        assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, false));
+        in = getHeadlessByteBuffer();
+        assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, false));
+        in.rewind();
+        assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, true));
+        assertEquals(out.limit(), 100);
+        assertTrue(out.position() > 0);
+        assertEquals(out.remaining(), out.capacity() - out.position());
+        assertEquals(out.capacity(), 100);
+        assertCharBufferValue(getString() + getString() + getString(), out);
+
+        // overflow
+        out = CharBuffer.allocate(4);
+        decoder.reset();
+        in = getByteBuffer();
+        out.rewind();
+        assertSame(CoderResult.OVERFLOW, decoder.decode(in, out, false));
+
+        assertCharBufferValue(getString().substring(0, 4), out);
+        out = CharBuffer.allocate(100);
+        assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, false));
+        assertCharBufferValue(getString().substring(4), out);
+        in.rewind();
+        out = CharBuffer.allocate(100);
+        assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, true));
+        assertCharBufferValue(bom + getString(), out);
+    }
+
+    public void testDecodeCharBufferByteBufferUnmappedException()
+            throws CharacterCodingException, UnsupportedEncodingException {
+        implTestDecodeCharBufferByteBufferUnmappedException(
+                getUnmappedByteBuffer(), true);
+    }
+
+    public void testDecodeCharBufferByteIncompleteBufferUnmappedException()
+            throws CharacterCodingException, UnsupportedEncodingException {
+        implTestDecodeCharBufferByteBufferUnmappedException(
+                getUnmappedByteBuffer(), false);
+    }
+
+    public void testDecodeCharBufferByteReadOnlyBufferUnmappedException()
+            throws CharacterCodingException, UnsupportedEncodingException {
+        implTestDecodeCharBufferByteBufferUnmappedException(
+                readOnly(getUnmappedByteBuffer()), true);
+    }
+
+    public void testDecodeCharBufferByteReadOnlyIncompleteBufferUnmappedException()
+            throws CharacterCodingException, UnsupportedEncodingException {
+        implTestDecodeCharBufferByteBufferUnmappedException(
+                readOnly(getUnmappedByteBuffer()), false);
+    }
+
+    void implTestDecodeCharBufferByteBufferUnmappedException(ByteBuffer in,
+            boolean endOfInput) throws CharacterCodingException,
+            UnsupportedEncodingException {
+        if (null == in) {
+            return;
+        }
+        CharBuffer out = CharBuffer.allocate(50);
+
+        decoder.onMalformedInput(CodingErrorAction.REPORT);
+        decoder.reset();
+        decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
+        CoderResult result = decoder.decode(in, out, endOfInput);
+        assertTrue(result.isUnmappable());
+
+        decoder.reset();
+        out.clear();
+        in.rewind();
+        decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
+        assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, endOfInput));
+        assertCharBufferValue(getString(), out);
+
+        decoder.reset();
+        out.clear();
+        in.rewind();
+        decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
+        assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, endOfInput));
+        assertCharBufferValue(decoder.replacement() + getString(), out);
+    }
+
+    public void testDecodeCharBufferByteBufferMalformedException()
+            throws CharacterCodingException, UnsupportedEncodingException {
+        implTestDecodeCharBufferByteBufferMalformedException(
+                getMalformedByteBuffer(), true);
+    }
+
+    public void testDecodeCharBufferByteIncompleteBufferMalformedException()
+            throws CharacterCodingException, UnsupportedEncodingException {
+
+        implTestDecodeCharBufferByteBufferMalformedException(
+                getMalformedByteBuffer(), false);
+    }
+
+    public void testDecodeCharBufferByteReadOnlyBufferMalformedException()
+            throws CharacterCodingException, UnsupportedEncodingException {
+        implTestDecodeCharBufferByteBufferMalformedException(
+                readOnly(getMalformedByteBuffer()), true);
+    }
+
+    public void testDecodeCharBufferByteReadOnlyIncompleteBufferMalformedException()
+            throws CharacterCodingException, UnsupportedEncodingException {
+        implTestDecodeCharBufferByteBufferMalformedException(
+                readOnly(getMalformedByteBuffer()), false);
+    }
+
+    void implTestDecodeCharBufferByteBufferMalformedException(ByteBuffer in,
+            boolean endOfInput) throws CharacterCodingException,
+            UnsupportedEncodingException {
+        if (null == in) {
+            return;
+        }
+        CharBuffer out = CharBuffer.allocate(getString().length() * 3);
+        decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
+        decoder.reset();
+        decoder.onMalformedInput(CodingErrorAction.REPORT);
+        CoderResult result = decoder.decode(in, out, endOfInput);
+        assertTrue(result.isMalformed());
+
+        decoder.reset();
+        out.clear();
+        in.rewind();
+        decoder.onMalformedInput(CodingErrorAction.IGNORE);
+        assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, endOfInput));
+        assertCharBufferValue(getString(), out);
+
+        decoder.reset();
+        out.clear();
+        in.rewind();
+        decoder.onMalformedInput(CodingErrorAction.REPLACE);
+        assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, endOfInput));
+        assertCharBufferValue(decoder.replacement() + getString(), out);
+    }
+
+    public void testDecodeCharBufferByteBufferException()
+            throws CharacterCodingException, UnsupportedEncodingException {
+        implTestDecodeCharBufferByteBufferException(getExceptionByteArray(),
+                true);
+    }
+
+    public void testDecodeCharBufferByteIncompleteBufferException()
+            throws CharacterCodingException, UnsupportedEncodingException {
+        implTestDecodeCharBufferByteBufferException(getExceptionByteArray(),
+                false);
+    }
+
+    public void testDecodeCharBufferByteReadOnlyBufferException()
+            throws CharacterCodingException, UnsupportedEncodingException {
+        implTestDecodeCharBufferByteBufferException(
+                readOnly(getExceptionByteArray()), true);
+    }
+
+    public void testDecodeCharBufferByteReadOnlyIncompleteBufferException()
+            throws CharacterCodingException, UnsupportedEncodingException {
+        implTestDecodeCharBufferByteBufferException(
+                readOnly(getExceptionByteArray()), false);
+    }
+
+    void implTestDecodeCharBufferByteBufferException(ByteBuffer in,
+            boolean endOfInput) throws CharacterCodingException,
+            UnsupportedEncodingException {
+        CharBuffer out = CharBuffer.allocate(50);
+        decoder.reset();
+        try {
+            decoder.decode(in, out, endOfInput);
+            fail("should throw runtime exception");
+        } catch (RuntimeException e) {
+        }
+    }
+
+    private ByteBuffer readOnly(ByteBuffer b) {
+        if (null == b) {
+            return null;
+        }
+        return b.asReadOnlyBuffer();
+    }
+
+    protected String getString() {
+        return " buffer";
+    }
+
+    protected ByteBuffer getByteBuffer() {
+        return ByteBuffer.wrap(new byte[] { 32, 98, 117, 102, 102, 101, 114 });
+    }
+
+    protected ByteBuffer getHeadlessByteBuffer() {
+        return getByteBuffer();
+    }
+
+    ByteBuffer getExceptionByteArray() throws UnsupportedEncodingException {
+        // "runtime"
+        return ByteBuffer
+                .wrap(new byte[] { 114, 117, 110, 116, 105, 109, 101 });
+    }
+
+    ByteBuffer getUnmappedByteBuffer() throws UnsupportedEncodingException {
+        // "unmap buffer"
+        byte[] ba = new byte[] { 117, 110, 109, 97, 112, 32, 98, 117, 102, 102,
+                101, 114 };
+        return ByteBuffer.wrap(ba);
+    }
+
+    ByteBuffer getMalformedByteBuffer() throws UnsupportedEncodingException {
+        // "malform buffer"
+        byte[] ba = new byte[] { 109, 97, 108, 102, 111, 114, 109, 32, 98, 117,
+                102, 102, 101, 114 };
+        return ByteBuffer.wrap(ba);
+    }
+
+    private void assertCharBufferValue(String expected, CharBuffer out) {
+        if (out.position() != 0) {
+            out.flip();
+        }
+        assertEquals(expected, new String(out.array(), out.arrayOffset(), out
+                .arrayOffset() + out.limit()));
+    }
+
+    /*
+     * test flush
+     */
+    public void testFlush() throws CharacterCodingException {
+        CharBuffer out = CharBuffer.allocate(10);
+        ByteBuffer in = ByteBuffer.wrap(new byte[] { 12, 12 });
+        decoder.decode(in, out, true);
+        assertSame(CoderResult.UNDERFLOW, decoder.flush(out));
+
+        decoder.reset();
+        decoder.decode((ByteBuffer) in.rewind(), (CharBuffer) out.rewind(),
+                true);
+        assertSame(CoderResult.UNDERFLOW, decoder
+                .flush(CharBuffer.allocate(10)));
+    }
+
+    /*
+     * ---------------------------------- methods to test illegal state
+     * -----------------------------------
+     */
+    // Normal case: just after reset, and it also means reset can be done
+    // anywhere
+    public void testResetIllegalState() throws CharacterCodingException {
+        decoder.reset();
+        decoder.decode(getByteBuffer());
+        decoder.reset();
+        decoder.decode(getByteBuffer(), CharBuffer.allocate(3), false);
+        decoder.reset();
+        decoder.decode(getByteBuffer(), CharBuffer.allocate(3), true);
+        decoder.reset();
+    }
+
+    public void testFlushIllegalState() throws CharacterCodingException {
+        ByteBuffer in = ByteBuffer.wrap(new byte[] { 98, 98 });
+        CharBuffer out = CharBuffer.allocate(5);
+        // Normal case: after decode with endOfInput is true
+        decoder.reset();
+        decoder.decode(in, out, true);
+        out.rewind();
+        CoderResult result = decoder.flush(out);
+        assertSame(result, CoderResult.UNDERFLOW);
+
+        // Illegal state: flush twice
+        try {
+            decoder.flush(out);
+            fail("should throw IllegalStateException");
+        } catch (IllegalStateException e) {
+        }
+
+        // Illegal state: flush after decode with endOfInput is false
+        decoder.reset();
+        decoder.decode(in, out, false);
+        try {
+            decoder.flush(out);
+            fail("should throw IllegalStateException");
+        } catch (IllegalStateException e) {
+        }
+    }
+    
+    // test illegal states for decode facade
+    public void testDecodeFacadeIllegalState() throws CharacterCodingException {
+        // decode facade can be execute in anywhere
+        ByteBuffer in = getByteBuffer();
+
+        // Normal case: just created
+        decoder.decode(in);
+        in.rewind();
+
+        // Normal case: just after decode facade
+        decoder.decode(in);
+        in.rewind();
+
+        // Normal case: just after decode with that endOfInput is true
+        decoder.reset();
+        decoder.decode(getByteBuffer(), CharBuffer.allocate(30), true);
+        decoder.decode(in);
+        in.rewind();
+
+        // Normal case:just after decode with that endOfInput is false
+        decoder.reset();
+        decoder.decode(getByteBuffer(), CharBuffer.allocate(30), false);
+        decoder.decode(in);
+        in.rewind();
+
+        // Normal case: just after flush
+        decoder.reset();
+        decoder.decode(getByteBuffer(), CharBuffer.allocate(30), true);
+        decoder.flush(CharBuffer.allocate(10));
+        decoder.decode(in);
+        in.rewind();
+    }
+
+    // test illegal states for two decode method with endOfInput is true
+    public void testDecodeTrueIllegalState() throws CharacterCodingException {
+        ByteBuffer in = ByteBuffer.wrap(new byte[] { 98, 98 });
+        CharBuffer out = CharBuffer.allocate(100);
+        // Normal case: just created
+        decoder.decode(in, out, true);
+        in.rewind();
+        out.rewind();
+
+        // Normal case: just after decode with that endOfInput is true
+        decoder.reset();
+        decoder.decode(in, CharBuffer.allocate(30), true);
+        in.rewind();
+        decoder.decode(in, out, true);
+        in.rewind();
+        out.rewind();
+
+        // Normal case:just after decode with that endOfInput is false
+        decoder.reset();
+        decoder.decode(in, CharBuffer.allocate(30), false);
+        in.rewind();
+        decoder.decode(in, out, true);
+        in.rewind();
+        out.rewind();
+
+        // Illegal state: just after flush
+        decoder.reset();
+        decoder.decode(in, CharBuffer.allocate(30), true);
+        decoder.flush(CharBuffer.allocate(10));
+        in.rewind();
+        try {
+            decoder.decode(in, out, true);
+            fail("should illegal state");
+        } catch (IllegalStateException e) {
+        }
+        in.rewind();
+        out.rewind();
+
+    }
+
+    // test illegal states for two decode method with endOfInput is false
+    public void testDecodeFalseIllegalState() throws CharacterCodingException {
+        ByteBuffer in = ByteBuffer.wrap(new byte[] { 98, 98 });
+        CharBuffer out = CharBuffer.allocate(5);
+        // Normal case: just created
+        decoder.decode(in, out, false);
+        in.rewind();
+        out.rewind();
+
+        // Illegal state: just after decode facade
+        decoder.reset();
+        decoder.decode(in);
+        in.rewind();
+        try {
+            decoder.decode(in, out, false);
+            fail("should illegal state");
+        } catch (IllegalStateException e) {
+        }
+        in.rewind();
+        out.rewind();
+
+        // Illegal state: just after decode with that endOfInput is true
+        decoder.reset();
+        decoder.decode(in, CharBuffer.allocate(30), true);
+        in.rewind();
+        try {
+            decoder.decode(in, out, false);
+            fail("should illegal state");
+        } catch (IllegalStateException e) {
+        }
+        in.rewind();
+        out.rewind();
+
+        // Normal case:just after decode with that endOfInput is false
+        decoder.reset();
+        decoder.decode(in, CharBuffer.allocate(30), false);
+        in.rewind();
+        decoder.decode(in, out, false);
+        in.rewind();
+        out.rewind();
+
+        // Illegal state: just after flush
+        decoder.reset();
+        decoder.decode(in, CharBuffer.allocate(30), true);
+        in.rewind();
+        decoder.flush(CharBuffer.allocate(10));
+        try {
+            decoder.decode(in, out, false);
+            fail("should illegal state");
+        } catch (IllegalStateException e) {
+        }
+    }
+
+    /*
+     * --------------------------------- illegal state test end
+     * ---------------------------------
+     */
+
+    public void testImplFlush() {
+        decoder = new MockCharsetDecoder(cs, 1, 3);
+        assertEquals(CoderResult.UNDERFLOW, ((MockCharsetDecoder) decoder)
+                .pubImplFlush(null));
+    }
+
+    public void testImplOnMalformedInput() {
+        decoder = new MockCharsetDecoder(cs, 1, 3);
+        assertEquals(CoderResult.UNDERFLOW, ((MockCharsetDecoder) decoder)
+                .pubImplFlush(null));
+
+    }
+
+    public void testImplOnUnmappableCharacter() {
+        decoder = new MockCharsetDecoder(cs, 1, 3);
+        ((MockCharsetDecoder) decoder).pubImplOnUnmappableCharacter(null);
+    }
+
+    public void testImplReplaceWith() {
+        decoder = new MockCharsetDecoder(cs, 1, 3);
+        ((MockCharsetDecoder) decoder).pubImplReplaceWith(null);
+    }
+
+    public void testImplReset() {
+        decoder = new MockCharsetDecoder(cs, 1, 3);
+        ((MockCharsetDecoder) decoder).pubImplReset();
+    }
+
+    /*
+     * mock decoder
+     */
+    public static class MockCharsetDecoder extends CharsetDecoder {
+        public MockCharsetDecoder(Charset cs, float ave, float max) {
+            super(cs, ave, max);
+        }
+
+        protected CoderResult decodeLoop(ByteBuffer in, CharBuffer out) {
+            int inPosition = in.position();
+            byte[] input = new byte[in.remaining()];
+            in.get(input);
+            String result = new String(input);
+            if (result.startsWith("malform")) {
+                // reset the cursor to the error position
+                in.position(inPosition);
+                // set the error length
+                return CoderResult.malformedForLength("malform".length());
+            } else if (result.startsWith("unmap")) {
+                // reset the cursor to the error position
+                in.position(inPosition);
+                // set the error length
+                return CoderResult.unmappableForLength("unmap".length());
+            } else if (result.startsWith("runtime")) {
+                // reset the cursor to the error position
+                in.position(0);
+                // set the error length
+                throw new RuntimeException("runtime");
+            }
+            int inLeft = input.length;
+            int outLeft = out.remaining();
+            CoderResult r = CoderResult.UNDERFLOW;
+            int length = inLeft;
+            if (outLeft < inLeft) {
+                r = CoderResult.OVERFLOW;
+                length = outLeft;
+                in.position(inPosition + outLeft);
+            }
+            for (int i = 0; i < length; i++) {
+                out.put((char) input[i]);
+            }
+            return r;
+        }
+
+        protected CoderResult implFlush(CharBuffer out) {
+            CoderResult result = super.implFlush(out);
+            if (out.remaining() >= 5) {
+                // TODO
+                // out.put("flush");
+                result = CoderResult.UNDERFLOW;
+            } else {
+                // out.put("flush", 0, out.remaining());
+                result = CoderResult.OVERFLOW;
+            }
+            return result;
+        }
+
+        public CoderResult pubImplFlush(CharBuffer out) {
+            return super.implFlush(out);
+        }
+
+        public void pubImplOnMalformedInput(CodingErrorAction newAction) {
+            super.implOnMalformedInput(newAction);
+        }
+
+        public void pubImplOnUnmappableCharacter(CodingErrorAction newAction) {
+            super.implOnUnmappableCharacter(newAction);
+        }
+
+        public void pubImplReplaceWith(String newReplacement) {
+            super.implReplaceWith(newReplacement);
+        }
+
+        public void pubImplReset() {
+            super.implReset();
+        }
 
-	protected static final double AVER_BYTES = 0.5;
-
-	// default charset
-	private static final Charset MOCKCS = new CharsetEncoderTest.MockCharset(
-			"mock", new String[0]);
-
-	Charset cs = MOCKCS;
-
-	// default decoder
-	protected static CharsetDecoder decoder;
-
-	String bom = "";
-
-	protected void setUp() throws Exception {
-		super.setUp();
-		decoder = cs.newDecoder();
-	}
-
-	protected void tearDown() throws Exception {
-		super.tearDown();
-	}
-
-	// FIXME: give up this tests
-	// /*
-	// * test default value
-	// */
-	// public void testDefaultCharsPerByte() {
-	// assertTrue(decoder.averageCharsPerByte() == AVER_BYTES);
-	// assertTrue(decoder.maxCharsPerByte() == MAX_BYTES);
-	// }
-
-	public void testDefaultValues() {
-		assertSame(cs, decoder.charset());
-		try {
-			decoder.detectedCharset();
-			fail("should unsupported");
-		} catch (UnsupportedOperationException e) {
-		}
-		try {
-			assertTrue(decoder.isCharsetDetected());
-			fail("should unsupported");
-		} catch (UnsupportedOperationException e) {
-		}
-		assertFalse(decoder.isAutoDetecting());
-		assertSame(CodingErrorAction.REPORT, decoder.malformedInputAction());
-		assertSame(CodingErrorAction.REPORT, decoder
-				.unmappableCharacterAction());
-		assertEquals(decoder.replacement(), "\ufffd");
-	}
-
-	/*
-	 * test constructor
-	 */
-	public void testCharsetDecoder() {
-		// default value
-		decoder = new MockCharsetDecoder(cs, (float) AVER_BYTES, MAX_BYTES);
-
-		// normal case
-		CharsetDecoder ec = new MockCharsetDecoder(cs, 1, MAX_BYTES);
-		assertSame(ec.charset(), cs);
-		assertEquals(1.0, ec.averageCharsPerByte(), 0.0);
-		assertTrue(ec.maxCharsPerByte() == MAX_BYTES);
-
-		/*
-		 * ------------------------ Exceptional cases -------------------------
-		 */
-		// Normal case: null charset
-		ec = new MockCharsetDecoder(null, 1, MAX_BYTES);
-		assertNull(ec.charset());
-		assertEquals(1.0, ec.averageCharsPerByte(), 0.0);
-		assertTrue(ec.maxCharsPerByte() == MAX_BYTES);
-
-		ec = new MockCharsetDecoder(new CharsetEncoderTest.MockCharset("mock",
-				new String[0]), 1, MAX_BYTES);
-
-                // Commented out since the comment is wrong since MAX_BYTES > 1
-		// // OK: average length less than max length
-		// ec = new MockCharsetDecoder(cs, MAX_BYTES, 1);
-		// assertTrue(ec.averageCharsPerByte() == MAX_BYTES);
-		// assertTrue(ec.maxCharsPerByte() == 1);
-
-		// Illegal Argument: zero length
-		try {
-			ec = new MockCharsetDecoder(cs, 0, MAX_BYTES);
-			fail("should throw IllegalArgumentException");
-		} catch (IllegalArgumentException e) {
-		}
-		try {
-			ec = new MockCharsetDecoder(cs, 1, 0);
-			fail("should throw IllegalArgumentException");
-		} catch (IllegalArgumentException e) {
-		}
-
-		// Illegal Argument: negative length
-		try {
-			ec = new MockCharsetDecoder(cs, -1, MAX_BYTES);
-			fail("should throw IllegalArgumentException");
-		} catch (IllegalArgumentException e) {
-		}
-		try {
-			ec = new MockCharsetDecoder(cs, 1, -1);
-			fail("should throw IllegalArgumentException");
-		} catch (IllegalArgumentException e) {
-		}
-	}
-
-	/*
-	 * test onMalformedInput
-	 */
-	public void testOnMalformedInput() {
-		assertSame(CodingErrorAction.REPORT, decoder.malformedInputAction());
-		try {
-			decoder.onMalformedInput(null);
-			fail("should throw null pointer exception");
-		} catch (IllegalArgumentException e) {
-		}
-		decoder.onMalformedInput(CodingErrorAction.IGNORE);
-		assertSame(CodingErrorAction.IGNORE, decoder.malformedInputAction());
-	}
-
-	/*
-	 * test unmappableCharacter
-	 */
-	public void testOnUnmappableCharacter() {
-		assertSame(CodingErrorAction.REPORT, decoder
-				.unmappableCharacterAction());
-		try {
-			decoder.onUnmappableCharacter(null);
-			fail("should throw null pointer exception");
-		} catch (IllegalArgumentException e) {
-		}
-		decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
-		assertSame(CodingErrorAction.IGNORE, decoder
-				.unmappableCharacterAction());
-	}
-
-	/*
-	 * test replaceWith
-	 */
-	public void testReplaceWith() {
-		try {
-			decoder.replaceWith(null);
-			fail("should throw null pointer exception");
-		} catch (IllegalArgumentException e) {
-		}
-		try {
-			decoder.replaceWith("");
-			fail("should throw null pointer exception");
-		} catch (IllegalArgumentException e) {
-		}
-		try {
-			decoder.replaceWith("testReplaceWith");
-			fail("should throw illegal argument exception");
-		} catch (IllegalArgumentException e) {
-		}
-
-		decoder.replaceWith("a");
-		assertSame("a", decoder.replacement());
-	}
-
-	/*
-	 * Class under test for CharBuffer decode(ByteBuffer)
-	 */
-	public void testDecodeByteBuffer() throws CharacterCodingException {
-		implTestDecodeByteBuffer();
-	}
-
-	void implTestDecodeByteBuffer() throws CharacterCodingException {
-		// Null pointer
-		try {
-			decoder.decode(null);
-			fail("should throw null pointer exception");
-		} catch (NullPointerException e) {
-		}
-
-		// empty input buffer
-		CharBuffer out = decoder.decode(ByteBuffer.allocate(0));
-		assertCharBufferValue(out, "");
-
-		// normal case
-		ByteBuffer in = ByteBuffer.wrap(getUnibytes());
-		out = decoder.decode(in);
-		assertEquals(out.position(), 0);
-		assertEquals(out.limit(), unistr.length());
-		assertEquals(out.remaining(), unistr.length());
-		assertEquals(new String(out.array(), 0, out.limit()), unistr);
-	}
-
-	public void testDecodeByteBufferException()
-			throws CharacterCodingException, UnsupportedEncodingException {
-		CharBuffer out;
-		ByteBuffer in;
-		String replaceStr = decoder.replacement() + " buffer";
-
-		// MalformedException:
-		decoder.onMalformedInput(CodingErrorAction.REPORT);
-		decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
-		in = getMalformByteBuffer();
-		if (in != null) {
-			try {
-				CharBuffer buffer = decoder.decode(in);
-				assertTrue(buffer.remaining() > 0);
-				fail("should throw MalformedInputException");
-			} catch (MalformedInputException e) {
-			}
-
-			decoder.reset();
-			in.rewind();
-			decoder.onMalformedInput(CodingErrorAction.IGNORE);
-			out = decoder.decode(in);
-			assertCharBufferValue(out, " buffer");
-
-			decoder.reset();
-			in.rewind();
-			decoder.onMalformedInput(CodingErrorAction.REPLACE);
-			out = decoder.decode(in);
-			assertCharBufferValue(out, replaceStr);
-		}
-
-		// Unmapped Exception:
-		decoder.onMalformedInput(CodingErrorAction.REPORT);
-		decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
-		in = getUnmappedByteBuffer();
-		if (in != null) {
-			try {
-				decoder.decode(in);
-				fail("should throw UnmappableCharacterException");
-			} catch (UnmappableCharacterException e) {
-			}
-
-			decoder.reset();
-			in.rewind();
-			decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
-			out = decoder.decode(in);
-			assertCharBufferValue(out, " buffer");
-
-			decoder.reset();
-			in.rewind();
-			decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
-			out = decoder.decode(in);
-			assertCharBufferValue(out, replaceStr);
-		}
-
-		// RuntimeException
-		try {
-			decoder.decode(getExceptionByteArray());
-			fail("should throw runtime exception");
-		} catch (RuntimeException e) {
-		}
-	}
-
-	/*
-	 * Class under test for CoderResult decode(ByteBuffer, CharBuffer, boolean)
-	 */
-	public void testDecodeByteBufferCharBufferboolean() {
-		implTestDecodeByteBufferCharBufferboolean();
-	}
-
-	void implTestDecodeByteBufferCharBufferboolean() {
-		byte[] gb = getUnibytes();
-		ByteBuffer in = ByteBuffer.wrap(gb);
-		CharBuffer out = CharBuffer.allocate(100);
-
-		// Null pointer
-		try {
-			decoder.decode(null, out, true);
-			fail("should throw null pointer exception");
-		} catch (NullPointerException e) {
-		}
-		try {
-			decoder.decode(in, null, true);
-			fail("should throw null pointer exception");
-		} catch (NullPointerException e) {
-		}
-
-		// normal case, one complete operation
-		decoder.reset();
-		in.rewind();
-		out.rewind();
-		assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, true));
-		assertEquals(out.limit(), 100);
-		assertEquals(out.position(), unistr.length());
-		assertEquals(out.remaining(), 100 - unistr.length());
-		assertEquals(out.capacity(), 100);
-		assertCharBufferValue(out, unistr);
-		decoder.flush(out);
-
-		// normal case, one complete operation, but call twice, first time set
-		// endOfInput to false
-		decoder.reset();
-		in.rewind();
-		out.clear();
-		assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, false));
-		assertEquals(out.limit(), 100);
-		assertEquals(out.position(), unistr.length());
-		assertEquals(out.remaining(), 100 - unistr.length());
-		assertEquals(out.capacity(), 100);
-		assertCharBufferValue(out, unistr);
-
-		decoder.reset();
-		in.rewind();
-		out.clear();
-		assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, false));
-		in = ByteBuffer.wrap(unibytes);
-		assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, false));
-		in.rewind();
-		assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, true));
-		assertEquals(out.limit(), 100);
-		assertTrue(out.position() > 0);
-		assertEquals(out.remaining(), out.capacity() - out.position());
-		assertEquals(out.capacity(), 100);
-		assertCharBufferValue(out, unistr + unistr + unistr);
-
-		// overflow
-		out = CharBuffer.allocate(4);
-		decoder.reset();
-		in = ByteBuffer.wrap(getUnibytes());
-		out.rewind();
-		assertSame(CoderResult.OVERFLOW, decoder.decode(in, out, false));
-
-		assertEquals(new String(out.array()), unistr.substring(0, 4));
-		out = CharBuffer.allocate(100);
-		assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, false));
-		assertCharBufferValue(out, unistr.substring(4));
-		in.rewind();
-		out = CharBuffer.allocate(100);
-		assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out, true));
-		assertCharBufferValue(out, bom + unistr);
-	}
-
-	public void testDecodeCharBufferByteBufferbooleanExceptionTrue()
-			throws CharacterCodingException, UnsupportedEncodingException {
-		implTestDecodeCharBufferByteBufferbooleanException(true);
-	}
-
-	public void testDecodeCharBufferByteBufferbooleanExceptionFalse()
-			throws CharacterCodingException, UnsupportedEncodingException {
-		implTestDecodeCharBufferByteBufferbooleanException(false);
-	}
-
-	void implTestDecodeCharBufferByteBufferbooleanException(boolean endOfInput)
-			throws CharacterCodingException, UnsupportedEncodingException {
-		CharBuffer out;
-		ByteBuffer in;
-
-		// Unmapped Exception:
-		in = getUnmappedByteBuffer();
-		out = CharBuffer.allocate(50);
-		decoder.onMalformedInput(CodingErrorAction.REPORT);
-		if (null != in) {
-			decoder.reset();
-			decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
-			CoderResult result = decoder.decode(in, out, endOfInput);
-			assertTrue(result.isUnmappable());
-
-			decoder.reset();
-			out.clear();
-			in.rewind();
-			decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
-			assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out,
-					endOfInput));
-			assertCharBufferValue(out, " buffer");
-
-			decoder.reset();
-			out.clear();
-			in.rewind();
-			decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
-			assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out,
-					endOfInput));
-			assertCharBufferValue(out, decoder.replacement() + " buffer");
-		} else if (endOfInput) {
-			System.err.println("Cannot find unmappable byte array for "
-					+ cs.name());
-		}
-
-		// MalformedException:
-		in = getMalformByteBuffer();
-		out = CharBuffer.allocate(50);
-		decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
-		if (null != in) {
-			decoder.onMalformedInput(CodingErrorAction.REPORT);
-			CoderResult result = decoder.decode(in, out, endOfInput);
-			assertTrue(result.isMalformed());
-
-			decoder.reset();
-			out.clear();
-			in.rewind();
-			decoder.onMalformedInput(CodingErrorAction.IGNORE);
-			assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out,
-					endOfInput));
-			assertCharBufferValue(out, " buffer");
-
-			decoder.reset();
-			out.clear();
-			in.rewind();
-			decoder.onMalformedInput(CodingErrorAction.REPLACE);
-			assertSame(CoderResult.UNDERFLOW, decoder.decode(in, out,
-					endOfInput));
-			assertCharBufferValue(out, decoder.replacement() + " buffer");
-		} else if (endOfInput) {
-			System.err.println("Cannot find malform byte array for "
-					+ cs.name());
-		}
-
-		// RuntimeException
-		in = getExceptionByteArray();
-		try {
-			decoder.decode(in, out, endOfInput);
-			fail("should throw runtime exception");
-		} catch (RuntimeException e) {
-		}
-	}
-
-	ByteBuffer getExceptionByteArray() throws UnsupportedEncodingException {
-		// "runtime"
-		return ByteBuffer
-				.wrap(new byte[] { 114, 117, 110, 116, 105, 109, 101 });
-	}
-
-	ByteBuffer getUnmappedByteBuffer() throws UnsupportedEncodingException {
-		// "unmap buffer"
-		byte[] ba = new byte[] { 117, 110, 109, 97, 112, 32, 98, 117, 102, 102,
-				101, 114 };
-		return ByteBuffer.wrap(ba);
-	}
-
-	ByteBuffer getMalformByteBuffer() throws UnsupportedEncodingException {
-		// "malform buffer"
-		byte[] ba = new byte[] { 109, 97, 108, 102, 111, 114, 109, 32, 98, 117,
-				102, 102, 101, 114 };
-		return ByteBuffer.wrap(ba);
-	}
-
-	void assertCharBufferValue(CharBuffer out, String expected) {
-		if (out.position() != 0) {
-			out.flip();
-		}
-		assertEquals(new String(out.array(), 0, out.limit()), expected);
-	}
-
-	/*
-	 * test flush
-	 */
-	public void testFlush() throws CharacterCodingException {
-		CharBuffer out = CharBuffer.allocate(10);
-		ByteBuffer in = ByteBuffer.wrap(new byte[] { 12, 12 });
-		decoder.decode(in, out, true);
-		assertSame(CoderResult.UNDERFLOW, decoder.flush(out));
-
-		decoder.reset();
-		decoder.decode((ByteBuffer) in.rewind(), (CharBuffer) out.rewind(),
-				true);
-		assertSame(CoderResult.UNDERFLOW, decoder
-				.flush(CharBuffer.allocate(10)));
-	}
-
-	/*
-	 * ---------------------------------- methods to test illegal state
-	 * -----------------------------------
-	 */
-	// Normal case: just after reset, and it also means reset can be done
-	// anywhere
-	public void testResetIllegalState() throws CharacterCodingException {
-		byte[] gb = getUnibytes();
-		decoder.reset();
-		decoder.decode(ByteBuffer.wrap(gb));
-		decoder.reset();
-		decoder.decode(ByteBuffer.wrap(gb), CharBuffer.allocate(3), false);
-		decoder.reset();
-		decoder.decode(ByteBuffer.wrap(gb), CharBuffer.allocate(3), true);
-		decoder.reset();
-	}
-
-	public void testFlushIllegalState() throws CharacterCodingException {
-		ByteBuffer in = ByteBuffer.wrap(new byte[] { 98, 98 });
-		CharBuffer out = CharBuffer.allocate(5);
-		// Normal case: after decode with endOfInput is true
-		decoder.reset();
-		decoder.decode(in, out, true);
-		out.rewind();
-		CoderResult result = decoder.flush(out);
-		assertSame(result, CoderResult.UNDERFLOW);
-
-		// Illegal state: flush twice
-		try {
-			decoder.flush(out);
-			fail("should throw IllegalStateException");
-		} catch (IllegalStateException e) {
-		}
-
-		// Illegal state: flush after decode with endOfInput is false
-		decoder.reset();
-		decoder.decode(in, out, false);
-		try {
-			decoder.flush(out);
-			fail("should throw IllegalStateException");
-		} catch (IllegalStateException e) {
-		}
-	}
-
-	byte[] getUnibytes() {
-		return unibytes;
-	}
-
-	// test illegal states for decode facade
-	public void testDecodeFacadeIllegalState() throws CharacterCodingException {
-		// decode facade can be execute in anywhere
-		byte[] gb = getUnibytes();
-		ByteBuffer in = ByteBuffer.wrap(gb);
-		// Normal case: just created
-		decoder.decode(in);
-		in.rewind();
-
-		// Normal case: just after decode facade
-		decoder.decode(in);
-		in.rewind();
-
-		// Normal case: just after decode with that endOfInput is true
-		decoder.reset();
-		decoder.decode(ByteBuffer.wrap(gb), CharBuffer.allocate(30), true);
-		decoder.decode(in);
-		in.rewind();
-
-		// Normal case:just after decode with that endOfInput is false
-		decoder.reset();
-		decoder.decode(ByteBuffer.wrap(gb), CharBuffer.allocate(30), false);
-		decoder.decode(in);
-		in.rewind();
-
-		// Normal case: just after flush
-		decoder.reset();
-		decoder.decode(ByteBuffer.wrap(gb), CharBuffer.allocate(30), true);
-		decoder.flush(CharBuffer.allocate(10));
-		decoder.decode(in);
-		in.rewind();
-	}
-
-	// test illegal states for two decode method with endOfInput is true
-	public void testDecodeTrueIllegalState() throws CharacterCodingException {
-		ByteBuffer in = ByteBuffer.wrap(new byte[] { 98, 98 });
-		CharBuffer out = CharBuffer.allocate(100);
-		// Normal case: just created
-		decoder.decode(in, out, true);
-		in.rewind();
-		out.rewind();
-
-		// Normal case: just after decode with that endOfInput is true
-		decoder.reset();
-		decoder.decode(in, CharBuffer.allocate(30), true);
-		in.rewind();
-		decoder.decode(in, out, true);
-		in.rewind();
-		out.rewind();
-
-		// Normal case:just after decode with that endOfInput is false
-		decoder.reset();
-		decoder.decode(in, CharBuffer.allocate(30), false);
-		in.rewind();
-		decoder.decode(in, out, true);
-		in.rewind();
-		out.rewind();
-
-		// Illegal state: just after flush
-		decoder.reset();
-		decoder.decode(in, CharBuffer.allocate(30), true);
-		decoder.flush(CharBuffer.allocate(10));
-		in.rewind();
-		try {
-			decoder.decode(in, out, true);
-			fail("should illegal state");
-		} catch (IllegalStateException e) {
-		}
-		in.rewind();
-		out.rewind();
-
-	}
-
-	// test illegal states for two decode method with endOfInput is false
-	public void testDecodeFalseIllegalState() throws CharacterCodingException {
-		ByteBuffer in = ByteBuffer.wrap(new byte[] { 98, 98 });
-		CharBuffer out = CharBuffer.allocate(5);
-		// Normal case: just created
-		decoder.decode(in, out, false);
-		in.rewind();
-		out.rewind();
-
-		// Illegal state: just after decode facade
-		decoder.reset();
-		decoder.decode(in);
-		in.rewind();
-		try {
-			decoder.decode(in, out, false);
-			fail("should illegal state");
-		} catch (IllegalStateException e) {
-		}
-		in.rewind();
-		out.rewind();
-
-		// Illegal state: just after decode with that endOfInput is true
-		decoder.reset();
-		decoder.decode(in, CharBuffer.allocate(30), true);
-		in.rewind();
-		try {
-			decoder.decode(in, out, false);
-			fail("should illegal state");
-		} catch (IllegalStateException e) {
-		}
-		in.rewind();
-		out.rewind();
-
-		// Normal case:just after decode with that endOfInput is false
-		decoder.reset();
-		decoder.decode(in, CharBuffer.allocate(30), false);
-		in.rewind();
-		decoder.decode(in, out, false);
-		in.rewind();
-		out.rewind();
-
-		// Illegal state: just after flush
-		decoder.reset();
-		decoder.decode(in, CharBuffer.allocate(30), true);
-		in.rewind();
-		decoder.flush(CharBuffer.allocate(10));
-		try {
-			decoder.decode(in, out, false);
-			fail("should illegal state");
-		} catch (IllegalStateException e) {
-		}
-	}
-
-	/*
-	 * --------------------------------- illegal state test end
-	 * ---------------------------------
-	 */
-
-	public void testImplFlush() {
-		decoder = new MockCharsetDecoder(cs, 1, 3);
-		assertEquals(CoderResult.UNDERFLOW, ((MockCharsetDecoder) decoder)
-				.pubImplFlush(null));
-	}
-
-	public void testImplOnMalformedInput() {
-		decoder = new MockCharsetDecoder(cs, 1, 3);
-		assertEquals(CoderResult.UNDERFLOW, ((MockCharsetDecoder) decoder)
-				.pubImplFlush(null));
-
-	}
-
-	public void testImplOnUnmappableCharacter() {
-		decoder = new MockCharsetDecoder(cs, 1, 3);
-		((MockCharsetDecoder) decoder).pubImplOnUnmappableCharacter(null);
-	}
-
-	public void testImplReplaceWith() {
-		decoder = new MockCharsetDecoder(cs, 1, 3);
-		((MockCharsetDecoder) decoder).pubImplReplaceWith(null);
-	}
-
-	public void testImplReset() {
-		decoder = new MockCharsetDecoder(cs, 1, 3);
-		((MockCharsetDecoder) decoder).pubImplReset();
-	}
-
-	/*
-	 * mock decoder
-	 */
-	public static class MockCharsetDecoder extends CharsetDecoder {
-		public MockCharsetDecoder(Charset cs, float ave, float max) {
-			super(cs, ave, max);
-		}
-
-		protected CoderResult decodeLoop(ByteBuffer in, CharBuffer out) {
-			int inPosition = in.position();
-			byte[] input = new byte[in.remaining()];
-			in.get(input);
-			String result = new String(input);
-			if (result.startsWith("malform")) {
-				// reset the cursor to the error position
-				in.position(inPosition);
-				// set the error length
-				return CoderResult.malformedForLength("malform".length());
-			} else if (result.startsWith("unmap")) {
-				// reset the cursor to the error position
-				in.position(inPosition);
-				// set the error length
-				return CoderResult.unmappableForLength("unmap".length());
-			} else if (result.startsWith("runtime")) {
-				// reset the cursor to the error position
-				in.position(0);
-				// set the error length
-				throw new RuntimeException("runtime");
-			}
-			int inLeft = input.length;
-			int outLeft = out.remaining();
-			CoderResult r = CoderResult.UNDERFLOW;
-			int length = inLeft;
-			if (outLeft < inLeft) {
-				r = CoderResult.OVERFLOW;
-				length = outLeft;
-				in.position(inPosition + outLeft);
-			}
-			for (int i = 0; i < length; i++) {
-				out.put((char) input[i]);
-			}
-			return r;
-		}
-
-		protected CoderResult implFlush(CharBuffer out) {
-			CoderResult result = super.implFlush(out);
-			if (out.remaining() >= 5) {
-				// TODO
-				// out.put("flush");
-				result = CoderResult.UNDERFLOW;
-			} else {
-				// out.put("flush", 0, out.remaining());
-				result = CoderResult.OVERFLOW;
-			}
-			return result;
-		}
-
-		public CoderResult pubImplFlush(CharBuffer out) {
-			return super.implFlush(out);
-		}
-
-		public void pubImplOnMalformedInput(CodingErrorAction newAction) {
-			super.implOnMalformedInput(newAction);
-		}
-
-		public void pubImplOnUnmappableCharacter(CodingErrorAction newAction) {
-			super.implOnUnmappableCharacter(newAction);
-		}
-
-		public void pubImplReplaceWith(String newReplacement) {
-			super.implReplaceWith(newReplacement);
-		}
-
-		public void pubImplReset() {
-			super.implReset();
-		}
-
-	}
+    }
 
 }

Modified: harmony/enhanced/classlib/branches/java6/modules/nio_char/src/test/java/tests/api/java/nio/charset/GBCharsetDecoderTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/nio_char/src/test/java/tests/api/java/nio/charset/GBCharsetDecoderTest.java?rev=646264&r1=646263&r2=646264&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/nio_char/src/test/java/tests/api/java/nio/charset/GBCharsetDecoderTest.java (original)
+++ harmony/enhanced/classlib/branches/java6/modules/nio_char/src/test/java/tests/api/java/nio/charset/GBCharsetDecoderTest.java Wed Apr  9 04:01:14 2008
@@ -49,10 +49,10 @@
 		return null;
 	}
 
-	ByteBuffer getMalformByteBuffer() throws UnsupportedEncodingException {
+	ByteBuffer getMalformedByteBuffer() throws UnsupportedEncodingException {
 		ByteBuffer buffer = ByteBuffer.allocate(20);
 		buffer.put(new byte[] { (byte) 0xd8 });
-		buffer.put(unibytes);
+		buffer.put(getByteBuffer());
 		buffer.flip();
 		return buffer;
 	}

Modified: harmony/enhanced/classlib/branches/java6/modules/nio_char/src/test/java/tests/api/java/nio/charset/GBCharsetEncoderTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/nio_char/src/test/java/tests/api/java/nio/charset/GBCharsetEncoderTest.java?rev=646264&r1=646263&r2=646264&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/nio_char/src/test/java/tests/api/java/nio/charset/GBCharsetEncoderTest.java (original)
+++ harmony/enhanced/classlib/branches/java6/modules/nio_char/src/test/java/tests/api/java/nio/charset/GBCharsetEncoderTest.java Wed Apr  9 04:01:14 2008
@@ -43,7 +43,7 @@
 		super.tearDown();
 	}
 
-	public void testCanEncodechar() throws CharacterCodingException {
+	public void testCanEncodechar() {
 		// normal case for utfCS
 		assertTrue(encoder.canEncode('\u0077'));
 		assertTrue(encoder.canEncode('\uc2a3'));

Modified: harmony/enhanced/classlib/branches/java6/modules/nio_char/src/test/java/tests/api/java/nio/charset/ISOCharsetDecoderTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/nio_char/src/test/java/tests/api/java/nio/charset/ISOCharsetDecoderTest.java?rev=646264&r1=646263&r2=646264&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/nio_char/src/test/java/tests/api/java/nio/charset/ISOCharsetDecoderTest.java (original)
+++ harmony/enhanced/classlib/branches/java6/modules/nio_char/src/test/java/tests/api/java/nio/charset/ISOCharsetDecoderTest.java Wed Apr  9 04:01:14 2008
@@ -49,7 +49,7 @@
 
 	}
 
-	ByteBuffer getMalformByteBuffer() throws UnsupportedEncodingException {
+	ByteBuffer getMalformedByteBuffer() throws UnsupportedEncodingException {
 		// TODO how malform
 		return null;
 	}

Modified: harmony/enhanced/classlib/branches/java6/modules/nio_char/src/test/java/tests/api/java/nio/charset/UTF16BECharsetDecoderTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/nio_char/src/test/java/tests/api/java/nio/charset/UTF16BECharsetDecoderTest.java?rev=646264&r1=646263&r2=646264&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/nio_char/src/test/java/tests/api/java/nio/charset/UTF16BECharsetDecoderTest.java (original)
+++ harmony/enhanced/classlib/branches/java6/modules/nio_char/src/test/java/tests/api/java/nio/charset/UTF16BECharsetDecoderTest.java Wed Apr  9 04:01:14 2008
@@ -25,50 +25,48 @@
  */
 public class UTF16BECharsetDecoderTest extends CharsetDecoderTest {
 
-	protected void setUp() throws Exception {
-		cs = Charset.forName("utf-16be");
-		unibytes = new byte[] { 0, 32, 0, 98, 0, 117, 0, 102, 0, 102, 0, 101,
-				0, 114 };
-		super.setUp();
-	}
-
-	/*
-	 * @see CharsetDecoderTest#tearDown()
-	 */
-	protected void tearDown() throws Exception {
-		super.tearDown();
-	}
-
-	// FIXME: give up this tests
-	// public void testDefaultCharsPerByte() {
-	// // assertEquals(1, decoder.averageCharsPerByte());
-	// // assertEquals(1, decoder.maxCharsPerByte());
-	// assertEquals(decoder.averageCharsPerByte(), 0.5, 0.001);
-	// assertEquals(decoder.maxCharsPerByte(), 2, 0.001);
-	// }
-
-	ByteBuffer getUnmappedByteBuffer() throws UnsupportedEncodingException {
-		// no unmap byte buffer
-		return null;
-	}
-
-	ByteBuffer getMalformByteBuffer() throws UnsupportedEncodingException {
-		// FIXME: different here, RI can parse 0xd8d8
-		// ByteBuffer buffer = ByteBuffer.allocate(100);
-		// buffer.put((byte)0xd8);
-		// buffer.put((byte)0xd8);
-		// buffer.put(unibytes);
-		// buffer.flip();
-		// return buffer;
-		return null;
-	}
-
-	ByteBuffer getExceptionByteArray() throws UnsupportedEncodingException {
-		return null;
-	}
-
-	byte[] getUnibytes() {
-		return new byte[] { 0, 32, 0, 98, 0, 117, 0, 102, 0, 102, 0, 101, 0,
-				114 };
-	}
+    protected void setUp() throws Exception {
+        cs = Charset.forName("utf-16be");
+        super.setUp();
+    }
+
+    /*
+     * @see CharsetDecoderTest#tearDown()
+     */
+    protected void tearDown() throws Exception {
+        super.tearDown();
+    }
+
+    // FIXME: give up this tests
+    // public void testDefaultCharsPerByte() {
+    // // assertEquals(1, decoder.averageCharsPerByte());
+    // // assertEquals(1, decoder.maxCharsPerByte());
+    // assertEquals(decoder.averageCharsPerByte(), 0.5, 0.001);
+    // assertEquals(decoder.maxCharsPerByte(), 2, 0.001);
+    // }
+
+    ByteBuffer getUnmappedByteBuffer() throws UnsupportedEncodingException {
+        // no unmap byte buffer
+        return null;
+    }
+
+    ByteBuffer getMalformedByteBuffer() throws UnsupportedEncodingException {
+        // FIXME: different here, RI can parse 0xd8d8
+        // ByteBuffer buffer = ByteBuffer.allocate(100);
+        // buffer.put((byte)0xd8);
+        // buffer.put((byte)0xd8);
+        // buffer.put(unibytes);
+        // buffer.flip();
+        // return buffer;
+        return null;
+    }
+
+    ByteBuffer getExceptionByteArray() throws UnsupportedEncodingException {
+        return null;
+    }
+
+    protected ByteBuffer getByteBuffer() {
+        return ByteBuffer.wrap(new byte[] { 0, 32, 0, 98, 0, 117, 0, 102, 0,
+                102, 0, 101, 0, 114 });
+    }
 }



Mime
View raw message