/** * Converts the provided String to bytes using the UTF-8 encoding. If the input is malformed, * invalid chars are replaced by a default value. * * @param string the string to encode * @return ByteBuffer: bytes stores at ByteBuffer.array() and length is ByteBuffer.limit() * @throws CharacterCodingException if the string could not be encoded */ public static ByteBuffer encode(String string) throws CharacterCodingException { return encode(string, true); }
/** * Set to contain the contents of a string. * * @param string the string to initialize from */ public void set(String string) { try { ByteBuffer bb = encode(string, true); bytes = bb.array(); length = bb.limit(); } catch (CharacterCodingException e) { throw new RuntimeException("Should not have happened ", e); } }
try { ByteBuffer src = ByteBuffer.wrap(this.bytes, 0, this.length); ByteBuffer tgt = encode(what); byte b = tgt.get(); src.position(start);