Thursday, September 15, 2005

Unanswered CharsetDecoder.decode bug report

dateCreated: Tue Aug 23 06:26:27 MDT 2005
type: bug
cust_name: Daniel Aioanei
jdcid: aioaneid
status: Waiting
category: java
subcategory: classes_nio
company: undisclosed
release: 5.0
hardware: x86
OSversion: win_xp
priority: 4
synopsis: java.nio.charset.CharsetDecoder.decode(ByteBuffer) does not call implFlush()
description: FULL PRODUCT VERSION :
java version "1.5.0_04"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_04-b05)
Java HotSpot(TM) Client VM (build 1.5.0_04-b05, mixed mode, sharing)

A DESCRIPTION OF THE PROBLEM :
java.nio.charset.CharsetDecoder.decode(ByteBuffer in) should call implFlush() as per the method's javadoc, but it doesn't.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the attached program.


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CoderResult;

public class CharsetDecoderTest {

public static void main(String[] args) throws CharacterCodingException {
new MyCharset().newDecoder().decode(ByteBuffer.wrap(new byte[1]));
System.out.println(MyCharsetDecoder.IMPL_FLUSH_CALLED);
}

static class MyCharset extends Charset {

protected MyCharset() {
super("canonicalName", null);
}

@Override
public boolean contains(Charset cs) {
return false;
}

@Override
public CharsetEncoder newEncoder() {
return null;
}

@Override
public CharsetDecoder newDecoder() {
return new MyCharsetDecoder(this);
}
}

static class MyCharsetDecoder extends CharsetDecoder {

protected MyCharsetDecoder(Charset cs) {
super(cs, 1, 1);
}

public static boolean IMPL_FLUSH_CALLED;

@Override
protected CoderResult decodeLoop(ByteBuffer in, CharBuffer out) {

while (in.remaining() > 0) {
in.get();
}

return CoderResult.UNDERFLOW;
}

@Override
protected CoderResult implFlush(CharBuffer out) {
IMPL_FLUSH_CALLED = true;
return CoderResult.UNDERFLOW;
}

}

}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
public static final CharBuffer decode(CharsetDecoder decoder, ByteBuffer in)
throws CharacterCodingException {

CharBuffer out = decoder.decode(in);

int n = 16;
CharBuffer tail = CharBuffer.allocate(n);

CoderResult cr;

do {
try {
cr = decoder.flush(tail);
} catch (IllegalStateException ise) {
return out;
}

if (cr.isOverflow()) {
n *= 2;
CharBuffer o = CharBuffer.allocate(n);
tail.flip();
o.put(tail);
tail = o;
}
} while (cr.isOverflow());

tail.flip();

if (tail.position() == tail.limit()) {
return out;
} else {
CharBuffer bb = CharBuffer.allocate(out.limit() - out.position()
+ tail.limit() - tail.position());
bb.put(out);
bb.put(tail);
bb.flip();
return bb;
}

}

1 Comments:

Blogger benalexander4983 said...

i thought your blog was cool and i think you may like this cool Website. now just Click Here

4:59 PM  

Post a Comment

<< Home