Code example for CharsetEncoder

Methods: encodereset

0
	public static CharsetEncoder encoder = charset.newEncoder();
	public static CharsetDecoder decoder = charset.newDecoder();
 
	public static ByteBuffer toByteBUffer(String msg) {
		try { 
			encoder.reset();
			return encoder.encode(CharBuffer.wrap(msg));
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return null; 
	} 
 
	public static String toString(ByteBuffer buffer) {
		String data = "";
		try { 
			int old_position = buffer.position();
			decoder.reset();
			data = decoder.decode(buffer).toString();
			// reset buffer's position to its original so it is not altered: