Thursday, September 15, 2005

Unanswered CharsetEncoder.encode Bug Report

dateCreated: Tue Aug 23 01:52:56 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.CharsetEncoder.encode(CharBuffer in) 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)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]

A DESCRIPTION OF THE PROBLEM :
According to the javadoc of java.nio.charset.CharsetEncoder.encode(CharBuffer), it finally flushes the encoder. This is not really happening though.


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 CharsetTest {

public static void main(String[] args) throws CharacterCodingException {
new MyCharset().newEncoder().encode(CharBuffer.wrap("abc"));
System.out.println(MyCharsetEncoder.IMPL_FLUSH_CALLED);
}

static class MyCharset extends Charset {

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

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

@Override
public CharsetDecoder newDecoder() {
return null;
}

@Override
public CharsetEncoder newEncoder() {
return new MyCharsetEncoder(this);
}

}

static class MyCharsetEncoder extends CharsetEncoder {

protected MyCharsetEncoder(Charset cs) {
super(cs, 1, 1, new byte[1]);
}

public static boolean IMPL_FLUSH_CALLED;

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

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

return CoderResult.UNDERFLOW;
}

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

@Override
public boolean isLegalReplacement(byte[] repl) {
return true;
}
}

}

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

CUSTOMER SUBMITTED WORKAROUND :
I think that calling encode(CharBuffer in, ByteBuffer out, boolean endOfInput) followed by flush() should do the trick.

0 Comments:

Post a Comment

<< Home