Java Code Examples for org.apache.mina.core.buffer.IoBuffer#putString()
The following examples show how to use
org.apache.mina.core.buffer.IoBuffer#putString() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MyTextLineCodecEncoder.java From java-study with Apache License 2.0 | 6 votes |
public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception { logger.info("开始进入编码方法-----------------------------------------------------------------"); // 如果文本换行符未指定,使用默认值 if (delimiter == null || "".equals(delimiter)) { delimiter = "\r\n"; } if (charset == null) { charset = Charset.forName("utf-8"); } String value = message.toString(); IoBuffer buf = IoBuffer.allocate(value.length()).setAutoExpand(true); //真实数据 buf.putString(value, charset.newEncoder()); //文本换行符 buf.putString(delimiter, charset.newEncoder()); buf.flip(); out.write(buf); }
Example 2
Source File: HttpServerEncoderImpl.java From game-server with MIT License | 6 votes |
/** {@inheritDoc} */ public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception { LOG.debug("encode {}", message.getClass().getCanonicalName()); if ((message instanceof HttpResponseImpl)) { HttpResponseImpl msg = (HttpResponseImpl) message; IoBuffer buf = IoBuffer.allocate(128).setAutoExpand(true); buf.putString(msg.getStatus().line(), ENCODER); for (Map.Entry<String, String> header : msg.getHeaders().entrySet()) { buf.putString((CharSequence) header.getKey(), ENCODER); buf.putString(": ", ENCODER); buf.putString((CharSequence) header.getValue(), ENCODER); buf.put(CRLF); } if (msg.getBody() != null) { buf.putString(CONTENTLENGTH, ENCODER); buf.putString(String.valueOf(msg.getBody().length), ENCODER); buf.put(CRLF); } buf.put(CRLF); if (msg.getBody() != null) { buf.put(msg.getBody()); } buf.flip(); out.write(buf); } }
Example 3
Source File: MessageChannelCodecFilter.java From neoscada with Eclipse Public License 1.0 | 6 votes |
private IoBuffer encodeProperties ( final IoSession session, final Map<String, String> properties ) throws CharacterCodingException { final IoBuffer data = IoBuffer.allocate ( 0 ); data.setAutoExpand ( true ); data.putInt ( properties.size () ); final CharsetEncoder encoder = getCharsetEncoder ( session ); for ( final Map.Entry<String, String> entry : properties.entrySet () ) { final String key = entry.getKey (); final String value = entry.getValue (); data.putString ( key, encoder ); data.put ( (byte)0x00 ); data.putString ( value, encoder ); data.put ( (byte)0x00 ); } data.flip (); return data; }
Example 4
Source File: TextLineEncoder.java From neoscada with Eclipse Public License 1.0 | 6 votes |
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); }
Example 5
Source File: FixedLengthStringAccessor.java From neoscada with Eclipse Public License 1.0 | 6 votes |
@Override public void put ( final IoBuffer data, final String value ) { if ( value == null ) { data.put ( (byte)0x00 ); } try { data.putString ( value, this.length, this.charset.newEncoder () ); } catch ( final CharacterCodingException e ) { throw new RuntimeException ( e ); } }
Example 6
Source File: ServerProtocolEncoder.java From seed with Apache License 2.0 | 5 votes |
@Override public void encode(IoSession session, String message, ProtocolEncoderOutput out) throws Exception { IoBuffer buffer = IoBuffer.allocate(100).setAutoExpand(true); buffer.putString(message, Charset.forName(SeedConstants.DEFAULT_CHARSET).newEncoder()); buffer.flip(); out.write(buffer); }
Example 7
Source File: MinaUtil.java From seed with Apache License 2.0 | 5 votes |
@Override public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception { IoBuffer buffer = IoBuffer.allocate(100).setAutoExpand(true); //二者等效:buffer.put(message.toString().getBytes(charset)) buffer.putString(message.toString(), Charset.forName(charset).newEncoder()); buffer.flip(); out.write(buffer); }
Example 8
Source File: MessageChannelCodecFilter.java From neoscada with Eclipse Public License 1.0 | 5 votes |
private Frame encodeCloseMessage ( final IoSession session, final CloseMessage message ) throws CharacterCodingException { final IoBuffer data = IoBuffer.allocate ( 0 ); data.setAutoExpand ( true ); data.putString ( message.getMessage (), getCharsetEncoder ( session ) ); data.put ( (byte)0x00 ); data.putInt ( message.getCode () ); data.flip (); return new Frame ( FrameType.CLOSE, data ); }
Example 9
Source File: TextLineDecoder.java From neoscada with Eclipse Public License 1.0 | 5 votes |
/** * Creates a new instance with the specified <tt>charset</tt> * and the specified <tt>delimiter</tt>. */ 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; } }
Example 10
Source File: NIOConnection.java From Openfire with Apache License 2.0 | 5 votes |
@Override public void deliver(Packet packet) throws UnauthorizedException { if (isClosed()) { backupDeliverer.deliver(packet); } else { boolean errorDelivering = false; IoBuffer buffer = IoBuffer.allocate(4096); buffer.setAutoExpand(true); try { buffer.putString(packet.getElement().asXML(), encoder.get()); if (flashClient) { buffer.put((byte) '\0'); } buffer.flip(); ioSessionLock.lock(); try { ioSession.write(buffer); } finally { ioSessionLock.unlock(); } } catch (Exception e) { Log.debug("Error delivering packet:\n" + packet, e); errorDelivering = true; } if (errorDelivering) { close(); // Retry sending the packet again. Most probably if the packet is a // Message it will be stored offline backupDeliverer.deliver(packet); } else { session.incrementServerPacketCount(); } } }
Example 11
Source File: MyTextLineCodecDecoder.java From java-study with Apache License 2.0 | 4 votes |
private void decodeNormal(Context ctx, IoBuffer in, ProtocolDecoderOutput out) throws CharacterCodingException { // 取出未完成任务中已经匹配的文本换行符的个数 int matchCount = ctx.getMatchCount(); // 设置匹配文本换行符的IoBuffer变量 if (delimBuf == null) { IoBuffer tmp = IoBuffer.allocate(2).setAutoExpand(true); tmp.putString(delimiter, charset.newEncoder()); tmp.flip(); delimBuf = tmp; } //解码的IoBuffer中数据的原始信息 int oldPos = in.position(); //输出值为0 int oldLimit = in.limit(); //输出值为1 logger.info("******************************************************************************"); logger.info("开始进入解码方法-----------------------------------------------------------------"); logger.info(""); logger.info("init Start--------------------------------------------------------------------"); logger.info("in.postion() = "+oldPos); logger.info("in.Limit() = "+oldLimit); logger.info("in.capacity() = "+in.capacity()); logger.info("matchCount = "+matchCount); logger.info("init End---------------------------------------------------------------------"); logger.info(""); //变量解码的IoBuffer while (in.hasRemaining()) { byte b = in.get(); logger.info(""); logger.info("输入进来的字符为 = "+(char)b+",对应的ascii值 = "+b); logger.info("in.position() = "+in.position()+",in.limit() = "+in.limit()); logger.info(""); //当b的ascii值为13,10 即为\r,\n时,会进入下述if语句 if (delimBuf.get(matchCount) == b) { // b='\r'时,matchCount=1, b='\n'时,matchCount=2 matchCount++; logger.info("matchCount = "+matchCount); //当前匹配到字节个数与文本换行符字节个数相同,即 b='\n'时 //此时matchCount=2, delimBuf.limit()=2 if (matchCount == delimBuf.limit()) { // 获得当前匹配到的position(position前所有数据有效) int pos = in.position(); //值为2 logger.info("pos = "+pos); in.limit(pos); //值为2 // position回到原始位置 in.position(oldPos); //值为0 // 追加到Context对象未完成数据后面 ctx.append(in); //将 \r\n这两个字符添加到 ctx.getBuf()中 // in中匹配结束后剩余数据 in.limit(oldLimit); //值为2 in.position(pos); //值为2 IoBuffer buf = ctx.getBuf(); //此时是得到 he\r\n buf.flip(); //此时 buf.position=0,buf.limit()=4 buf.limit(buf.limit() - matchCount); //4-2 = 2 try{ // 输出解码内容 ,即 he out.write(buf.getString(ctx.getDecoder())); } finally { buf.clear(); // 释放缓存空间 } matchCount = 0; } }else { //h字符,e字符时,均会进入 此else逻辑判断中 //把in中未解码内容放回buf中 //下面会在 输入的字符不是 \r\n时会需要保存使用 in.position(oldPos); ctx.append(in); ctx.setMatchCount(matchCount); } } }