// use java.nio to convert unicodeBuffer from char[] to byte[]
CharBuffer source = CharBuffer.wrap(unicodeBuffer, 0, unicodeBuffer.length);
CharsetEncoder encoder = Charset.forName(encoding).newEncoder();
encoder.onMalformedInput(CodingErrorAction.REPORT);
encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
ByteBuffer target = encoder.encode(source);
// target.array() will probably return what we want, but lets take no chances
encBuffer = new byte[target.limit()];
for (int i=0; i<encBuffer.length; i++)