How do I convert between ByteBuffer and a CharBuffer using Java 1.4?

John Zukowski

The java.nio.charset package include character set converters for you. To go from ByteBuffer to CharBuffer, you would do something like the following:

ByteBuffer buffer = ...
Charset charset = Charset.forName("ISO-8859-1");
CharsetDecoder decoder = charset.newDecoder();
CharBuffer charBuffer = decoder.decode(buffer);
For different character sets, provide a different name to the forName() call.