// encoding a String to bytes
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;

String s = ...;
Charset cs = Charset.forName( "Shift_JIS" /* encoding */ );
CharBuffer ss = CharBuffer.wrap( s );
ByteBuffer bb = cs.encode ( ss );
int limit = bb.limit();  // how many chars in buffer
byte[] b = new byte[ limit ];

// copy limit bytes into b
bb.get( b, 0 /* offset */, limit );