List of usage examples for io.netty.buffer ByteBuf writeBytes
public abstract ByteBuf writeBytes(ByteBuffer src);
From source file:com.mobius.software.mqtt.parser.test.TestParser.java
License:Open Source License
@Test public void testNextContentIncompleteBufferNotReset() throws MalformedMessageException { ByteBuf buf = Unpooled.buffer(2120207); buf.writeByte(0x82);//from www .j a v a2s .c om buf.writeByte(0x8a); buf.writeByte(0xB4); buf.writeByte(0x81); buf.writeByte(0x01); buf.writeBytes(new byte[2120201]); // one byte missing in content try { MQParser.next(buf); } catch (MalformedMessageException e) { } assertEquals("buffer index was not reset", 0, buf.readerIndex()); }
From source file:com.mobius.software.mqtt.parser.test.TestParser.java
License:Open Source License
@Test public void testDecodeLengthOne() { ByteBuf buf = Unpooled.buffer(114); buf.writeByte(0x82);/*from w w w .ja v a 2s.co m*/ buf.writeByte(0x66); buf.writeShort(10); buf.writeShort(97); buf.writeBytes(new byte[96]); buf.writeByte(1); buf.writeByte(0); }
From source file:com.mobius.software.mqtt.parser.test.TestParser.java
License:Open Source License
@Test public void testDecodeLengthTwo() { ByteBuf buf = Unpooled.buffer(205); buf.writeByte(0x82);//w w w .j a v a 2 s . co m buf.writeByte(0xCA); buf.writeByte(0x01); buf.writeShort(10); buf.writeShort(197); buf.writeBytes(new byte[196]); buf.writeByte(1); buf.writeByte(0); }
From source file:com.mobius.software.mqtt.parser.test.TestParser.java
License:Open Source License
@Test public void testDecodeLengthThree() { ByteBuf buf = Unpooled.buffer(20206); buf.writeByte(0x82);//from w w w .java2s.co m buf.writeByte(0xEA); buf.writeByte(0x9D); buf.writeByte(0x01); buf.writeShort(10); buf.writeShort(20197); buf.writeBytes(new byte[20196]); buf.writeByte(1); buf.writeByte(0); }
From source file:com.mobius.software.mqtt.parser.test.TestParser.java
License:Open Source License
@Test public void testDecodeLengthFour() { ByteBuf buf = Unpooled.buffer(2097159); buf.writeByte(0x82);//from w ww . j a va2 s . c om buf.writeByte(0x82); buf.writeByte(0x80); buf.writeByte(0x80); buf.writeByte(0x01); buf.writeShort(10); for (int i = 0; i < 32; i++) { buf.writeShort(65533); buf.writeBytes(new byte[65532]); buf.writeByte(1); buf.writeByte(0); } }
From source file:com.mobius.software.mqtt.performance.controller.net.Encoder.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext ctx, MQMessage msg, ByteBuf out) throws Exception { out.writeBytes(MQParser.encode(msg)); }
From source file:com.mpush.api.protocol.Packet.java
License:Apache License
public static void encodePacket(Packet packet, ByteBuf out) { if (packet.cmd == Command.HEARTBEAT.cmd) { out.writeByte(Packet.HB_PACKET_BYTE); } else {//from ww w.j ava 2 s.co m out.writeInt(packet.getBodyLength()); out.writeByte(packet.cmd); out.writeShort(packet.cc); out.writeByte(packet.flags); out.writeInt(packet.sessionId); out.writeByte(packet.lrc); if (packet.getBodyLength() > 0) { out.writeBytes(packet.body); } } packet.body = null; }
From source file:com.mycompany.device.FFDevice.java
public FFDevice(SocketChannel ch) { this.req = null; this.soc = ch; this.data_rcv = soc.alloc().buffer(512); this.reg_str = ""; this.connect_time = System.currentTimeMillis(); ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new IdleStateHandler(0, 0, idle_time_interval_s)); pipeline.addLast(new MessageToByteEncoder<byte[]>() { @Override// w w w. j a va 2s .com protected void encode(ChannelHandlerContext ctx, byte[] msg, ByteBuf out) throws Exception { // TODO Auto-generated method stub out.writeBytes(msg); } }); pipeline.addLast(new ChannelInboundHandlerAdapter() { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { // TODO Auto-generated method stub ByteBuf bb = (ByteBuf) msg; if (reg_str.equals("")) { reg_str = bb.toString(Charset.defaultCharset()); FFServer.logger.info(String.format("device that has regs %s is registed", reg_str)); } else { FFServer.logger.debug(String.format("%s receive: %d bytes", reg_str, bb.readableBytes())); data_rcv.writeBytes(bb); } ReferenceCountUtil.release(msg); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { // TODO Auto-generated method stub FFServer.logger.error(cause); Close(); } @Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { if (evt instanceof IdleStateEvent) { IdleStateEvent event = (IdleStateEvent) evt; if (event.state() == IdleState.ALL_IDLE) { FFServer.logger.info(String.format("%s in idle state", reg_str)); ByteBuf hb = ctx.alloc().buffer(1).writeByte('.'); Send(hb); } } } }); ChannelFuture f = soc.closeFuture(); f.addListener((ChannelFutureListener) new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { is_closed = true; FFServer.logger.info(String.format("%s disconnected", reg_str)); } }); }
From source file:com.nanxiaoqiang.test.netty.protocol.demo1.codec.NettyMessageEncoder.java
License:Apache License
@Override protected void encode(ChannelHandlerContext ctx, NettyMessage msg, ByteBuf sendBuf) throws Exception { if (msg == null || msg.getHeader() == null) throw new Exception("The encode message is null"); // Header Start sendBuf.writeInt((msg.getHeader().getCrcCode()));// crc sendBuf.writeInt((msg.getHeader().getLength()));// ? sendBuf.writeLong((msg.getHeader().getSessionID())); sendBuf.writeByte((msg.getHeader().getType())); sendBuf.writeByte((msg.getHeader().getPriority())); sendBuf.writeInt((msg.getHeader().getAttachment().size())); String key = null;//from www . j a va 2s . co m byte[] keyArray = null; Object value = null; for (Map.Entry<String, Object> param : msg.getHeader().getAttachment().entrySet()) { key = param.getKey(); keyArray = key.getBytes("UTF-8"); sendBuf.writeInt(keyArray.length); sendBuf.writeBytes(keyArray); value = param.getValue(); marshallingEncoder.encode(value, sendBuf); } key = null; keyArray = null; value = null; // Head End // Body Start if (msg.getBody() != null) {// ? marshallingEncoder.encode(msg.getBody(), sendBuf); } else // ? sendBuf.writeInt(0);// 0int sendBuf.setInt(4, sendBuf.readableBytes() - 8);// 0~3CRC4~7??8? }
From source file:com.navercorp.nbasearc.gcp.Pipeline.java
License:Apache License
ByteBuf aggregate(ByteBufAllocator allocator) { if (requests.isEmpty()) { return null; }//from ww w. j av a 2 s . co m /* aggregate */ ByteBuf buf = allocator.ioBuffer(INITIAL_CAPACITY); while (true) { Request rqst = requests.poll(); if (rqst == null) { break; } rqst.setState(Request.State.SENT); buf.writeBytes(rqst.getCommand()); sent.add(rqst); if (SOCKET_BUFFER_SIZE <= buf.readableBytes()) { break; } } return buf; }