java.nio.charset.Charset.encode(in) doesn't use the same cached encoder.
------------------------------------------------------------------------

         Key: HARMONY-182
         URL: http://issues.apache.org/jira/browse/HARMONY-182
     Project: Harmony
        Type: Bug
    Reporter: Richard Liang


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;
                }
        }

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
   http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see:
   http://www.atlassian.com/software/jira

Reply via email to