public byte[] encode(CharSequence cs) { ByteBuffer buffer; if (cs.length() > CHAR_THRESHOLD) { buffer = UTF8.encode(cs.toString()); } else { buffer = byteBuffer; CharBuffer cbuffer = charBuffer; buffer.clear(); cbuffer.clear(); cbuffer.put(cs.toString()); cbuffer.flip(); encoder.encode(cbuffer, buffer, true); buffer.flip(); } byte[] bytes = new byte[buffer.limit()]; buffer.get(bytes); return bytes; }
public static byte[] encode(CharSequence cs, Charset charset) { ByteBuffer buffer = charset.encode(cs.toString()); byte[] bytes = new byte[buffer.limit()]; buffer.get(bytes); return bytes; }