]> granicus.if.org Git - icu/commitdiff
ICU-8729 Fix source buffer's array offset handling error
authorMichael Ow <mow@svn.icu-project.org>
Mon, 25 Jul 2011 22:18:42 +0000 (22:18 +0000)
committerMichael Ow <mow@svn.icu-project.org>
Mon, 25 Jul 2011 22:18:42 +0000 (22:18 +0000)
X-SVN-Rev: 30420

icu4j/main/classes/charset/src/com/ibm/icu/charset/Charset88591.java
icu4j/main/classes/charset/src/com/ibm/icu/charset/CharsetASCII.java
icu4j/main/tests/charset/src/com/ibm/icu/dev/test/charset/TestCharset.java

index 761fd103da4861b3cffdb5d30791c0f1d6392ee5..d56a175aafa76d276c0454e47cc0b1995040bda5 100644 (file)
@@ -86,7 +86,7 @@ class Charset88591 extends CharsetASCII {
              * early termination of the loop
              */
             if ((ch & 0xff00) != 0) {
-                source.position(i + 1);
+                source.position((i + 1) - source.arrayOffset());
                 target.position(i + offset);
                 return encodeMalformedOrUnmappable(source, ch, flush);
             } else
index 8bfc291bfe43a20b2a61320d9f7bfeed0b61950a..aab198a2c8f6b03e30fe40c0e75b279fdced776d 100644 (file)
@@ -281,7 +281,7 @@ 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
index 200b355bebbdb667d07a8b394b91d2a3deee6cd6..b6ac4bb353a5505a17b90419860e3295129b9b8e 100644 (file)
@@ -5674,4 +5674,47 @@ public class TestCharset extends TestFmwk {
             }
         }
     }
+    
+    /*
+     * When converting slices of a larger CharBuffer, Charset88591 and CharsetASCII does not handle the buffer correctly when
+     * an unmappable character occurs.
+     * Ticket #8729
+     */
+    public void TestCharsetASCII8859BufferHandling() {
+        String firstLine = "C077693790=|MEMO=|00=|022=|Blanche st and the driveway grate was fault and rotated under my car=|\r\n";
+        String secondLine = "C077693790=|MEMO=|00=|023=|puncturing the fuel tank. I spoke to the store operator (Ram Reddi –=|\r\n";
+        
+        String charsetNames[] = {
+                "ASCII",
+                "ISO-8859-1"
+        };
+        
+        CoderResult result = CoderResult.UNDERFLOW;
+        
+        CharsetEncoder encoder;
+        
+        ByteBuffer outBuffer = ByteBuffer.allocate(500);
+        CharBuffer charBuffer = CharBuffer.allocate(firstLine.length() + secondLine.length());
+        charBuffer.put(firstLine);
+        charBuffer.put(secondLine);
+        charBuffer.flip();
+        
+        for (int i = 0; i < charsetNames.length; i++) {
+            encoder =  CharsetICU.forNameICU(charsetNames[i]).newEncoder();
+            
+            charBuffer.position(firstLine.length());
+            CharBuffer charBufferSlice = charBuffer.slice();
+            charBufferSlice.limit(secondLine.length() - 2);
+    
+    
+            try {
+                result = encoder.encode(charBufferSlice, outBuffer, false);
+                if (!result.isUnmappable()) {
+                    errln("Result of encoding " + charsetNames[i] + " should be: \"Unmappable\". Instead got: " + result);
+                }
+            } catch (IllegalArgumentException ex) {
+                errln("IllegalArgumentException should not have been thrown when encoding: " + charsetNames[i]);
+            }
+        }
+    }
 }