Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Duplicate
-
None
-
None
-
None
-
None
Description
As spec says, "An invocation of this method upon a charset cs returns the same result as the expression cs.newEncoder() .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE) .decode(bb); except that it is potentially more efficient because it can cache encoders between successive invocations. "
RI always uses the same cached encoder (the same reference) for the charsets with same name.
The following test case pass on RI 5.0, but fail on Harmony.
/*
- test cached encoder
*/
public void testCachedEncoder() throws Exception { MockCachedCharset cs1 = new MockCachedCharset("CachedCharset", null); MockCachedCharset cs2 = new MockCachedCharset("CachedCharset", null); CharBuffer in = CharBuffer.wrap("A"); cs1.encode(in); in.flip(); cs2.encode(in); }
/*
- Mock Charset for cached encoder test
*/
static class MockCachedCharset extends Charset {
public MockCachedCharset(String canonicalName, String[] aliases)
{ super(canonicalName, aliases); }public boolean contains(Charset charset)
{ return false; }public CharsetDecoder newDecoder()
{ return new MockCachedDecoder(this); }public CharsetEncoder newEncoder()
{ return new MockCachedEncoder(this); }}
/*
- Mock encoder. Only one caller is permitted.
*/
static class MockCachedEncoder extends CharsetEncoder {
static MockCachedEncoder caller = null;
public MockCachedEncoder(Charset cs)
{ super(cs, 1, 10); }/*
* Only one caller is permitted.
*/
protected CoderResult encodeLoop(CharBuffer in, ByteBuffer out) {
if (null == caller) { caller = this; } else {
if (caller != this) { // Another instance fail("should use same instance"); }
}
return CoderResult.UNDERFLOW;
}
}
/*
* Mock decoder.
*/
static class MockCachedDecoder extends CharsetDecoder {
static MockCachedEncoder caller = null;
public MockCachedDecoder(Charset cs) { super(cs, 1, 10); }
protected CoderResult decodeLoop(ByteBuffer in, CharBuffer out)
{ in.position(in.limit()); return CoderResult.UNDERFLOW; }}