Uploaded image for project: 'Harmony'
  1. Harmony
  2. HARMONY-148

java.nio.charset.CharsetDecoder: decode(in,out,endOfInput) method doesn't preserve replace string for successive decode invocation while "out" doesn't have engouh space for replace string.

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • None
    • None
    • Classlib
    • None

    Description

      Consider following senario:
      During the invocation of method java.nio.charset.CharsetDecoder.decode(in,out,false), method "decodeLoop" returns malformed CoderResult, and the action is set as "CodingErrorAction.REPLACE". So replace action should be taken, but "out" doesn't hold enough space for the replace string. Therefore, CoderResult.OVERFLOW should be returned. Harmony does return OVERFLOW currently, but the replace string won't put into new allocated output any more. In other words, the replace string is missing from the view of "stream decoding". Therefore, replace string should be preserved for successive decode(in,out,endOfInput) invacation.
      Both RI5.0 and Harmony fails on the following test case. However, according to the spec, we think it is a defect of RI5.0.

      ================================== Test Case======================
      /*

      • Test the method decode(ByteBuffer) .
        */
        public void testDecode_ReplaceOverflow() throws Exception {
        String replaceString = "a";
        Charset cs = Charset.forName("UTF-8");
        MockMalformedDecoder decoder = new MockMalformedDecoder(cs);
        decoder.onMalformedInput(CodingErrorAction.REPLACE);
        decoder.replaceWith(replaceString);
        CharBuffer out = CharBuffer.allocate(1);
        // MockMalformedDecoder treats the second byte '0x38' as malformed,
        // but "out" doesn't have enough space for replace string.
        ByteBuffer in = ByteBuffer.wrap(new byte[] {0x45,0x38,0x45,0x45}

        );
        CoderResult result = decoder.decode(in,out,false);
        assertTrue(result.isOverflow());

      // allocate enough space for "out"
      out = CharBuffer.allocate(10);
      // replace string should be put into "out" firstly,
      // and then decode "in".
      result =decoder.decode(in,out,true);
      out.flip();
      assertTrue(result.isUnderflow());
      assertEquals("abb",out.toString());
      }
      /*

      • Mock decoder. It treats byte whose value is less than "0x40" as malformed.
        */
        static class MockMalformedDecoder extends java.nio.charset.CharsetDecoder {

      public MockMalformedDecoder(Charset cs)

      { super(cs, 1, 10); }

      /*

      • It treats byte whose value is less than "0x40" as malformed.
      • Otherwise, it's decoded as 'b'.
        */
        protected CoderResult decodeLoop(ByteBuffer in, CharBuffer out) {
        System.out.println("Using MockMalformedDecoder");
        while (in.hasRemaining())
        Unknown macro: { byte b = in.get(); if(b < 0x40){ return CoderResult.malformedForLength(1); } if(!out.hasRemaining()){ return CoderResult.OVERFLOW; } out.put((char)'b'); }

        return CoderResult.UNDERFLOW;
        }
        }

      Attachments

        1. CharsetDecoderTest_patch_148.txt
          2 kB
          Paulex Yang
        2. CharsetDecoder_patch_148.txt
          1 kB
          Richard Liang

        Activity

          People

            tellison Tim Ellison
            richard_liang Richard Liang
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: