United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-4853350 : (cs) Charset{De,En}coder constructors should enforce average <= max

Details
Type:
Bug
Submit Date:
2003-04-24
Status:
Closed
Updated Date:
2004-09-01
Project Name:
JDK
Resolved Date:
2003-12-19
Component:
core-libs
OS:
solaris_8,generic
Sub-Component:
java.nio
CPU:
sparc,generic
Priority:
P4
Resolution:
Fixed
Affected Versions:
5.0
Fixed Versions:
5.0 (b32)

Related Reports

Sub Tasks

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
EVALUATION

This problem affects all of the coder constructors, not just CharsetDecoder
as reported.

-- ###@###.### 2003/11/26
                                     
192-11-11 0
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


                                     
2004-09-13



Hardware and Software, Engineered to Work Together