CharsetEncoder CoderResult underflows on Japanese string

I am encoding/decoding various objects over the network. Inside the guts of the e/d engine I have the following segment:

inBuffer.clear();

inBuffer.put(string);

inBuffer.flip();

outBuffer.clear();

encoder.reset();

CoderResult coderResult = encoder.encode(inBuffer, outBuffer,true);

The encoder is using UTF-8. This works great if the string is a regular ASCII string. As soon as I put any Japanese characters in the string, the last line underflows (coderResult.isUnderflow() is true). I debugged this a bit and the buffer sizes seem correct and the hex bytes inside buffers also seem correct.

If I don't use encoder and just do it with bytes, it works fine:

bytes = string.getBytes("UTF-8");

outBuffer.clear();

for(int i=0; i<bytes.length; i++){

outBuffer.put(bytes[i]);

}

finalint length = outBuffer.position();

outBuffer.flip();

However, for some deep reasons, I can't use the latter approach.

I examined the source code of the CharsetEncoder and it boils down to a native method. Since the trivial approach with bytes works, I am probably doing something fundamentally wrong. I am not very familiar with NIO -- any NIO experts out there who could help?>

[1519 byte] By [mihailoa] at [2007-11-26 22:18:09]
# 1
OK, it's me being dumb. I confused length in characters with length in bytes in one line of my code and that's why it was underflowing...
mihailoa at 2007-7-10 11:12:47 > top of Java-index,Core,Core APIs...