Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Fixed
-
None
-
None
-
None
Description
java.nio.charset.Charset.decode(in) doesn't use the same cached decoder.
As spec says, "An invocation of this method upon a charset cs returns the same result as the expression cs.newDecoder() .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE) .decode(bb); except that it is potentially more efficient because it can cache decoders between successive invocations. "
RI always uses the same cached decoder (the same reference) for the same name charset. For details, please refer to the test case below:
======== test case output =====
RI 5.0 passes the test case while Harmony fails.
======== test case===========
/*
- test cached decoder
*/
public void testCachedDecoder() throws ExceptionUnknown macro: { MockCachedCharset cs1 = new MockCachedCharset("CachedCharset",null); MockCachedCharset cs2 = new MockCachedCharset("CachedCharset",null); ByteBuffer in = ByteBuffer.wrap(new byte[]{0x00}); cs1.decode(in); in.flip(); cs2.decode(in); in.flip(); }/*
- Mock Charset for cached decoder test
*/
static class MockCachedCharset extends Charset{
public MockCachedCharset(String canonicalName, String[] aliases)
{ super(canonicalName, aliases); }public boolean contains(Charset charset)
{ return false; }public CharsetEncoder newEncoder()
{ return null; }public CharsetDecoder newDecoder()
{ return new MockCachedDecoder(this); } }
/*
- Mock decoder. Only one caller is permitted.
*/
static class MockCachedDecoder extends CharsetDecoder {
static MockCachedDecoder caller = null;
public MockCachedDecoder(Charset cs)
{ super(cs, 1, 10); }/*
- Only one caller is permitted.
- If there's another caller, throw RuntimeException.
*/
protected CoderResult decodeLoop(ByteBuffer in, CharBuffer out) {
if(null == caller) { caller = this; }else
Unknown macro: { if(caller != this){ // Another instance throw new RuntimeException(); } }return CoderResult.UNDERFLOW;
}
}