if (strCharset == null || "".equals(strCharset)) {
charSet = Charset.defaultCharset();
} else if (Charset.isSupported(strCharset)) {
charSet = Charset.forName(strCharset);
} else {
CharacterCodingException e = new CharacterCodingException();
e.initCause(new UnsupportedCharsetException(strCharset));
throw e;
}
CharsetEncoder encoder = charSet.newEncoder();
ByteBuffer byteBuffer = null;
try {
byteBuffer = encoder.encode(charBuffer);
} catch(CharacterCodingException cce) {
throw cce;
} catch(Throwable t) {
CharacterCodingException e = new CharacterCodingException();
e.initCause(t);
throw e;
}
byte[] result = new byte[byteBuffer.remaining()];
byteBuffer.get(result);