OSDN Git Service

Normalise whitespace in GNU Classpath.
[pf3gnuchains/gcc-fork.git] / libjava / classpath / gnu / java / nio / charset / ISO_8859_1.java
index cc06ecd..eb7c5ec 100644 (file)
@@ -1,4 +1,4 @@
-/* ISO_8859_1.java -- 
+/* ISO_8859_1.java --
    Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
@@ -48,6 +48,7 @@ import java.nio.charset.CoderResult;
  * ISO-8859-1 charset.
  *
  * @author Jesse Rosenstock
+ * @modified Ian Rogers
  */
 final class ISO_8859_1 extends Charset
 {
@@ -57,7 +58,7 @@ final class ISO_8859_1 extends Charset
      * http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html
      */
     super ("ISO-8859-1", new String[] {
-        /* These names are provided by 
+        /* These names are provided by
          * http://www.iana.org/assignments/character-sets
          */
         "iso-ir-100",
@@ -71,8 +72,12 @@ final class ISO_8859_1 extends Charset
         /* These names are provided by
          * http://oss.software.ibm.com/cgi-bin/icu/convexp?s=ALL
          */
-        "ISO8859_1", "ISO_8859_1", "ibm-819", "ISO_8859-1:1987",
-        "819"
+        "ISO8859_1",
+        "ISO_8859_1",
+        "ibm-819",
+        "ISO_8859-1:1987",
+        "819",
+        "ISO8859-1"
         });
 
   }
@@ -94,6 +99,19 @@ final class ISO_8859_1 extends Charset
 
   private static final class Decoder extends CharsetDecoder
   {
+    /** Helper to decode loops */
+    private static final ByteDecodeLoopHelper helper = new ByteDecodeLoopHelper()
+    {
+      protected boolean isMappable(byte b)
+      {
+        return true;
+      }
+      protected char mapToChar(byte b)
+      {
+        return (char)(b & 0xFF);
+      }
+    };
+
     // Package-private to avoid a trampoline constructor.
     Decoder (Charset cs)
     {
@@ -102,54 +120,46 @@ final class ISO_8859_1 extends Charset
 
     protected CoderResult decodeLoop (ByteBuffer in, CharBuffer out)
     {
-      // TODO: Optimize this in the case in.hasArray() / out.hasArray()
-      while (in.hasRemaining ())
-      {
-        byte b = in.get ();
-
-        if (!out.hasRemaining ())
-          {
-            in.position (in.position () - 1);
-            return CoderResult.OVERFLOW;
-          }
-
-        out.put ((char) (b & 0xFF));
-      }
-
-      return CoderResult.UNDERFLOW;
+      return helper.decodeLoop(in, out);
     }
   }
 
   private static final class Encoder extends CharsetEncoder
   {
+    /** Helper to encode loops */
+    private static final ByteEncodeLoopHelper helper = new ByteEncodeLoopHelper()
+    {
+      protected boolean isMappable(char c)
+      {
+        return c <= 0xff;
+      }
+      protected byte mapToByte(char c)
+      {
+        return (byte)c;
+      }
+    };
     // Package-private to avoid a trampoline constructor.
     Encoder (Charset cs)
     {
       super (cs, 1.0f, 1.0f);
     }
 
-    protected CoderResult encodeLoop (CharBuffer in, ByteBuffer out)
+    public boolean canEncode(char c)
     {
-      // TODO: Optimize this in the case in.hasArray() / out.hasArray()
-      while (in.hasRemaining ())
-      {
-        char c = in.get ();
-
-        if (c > 0xFF)
-          {
-            in.position (in.position () - 1);
-            return CoderResult.unmappableForLength (1);
-          }
-        if (!out.hasRemaining ())
-          {
-            in.position (in.position () - 1);
-            return CoderResult.OVERFLOW;
-          }
-
-        out.put ((byte) c);
-      }
+      return c <= 0xff;
+    }
+
+    public boolean canEncode(CharSequence cs)
+    {
+      for (int i = 0; i < cs.length(); ++i)
+        if (! canEncode(cs.charAt(i)))
+          return false;
+      return true;
+    }
 
-      return CoderResult.UNDERFLOW;
+    protected CoderResult encodeLoop (CharBuffer in, ByteBuffer out)
+    {
+      return helper.encodeLoop(in, out);
     }
   }
 }