Code example for Charset

Methods: newEncoder

0
 
	public void encode(IoSession session, Object message,
			ProtocolEncoderOutput out) throws Exception {
		CharsetEncoder encoder = (CharsetEncoder) session.getAttribute(ENCODER);
		if (encoder == null) {
			encoder = charset.newEncoder();
			session.setAttribute(ENCODER, encoder);
		} 
 
		String value = message.toString();
		ByteBuffer buf = ByteBuffer.allocate(value.length())
				.setAutoExpand(true); 
		buf.putString(value, encoder);
		if (buf.position() > maxLineLength) {
			throw new IllegalArgumentException("Line length: " + buf.position());
		} 
		buf.put((byte) 0);
		buf.flip();
		out.write(buf);
	}