Harmony
  1. Harmony
  2. HARMONY-99

java.nio.charset.CharsetDecoder.decode(ByteBuffer in) does not throw MalformedInputException when buffer's current position is not legal

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Classlib
    • Labels:
      None

      Description

      According to 1.4.2 and 1.5 java.nio.charset.CharsetDecoder.decode(ByteBuffer in) throws
      MalformedInputException - If the byte sequence starting at the input buffer's current position is not legal for this charset and the current malformed-input action is CodingErrorAction.REPORT.

      Harmony does not throw MalformedInputException in this case as test listed below shows.

      Code to reproduce:
      import java.nio.*;
      import java.nio.charset.*;
      public class test2 {
      public static void main(String[] args) throws Exception {
      Charset cs =Charset.forName("utf-16");
      CharsetDecoder decoder = cs.newDecoder();
      decoder.onMalformedInput(CodingErrorAction.REPORT);
      decoder.onUnmappableCharacter(CodingErrorAction.REPORT);

      CharBuffer out = CharBuffer.allocate(10);
      ByteBuffer in = ByteBuffer.wrap(new byte[]

      { 109, 97, 109});
      CharBuffer res1 = decoder.decode(in);
      //output
      System.out.println("CharBuffer=="+res1);
      System.out.println("CharBuffer.position =="+res1.position());
      System.out.println("toHexString=="+Integer.toHexString(res1.charAt(0)));
      System.out.println("ByteBuffer.position()=="+in.position());
      System.out.println("ByteBuffer.remaining()=="+in.remaining());
      }
      }
      Steps to Reproduce:
      1. Build Harmony (check-out on 2006-01-30) j2se subset as described in README.txt.
      2. Compile test2.java using BEA 1.4 javac
      > javac -d . test2.java
      3. Run java using compatible VM (J9)
      > java -showversion test2

      Output:
      C:\tmp>C:\jrockit-j2sdk1.4.2_04\bin\java.exe -showversion test2
      java version "1.4.2_04"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_04-b05)
      BEA WebLogic JRockit(TM) 1.4.2_04 JVM (build ari-31788-20040616-1132-win-ia32, Native Threads, GC strategy: parallel)
      java.nio.charset.MalformedInputException: Input length = 1
      at java.nio.charset.CoderResult.throwException()V(CoderResult.java:260)
      at java.nio.charset.CharsetDecoder.decode(Ljava.nio.ByteBuffer;)Ljava.nio.CharBuffer;(CharsetDecoder.java:763)
      at test2.main([Ljava.lang.String;)V(test2.java:34)

      C:\tmp>C:\harmony\trunk\deploy\jre\bin\java -showversion test2
      (c) Copyright 1991, 2005 The Apache Software Foundation or its licensors, as applicable.

      res1==
      res1==0
      toHexString==6d61
      ByteBuffer.position()==3
      ByteBuffer.remaining()==0

      Suggested junit test case:
      ------------------------ CharsetDecoderTest.java -------------------------------------------------
      import junit.framework.*;
      import java.nio.*;
      import java.nio.charset.*;
      public class CharsetDecoderTest extends TestCase {
      public static void main(String[] args) { junit.textui.TestRunner.run(CharsetDecoderTest.class); }
      public void test_decode() {
      try {
      Charset cs =Charset.forName("utf-16");
      CharsetDecoder decoder = cs.newDecoder();
      decoder.onMalformedInput(CodingErrorAction.REPORT);
      decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
      CharBuffer out = CharBuffer.allocate(10);
      ByteBuffer in = ByteBuffer.wrap(new byte[] { 109, 97, 109}

      );
      CharBuffer res1 = decoder.decode(in);
      fail("MalformedInputException should have thrown");
      } catch (MalformedInputException e)

      { //expected }

      catch (CharacterCodingException e)

      { fail("unexpected CharacterCodingException"); }

      }
      }

        Activity

        Hide
        Tim Ellison added a comment -

        Verified by Svetlana

        Show
        Tim Ellison added a comment - Verified by Svetlana
        Hide
        Svetlana Samoilenko added a comment -

        Tim/Richard,
        thanks, bug is not reproducible with latest sources.

        Show
        Svetlana Samoilenko added a comment - Tim/Richard, thanks, bug is not reproducible with latest sources.
        Hide
        Tim Ellison added a comment -

        Svetlana / Richard,

        The patch looks good, thanks.

        Fixed in NIO_CHAR module java.nio.charset at repo revision 378491.

        Please check that this fully resolves your prolem.

        Show
        Tim Ellison added a comment - Svetlana / Richard, The patch looks good, thanks. Fixed in NIO_CHAR module java.nio.charset at repo revision 378491. Please check that this fully resolves your prolem.
        Hide
        Svetlana Samoilenko added a comment -

        Richard, thanks, your patch works fihe!
        At least, my test pass

        Show
        Svetlana Samoilenko added a comment - Richard, thanks, your patch works fihe! At least, my test pass
        Hide
        Richard Liang added a comment -

        Please try me patch. At least, it will pass the proposed test case.

        Show
        Richard Liang added a comment - Please try me patch. At least, it will pass the proposed test case.

          People

          • Assignee:
            Tim Ellison
            Reporter:
            Svetlana Samoilenko
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development