/** * 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 TextLineEncoder(Charset charset, String delimiter) { this(charset, new LineDelimiter(delimiter)); }
/** * 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 TextLineEncoder( Charset charset, LineDelimiter delimiter ) { if( charset == null ) { throw new NullPointerException( "charset" ); } if( delimiter == null ) { throw new NullPointerException( "delimiter" ); } if( LineDelimiter.AUTO.equals( delimiter ) ) { throw new IllegalArgumentException( "AUTO delimiter is not allowed for encoder." ); } this.charset = charset; this.delimiter = delimiter; }
/** * 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 TextLineEncoder(Charset charset, LineDelimiter delimiter) { if (charset == null) { throw new IllegalArgumentException("charset"); } if (delimiter == null) { throw new IllegalArgumentException("delimiter"); } if (LineDelimiter.AUTO.equals(delimiter)) { throw new IllegalArgumentException("AUTO delimiter is not allowed for encoder."); } this.charset = charset; this.delimiter = delimiter; }
/** * {@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); }
/** * Creates a new instance with the spcified <tt>charset</tt> * and the specified <tt>delimiter</tt>. */ public TcpIpTextLineEncoder(final Charset charset, final LineDelimiter delimiter) { if (charset == null) { throw new IllegalArgumentException("charset"); } if (delimiter == null) { throw new IllegalArgumentException("delimiter"); } if (LineDelimiter.AUTO.equals(delimiter)) { throw new IllegalArgumentException( "AUTO delimiter is not allowed for encoder."); } this.charset = charset; //this.delimiter = delimiter; }
/** * Creates a new instance with the current default {@link Charset} * and the specified <tt>delimiter</tt>. * * @param delimiter The line delimiter to use */ public TextLineDecoder(String delimiter) { this(new LineDelimiter(delimiter)); }
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); }
/** * Creates a new instance with the spcified <tt>charset</tt> * and the specified <tt>delimiter</tt>. */ public TextLineEncoder(Charset charset, LineDelimiter delimiter) { if (charset == null) { throw new NullPointerException("charset"); } if (delimiter == null) { throw new NullPointerException("delimiter"); } if (LineDelimiter.AUTO.equals(delimiter)) { throw new IllegalArgumentException( "AUTO delimiter is not allowed for encoder."); } this.charset = charset; this.delimiter = delimiter; }
/** * Creates a new instance with the current default {@link Charset} * and the specified <tt>delimiter</tt>. * * @param delimiter The line delimiter to use */ public TextLineDecoder(String delimiter) { this(new LineDelimiter(delimiter)); }
/** * {@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); }
/** * 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 TextLineEncoder(Charset charset, LineDelimiter delimiter) { if (charset == null) { throw new IllegalArgumentException("charset"); } if (delimiter == null) { throw new IllegalArgumentException("delimiter"); } if (LineDelimiter.AUTO.equals(delimiter)) { throw new IllegalArgumentException("AUTO delimiter is not allowed for encoder."); } this.charset = charset; this.delimiter = delimiter; }
/** * Creates a new instance with the current default {@link Charset} * and the specified <tt>delimiter</tt>. * * @param delimiter The line delimiter to use */ public TextLineEncoder(String delimiter) { this(new LineDelimiter(delimiter)); }
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 ); }
/** * {@inheritDoc} */ @Override public void decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception { Context ctx = getContext(session); if (LineDelimiter.AUTO.equals(delimiter)) { decodeAuto(ctx, session, in, out); } else { decodeNormal(ctx, session, in, out); } }
/** * Creates a new instance with the spcified <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, String delimiter) { this(charset, new LineDelimiter(delimiter)); }
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); }
/** * {@inheritDoc} */ @Override public void decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception { Context ctx = getContext(session); if (LineDelimiter.AUTO.equals(delimiter)) { decodeAuto(ctx, session, in, out); } else { decodeNormal(ctx, session, in, out); } }
/** * Creates a new instance with the current default {@link Charset} * and the specified <tt>delimiter</tt>. * * @param delimiter The line delimiter to use */ public TextLineEncoder(String delimiter) { this(new LineDelimiter(delimiter)); }