Harmony
  1. Harmony
  2. HARMONY-182

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

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Duplicate
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      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; }

      }

        Activity

        Hide
        Tim Ellison added a comment -

        Fixed by duplicate

        Show
        Tim Ellison added a comment - Fixed by duplicate
        Hide
        Richard Liang added a comment -

        Hi, Tim,

        Please close this issue since HARMONY-150 has been resolved and closed.

        Thanks!

        Show
        Richard Liang added a comment - Hi, Tim, Please close this issue since HARMONY-150 has been resolved and closed. Thanks!
        Hide
        Tim Ellison added a comment -

        Duplicate of HARMONY-150

        Show
        Tim Ellison added a comment - Duplicate of HARMONY-150
        Hide
        Paulex Yang added a comment -

        This issue is duplicate with JIRA 150(http://issues.apache.org/jira/browse/HARMONY-150), because Charset class uses same mechanism to cache encoder and decoder, so the patch for 150 should have fixed this problem.

        Show
        Paulex Yang added a comment - This issue is duplicate with JIRA 150( http://issues.apache.org/jira/browse/HARMONY-150 ), because Charset class uses same mechanism to cache encoder and decoder, so the patch for 150 should have fixed this problem.

          People

          • Assignee:
            Tim Ellison
            Reporter:
            Richard Liang
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development