Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6695386

Provide termination of CharsetDecoder by exclusive cipher

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Won't Fix
    • Icon: P5 P5
    • None
    • 6
    • core-libs
    • x86
    • windows_xp

      A DESCRIPTION OF THE REQUEST :
      Class java.nio.CharsetDecoder is often used to encode native zero-terminated ASCII strings.
      If used ByteBuffer wraps from a fixed sized byte array there are probably trailing 0x00's and neither position nor limit are correctly aligned to these. Then the ByteBuffer's limit must first be aligned to the first 0x00 to avoid invalid chars in the destination CharBuffer.

      For this I suggest to add following methods:
          public int terminator() {
      throw new UnsupportedOperationException();
          }
          public CharsetDecoder terminateBy(int newTerminator) {
      throw new UnsupportedOperationException();
          }
      By default they should throw UnsupportedOperationException and should be overridden by appropriated sub classes.

      JUSTIFICATION :
      - zero-terminated ASCII strings are often used to access native APIs.
      - is more elegant than extra lines of code
      - will enhance performance, as source bytes must not be looped twice
      - e.g. helps to fix bug 6449421



      ---------- BEGIN SOURCE ----------
      Usage (e.g.):
      (Possible fix for http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6449421)

        return Charset.forName(charSet).newDecoder()
            .onMalformedInput(CodingErrorAction.REPLACE)
            .onUnmappableCharacter(CodingErrorAction.REPLACE)
            .replaceWith("?")
            .terminateBy(0)
            .decode (ByteBuffer.wrap(inBytes))
            .toString();


      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
        int len = 0;
        while (inBytes[len] != 0)
          len++;
        return Charset.forName(charSet).newDecoder()
            .onMalformedInput(CodingErrorAction.REPLACE)
            .onUnmappableCharacter(CodingErrorAction.REPLACE)
            .replaceWith("?")
            .decode (ByteBuffer.wrap(inBytes, 0, len))
            .toString();

            sherman Xueming Shen
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: