JDK-4853350 : (cs) Charset{De,En}coder constructors should enforce average <= max
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.nio
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: generic,solaris_8
  • CPU: generic,sparc
  • Submitted: 2003-04-24
  • Updated: 2017-06-29
  • Resolved: 2003-12-19
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
5.0 b32Fixed
Related Reports
Relates :  
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

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

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: tiger-beta FIXED IN: tiger-beta INTEGRATED IN: tiger-b32 tiger-beta VERIFIED IN: tiger-b63
13-09-2004

EVALUATION This problem affects all of the coder constructors, not just CharsetDecoder as reported. -- ###@###.### 2003/11/26
11-11-0192