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?>

