Work around icu4c causing canEncode to always return true for surrogates.
Bug: 10310751
(cherry picked from commit bb317b5a6ff071ee4ffa0b4e811367be962238b7)
Change-Id: Id31ea5715c1e3f744ed8eb33d6a7f3cca03159d1
diff --git a/luni/src/main/java/java/nio/charset/CharsetEncoder.java b/luni/src/main/java/java/nio/charset/CharsetEncoder.java
index c2d74a1..5c4d3b3 100644
--- a/luni/src/main/java/java/nio/charset/CharsetEncoder.java
+++ b/luni/src/main/java/java/nio/charset/CharsetEncoder.java
@@ -204,7 +204,7 @@
onMalformedInput(CodingErrorAction.REPORT);
onUnmappableCharacter(CodingErrorAction.REPORT);
try {
- this.encode(cb);
+ encode(cb);
return true;
} catch (CharacterCodingException e) {
return false;
@@ -290,9 +290,6 @@
return out;
}
- /*
- * checks the result whether it needs to throw CharacterCodingException.
- */
private void checkCoderResult(CoderResult result) throws CharacterCodingException {
if (malformedInputAction == CodingErrorAction.REPORT && result.isMalformed()) {
throw new MalformedInputException(result.length());
@@ -301,7 +298,6 @@
}
}
- // allocate more spaces to the given ByteBuffer
private ByteBuffer allocateMore(ByteBuffer output) {
if (output.capacity() == 0) {
return ByteBuffer.allocate(1);
diff --git a/luni/src/test/java/libcore/java/nio/charset/OldCharset_AbstractTest.java b/luni/src/test/java/libcore/java/nio/charset/OldCharset_AbstractTest.java
index 411727e..d4cf83e 100644
--- a/luni/src/test/java/libcore/java/nio/charset/OldCharset_AbstractTest.java
+++ b/luni/src/test/java/libcore/java/nio/charset/OldCharset_AbstractTest.java
@@ -177,7 +177,12 @@
encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
decoder.onMalformedInput(CodingErrorAction.REPORT);
CharBuffer inputCB = CharBuffer.allocate(65536);
- for (int code = 32; code <= 65533; code ++) {
+ for (int code = 32; code <= 65533; ++code) {
+ // icu4c seems to accept any surrogate as a sign that "more is coming",
+ // even for charsets like US-ASCII. http://b/10310751
+ if (code >= 0xd800 && code <= 0xdfff) {
+ continue;
+ }
if (encoder.canEncode((char) code)) {
inputCB.put((char) code);
}