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

Provide termination of CharsetDecoder by exclusive cipher

    XMLWordPrintable

    Details

    • Type: Enhancement
    • Status: Closed
    • Priority: P5
    • Resolution: Won't Fix
    • Affects Version/s: 6
    • Fix Version/s: None
    • Component/s: core-libs
    • Labels:
    • Subcomponent:
    • CPU:
      x86
    • OS:
      windows_xp

      Description

      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();

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: