United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-6211132 : (cs) CharsetDecoder.decode(ByteBuffer) may return wrong result

Details
Type:
Bug
Submit Date:
2004-12-21
Status:
Closed
Updated Date:
2011-02-16
Project Name:
JDK
Resolved Date:
2005-02-10
Component:
core-libs
OS:
windows_xp
Sub-Component:
java.nio
CPU:
x86
Priority:
P4
Resolution:
Duplicate
Affected Versions:
1.4.2
Fixed Versions:

Related Reports
Duplicate:

Sub Tasks

Description
FULL PRODUCT VERSION :
java version "1.4.2_05"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_05-b04)
Java HotSpot(TM) Client VM (build 1.4.2_05-b04, mixed mode)
 
A DESCRIPTION OF THE PROBLEM :
When use some CharsetDecoder which averageCharsPerByte less than 1 and decode 1 byte remain ByteBuffer, may return wrong result. May also appeared in CharsetEncoder.encode.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. declare a String only have 1 byte, such as "a".
2. declare a Charset which averageCharsPerByte property is less than 1, such as GBK.
3. use charset to encode the string.
4. use charset to decode the result in step 3.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
the string declared in step 1
ACTUAL -
nothing

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
String s = "a";
Charset charset = Charset.forName("GBK");
ByteBuffer buffer = charset.encode(s);
System.out.println(charset.decode(buffer));
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
public CharBuffer decode(ByteBuffer buffer) throws CharacterCodingException {
        CharsetDecoder decoder = getDecoder();

        int n = 0;
        if (buffer.remaining() > 0) {
            n = (int) (buffer.remaining() * decoder.averageCharsPerByte());
            if (n == 0)
                n = (int) (buffer.remaining() * decoder.maxCharsPerByte());
        }
        CharBuffer result = CharBuffer.allocate(n);
        if (n == 0)
            return result;

        decoder.reset();
        while (true) {
            CoderResult cr = buffer.hasRemaining() ? decoder.decode(buffer,
                    result, true) : decoder.flush(result);
            if (cr.isUnderflow())
                break;
            if (cr.isOverflow()) {
                n *= 2;
                result.flip();
                result = CharBuffer.allocate(n).put(result);
                continue;
            }
           cr.throwException();
        }
        result.flip();
        return result;
    }
###@###.### 2004-12-21 10:48:03 GMT

                                    

Comments



Hardware and Software, Engineered to Work Together