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

(cs) Charset{De,En}coder constructors should enforce average <= max

    XMLWordPrintable

    Details

    • Subcomponent:
    • Resolved In Build:
      b32
    • CPU:
      generic, sparc
    • OS:
      generic, solaris_8
    • Verification:
      Verified

      Description



      Name: auR10023 Date: 04/24/2003



      java.nio.charset.CharsetDecoder.CharsetDecoder(Charset, float, float)
      should throw llegalArgumentException with invalid parameters.

      Javadoc for this method says:
      ...
      protected CharsetDecoder(Charset cs,
                               float averageCharsPerByte,
                               float maxCharsPerByte)

      Initializes a new decoder. The new decoder will have the given chars-per-byte values and its replacement will be the string "\uFFFD".

      Parameters:
      averageCharsPerByte - A positive float value indicating the expected number of characters that will be produced for each input byte
      maxCharsPerByte - A positive float value indicating the maximum number of characters that will be produced for each input byte
      Throws:
      IllegalArgumentException - If the preconditions on the parameters do not hold
      ...

      But invocation of this constructor with averageCharsPerByte > maxCharsPerByte
      (invalid values, because average value cannot be greater than maximum value)
      passes without any exception.

      Here is the example:

      -------t.java---------
      import java.util.*;
      import java.nio.*;
      import java.nio.charset.*;

      public class t extends CharsetDecoder {
          public t(CharsetDecoder d) {
              super(d.charset(), 2, 1);
          }

          protected CoderResult decodeLoop(ByteBuffer in, CharBuffer out) {
              return null;
          }

          public static void main(String [] args) {

              CharsetDecoder d = null;

              try {
                  d = Charset.forName("US-ASCII").newDecoder();
              } catch(Exception e) {
                  e.printStackTrace(System.out);
                  return;
              }

              try {
                  new t(d);
                  System.out.println("IllegalArgumentException expected");
              } catch(IllegalArgumentException e) {
                  System.out.println("OKAY");
              }
          }
      }

      Here is the result
      #java -version

      java version "1.5.0-beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta-b04)
      Java HotSpot(TM) Client VM (build 1.5.0-beta-b04, mixed mode)

      #java t
      IllegalArgumentException expected

      ======================================================================

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              mr Mark Reinhold
              Reporter:
              avusunw Avu Avu (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: