/** * Creates a new instance with the specified <tt>charset</tt> * and the specified <tt>delimiter</tt>. * * @param charset The {@link Charset} to use * @param delimiter The line delimiter to use */ public TextLineDecoder(Charset charset, LineDelimiter delimiter) { if (charset == null) { throw new IllegalArgumentException("charset parameter shuld not be null"); } if (delimiter == null) { throw new IllegalArgumentException("delimiter parameter should not be null"); } this.charset = charset; this.delimiter = delimiter; // Convert delimiter to ByteBuffer if not done yet. if (delimBuf == null) { IoBuffer tmp = IoBuffer.allocate(2).setAutoExpand(true); try { tmp.putString(delimiter.getValue(), charset.newEncoder()); } catch (CharacterCodingException cce) { } tmp.flip(); delimBuf = tmp; } }
/** * Creates a new instance with the specified <tt>charset</tt> * and the specified <tt>delimiter</tt>. * * @param charset The {@link Charset} to use * @param delimiter The line delimiter to use */ public TextLineDecoder(Charset charset, LineDelimiter delimiter) { if (charset == null) { throw new IllegalArgumentException("charset parameter shuld not be null"); } if (delimiter == null) { throw new IllegalArgumentException("delimiter parameter should not be null"); } this.charset = charset; this.delimiter = delimiter; // Convert delimiter to ByteBuffer if not done yet. if (delimBuf == null) { IoBuffer tmp = IoBuffer.allocate(2).setAutoExpand(true); try { tmp.putString(delimiter.getValue(), charset.newEncoder()); } catch (CharacterCodingException cce) { } tmp.flip(); delimBuf = tmp; } }
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(); IoBuffer buf = IoBuffer.allocate(value.length()) .setAutoExpand(true); buf.putString(value, encoder); if (buf.position() > maxLineLength) { throw new IllegalArgumentException("Line length: " + buf.position()); } buf.putString(delimiter.getValue(), encoder); buf.flip(); out.write(buf); }
/** * {@inheritDoc} */ @Override 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 == null ? "" : message.toString(); IoBuffer buf = IoBuffer.allocate(value.length()).setAutoExpand(true); buf.putString(value, encoder); if (buf.position() > maxLineLength) { throw new IllegalArgumentException("Line length: " + buf.position()); } buf.putString(delimiter.getValue(), encoder); buf.flip(); out.write(buf); }
/** * {@inheritDoc} */ @Override 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 == null ? "" : message.toString(); IoBuffer buf = IoBuffer.allocate(value.length()).setAutoExpand(true); buf.putString(value, encoder); if (buf.position() > maxLineLength) { throw new IllegalArgumentException("Line length: " + buf.position()); } buf.putString(delimiter.getValue(), encoder); buf.flip(); out.write(buf); }
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.putString( delimiter.getValue(), encoder ); buf.flip(); out.write( buf ); }
private PushManager() { connector=new NioSocketConnector(); connector.setConnectTimeoutMillis(Params.CONNECT_TIMEOUT); //为接收器设置管理服务 connector.setHandler(new ClientSessionHandler()); //设置过滤器(使用Mina提供的文本换行符编解码器) connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"), LineDelimiter.WINDOWS.getValue(),LineDelimiter.WINDOWS.getValue()))); //读写通道5秒内无操作进入空闲状态 connector.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, Params.REQUEST_TIMEOUT); //设置读取数据的缓冲区大小 connector.getSessionConfig().setReadBufferSize(2048); //设置心跳 KeepAliveMessageFactory heartBeatFactory = new ClientKeepAliveMessageFactoryImp(); KeepAliveRequestTimeoutHandler heartBeatHandler = new ClientKeepAliveMessageTimeoutFactoryImp(); KeepAliveFilter heartBeat = new KeepAliveFilter(heartBeatFactory, IdleStatus.BOTH_IDLE, heartBeatHandler); //是否回发 heartBeat.setForwardEvent(true); //心跳发送频率 heartBeat.setRequestInterval(Params.REQUEST_INTERVAL); connector.getSessionConfig().setKeepAlive(true); connector.getFilterChain().addLast("keepalive", heartBeat); }
LineDelimiter.UNIX.getValue(), "EOF"); textLine.setDecoderMaxLineLength(Integer.MAX_VALUE);
tmp.putString(delimiter.getValue(), charset.newEncoder()); tmp.flip(); delimBuf = tmp;
tmp.putString( delimiter.getValue(), charset.newEncoder() ); tmp.flip(); delimBuf = tmp;