// CharBuffer-ByteBuffer style encoding/decoding.
// Using nio methods for encoding and decoding.
// These are more efficient because there is less
// hidden copying of the data as there is when you work
// with String and byte[] in the previous methods.

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;

// choose an encoding
Charset latin1 = Charset.forName( "ISO-8859-1" );

// for byte to char
CharsetDecoder decoder = latin1.newDecoder();

// for char to byte
CharsetEncoder encoder = latin1.newEncoder();

// Presuming you have ByteBuffers and CharBuffer objects
// as a side effect of doing nio-style i/o.

// effectively convert byte[] to char[] after a read
CharBuffer charBuffer = decoder.decode( byteBuffer  );

// effectively convert char[] to byte[] before a write
ByteBuffer byteBuffer = encoder.encode( charBuffer );