If you're using the characters 0-9,a-z,A-Z, it should (almost) always work. If you still have odd characters, you simply have a bug in your networking code (byte transfer). No encoding will save you from that
I think the issue in its core was that since channels are inconsistent in how many bytes are actually read, that the decoder wraps around the bytes it is fed and keeps them in track for you.
I'll post my simple echo server in java.nio here for those interested to look it over.
http://www.java-gaming.org/?action=pastebin&id=62I recommend you just stick with UTF-8:
1 2 3 4
| byte[] b = myString.getBytes("UTF-8"); byteBuffer.putInt(b.length); byteBuffer.put(b); |
1 2 3 4 5 6 7
| int len = byteBuffer.getInt();
byte[] b = new byte[len]; String s = new String(b,"UTF-8"); |
EDIT: heh, didn't see that last message. It's best to avoid Charset since it's slow. See code above.
What if the non-blocking channel.read() didn't register all the bytes that you're attempting to read with len?
And in your reading code, where exactly do you read the data into the buffer? o.O
Am I blind or do I only see you making a string of an empty allocated byte array of length len?