From: Michael Ow Date: Mon, 25 Jul 2011 22:18:42 +0000 (+0000) Subject: ICU-8729 Fix source buffer's array offset handling error X-Git-Tag: milestone-59-0-1~4624 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ddaa367fa9c658c106426b4a5107bb6a6c056754;p=icu ICU-8729 Fix source buffer's array offset handling error X-SVN-Rev: 30420 --- diff --git a/icu4j/main/classes/charset/src/com/ibm/icu/charset/Charset88591.java b/icu4j/main/classes/charset/src/com/ibm/icu/charset/Charset88591.java index 761fd103da4..d56a175aafa 100644 --- a/icu4j/main/classes/charset/src/com/ibm/icu/charset/Charset88591.java +++ b/icu4j/main/classes/charset/src/com/ibm/icu/charset/Charset88591.java @@ -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 diff --git a/icu4j/main/classes/charset/src/com/ibm/icu/charset/CharsetASCII.java b/icu4j/main/classes/charset/src/com/ibm/icu/charset/CharsetASCII.java index 8bfc291bfe4..aab198a2c8f 100644 --- a/icu4j/main/classes/charset/src/com/ibm/icu/charset/CharsetASCII.java +++ b/icu4j/main/classes/charset/src/com/ibm/icu/charset/CharsetASCII.java @@ -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 diff --git a/icu4j/main/tests/charset/src/com/ibm/icu/dev/test/charset/TestCharset.java b/icu4j/main/tests/charset/src/com/ibm/icu/dev/test/charset/TestCharset.java index 200b355bebb..b6ac4bb353a 100644 --- a/icu4j/main/tests/charset/src/com/ibm/icu/dev/test/charset/TestCharset.java +++ b/icu4j/main/tests/charset/src/com/ibm/icu/dev/test/charset/TestCharset.java @@ -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]); + } + } + } }