]> gitweb.fperrin.net Git - Dictionary.git/blobdiff - jars/icu4j-52_1/main/classes/charset/src/com/ibm/icu/charset/CharsetASCII.java
Upgrade ICU4J.
[Dictionary.git] / jars / icu4j-52_1 / main / classes / charset / src / com / ibm / icu / charset / CharsetASCII.java
similarity index 84%
rename from jars/icu4j-4_8_1_1/main/classes/charset/src/com/ibm/icu/charset/CharsetASCII.java
rename to jars/icu4j-52_1/main/classes/charset/src/com/ibm/icu/charset/CharsetASCII.java
index 419a4601aeba1646051c9f68480bc86408fcc5b6..aab198a2c8f6b03e30fe40c0e75b279fdced776d 100644 (file)
@@ -1,6 +1,6 @@
 /**
  *******************************************************************************
- * Copyright (C) 2006-2008, International Business Machines Corporation and    *
+ * Copyright (C) 2006-2011, International Business Machines Corporation and    *
  * others. All Rights Reserved.                                                *
  *******************************************************************************
  *
@@ -8,8 +8,6 @@
  */
 package com.ibm.icu.charset;
 
-import java.nio.BufferOverflowException;
-import java.nio.BufferUnderflowException;
 import java.nio.ByteBuffer;
 import java.nio.CharBuffer;
 import java.nio.IntBuffer;
@@ -90,21 +88,10 @@ class CharsetASCII extends CharsetICU {
                 }
             } else {
                 /* unoptimized loop */
-
-                try {
-                    /*
-                     * perform the core loop... if it throws an exception, it must be due to an
-                     * overflow or underflow
-                     */
-                    cr = decodeLoopCoreUnoptimized(source, target);
-
-                } catch (BufferUnderflowException ex) {
-                    /* all of the source has been read */
-                    cr = CoderResult.UNDERFLOW;
-                } catch (BufferOverflowException ex) {
+                cr = decodeLoopCoreUnoptimized(source, target);
+                if (cr == CoderResult.OVERFLOW) {
                     /* the target is full */
                     source.position(source.position() - 1); /* rewind by 1 */
-                    cr = CoderResult.OVERFLOW;
                 }
             }
 
@@ -142,22 +129,32 @@ class CharsetASCII extends CharsetICU {
                 return null;
         }
 
-        protected CoderResult decodeLoopCoreUnoptimized(ByteBuffer source, CharBuffer target)
-                throws BufferUnderflowException, BufferOverflowException {
+        protected CoderResult decodeLoopCoreUnoptimized(ByteBuffer source, CharBuffer target) {
             int ch = 0;
 
             /*
              * perform ascii conversion from the source buffer to the target buffer, making sure
              * each byte in the source is within the correct range
              */
-            while (((ch = (source.get() & 0xff)) & 0x80) == 0)
-                target.put((char) ch);
-
-            /*
-             * if we reach here, it's because a character was not in the correct range, and we need
-             * to deak with this by calling decodeMalformedOrUnmappable
-             */
-            return decodeMalformedOrUnmappable(ch);
+            while (source.hasRemaining()) {
+                ch = source.get() & 0xff;
+                
+                if ((ch & 0x80) == 0) {
+                    if (target.hasRemaining()) {
+                        target.put((char)ch);
+                    } else {
+                        return CoderResult.OVERFLOW;
+                    }
+                } else {
+                    /*
+                     * if we reach here, it's because a character was not in the correct range, and we need
+                     * to deak with this by calling decodeMalformedOrUnmappable
+                     */
+                    return decodeMalformedOrUnmappable(ch);
+                }
+            }
+            
+            return CoderResult.UNDERFLOW;
         }
 
         protected CoderResult decodeMalformedOrUnmappable(int ch) {
@@ -247,19 +244,11 @@ class CharsetASCII extends CharsetICU {
                     }
                 } else {
                     /* unoptimized loop */
-
-                    try {
-                        /*
-                         * perform the core loop... if it throws an exception, it must be due to an
-                         * overflow or underflow
-                         */
-                        cr = encodeLoopCoreUnoptimized(source, target, flush);
-
-                    } catch (BufferUnderflowException ex) {
-                        cr = CoderResult.UNDERFLOW;
-                    } catch (BufferOverflowException ex) {
+                    
+                    cr = encodeLoopCoreUnoptimized(source, target, flush);
+                    
+                    if (cr == CoderResult.OVERFLOW) {
                         source.position(source.position() - 1); /* rewind by 1 */
-                        cr = CoderResult.OVERFLOW;
                     }
                 }
             }
@@ -292,29 +281,39 @@ class CharsetASCII extends CharsetICU {
              * early termination of the loop
              */
             if ((ch & 0xff80) != 0) {
-                source.position(i + 1);
+                source.position((i + 1) - source.arrayOffset());
                 target.position(i + offset);
                 return encodeMalformedOrUnmappable(source, ch, flush);
             } else
                 return null;
         }
 
-        protected CoderResult encodeLoopCoreUnoptimized(CharBuffer source, ByteBuffer target,
-                boolean flush) throws BufferUnderflowException, BufferOverflowException {
+        protected CoderResult encodeLoopCoreUnoptimized(CharBuffer source, ByteBuffer target, boolean flush) {
             int ch;
-
+            
             /*
              * perform ascii conversion from the source buffer to the target buffer, making sure
              * each char in the source is within the correct range
              */
-            while (((ch = (int) source.get()) & 0xff80) == 0)
-                target.put((byte) ch);
-
-            /*
-             * if we reach here, it's because a character was not in the correct range, and we need
-             * to deak with this by calling encodeMalformedOrUnmappable.
-             */
-            return encodeMalformedOrUnmappable(source, ch, flush);
+            while (source.hasRemaining()) {
+                ch = (int) source.get();
+                
+                if ((ch & 0xff80) == 0) {
+                    if (target.hasRemaining()) {
+                        target.put((byte) ch);
+                    } else {
+                        return CoderResult.OVERFLOW;
+                    }
+                } else {
+                    /*
+                     * if we reach here, it's because a character was not in the correct range, and we need
+                     * to deak with this by calling encodeMalformedOrUnmappable.
+                     */
+                    return encodeMalformedOrUnmappable(source, ch, flush);
+                }
+            }
+            
+            return CoderResult.UNDERFLOW;
         }
 
         protected final CoderResult encodeMalformedOrUnmappable(CharBuffer source, int ch, boolean flush) {