List of usage examples for io.netty.buffer ByteBuf writeInt
public abstract ByteBuf writeInt(int value);
From source file:com.tesora.dve.sql.LargeMaxPktTest.java
License:Open Source License
@Test public void testComQueryMessageContinuationOverlap() throws Exception { int defaultMaxPacket = 0xFFFFFF; int payloadSize = (defaultMaxPacket * 4) + (4 * 40); //four full packets and a little change, divisible by 4. ByteBuf source = Unpooled.buffer(payloadSize, payloadSize); Random rand = new Random(239873L); while (source.isWritable()) source.writeInt(rand.nextInt()); Assert.assertEquals(source.writableBytes(), 0, "Oops, I intended to fill up the source buffer"); ByteBuf dest = Unpooled.buffer(payloadSize); MSPComQueryRequestMessage outboundMessage = MSPComQueryRequestMessage.newMessage(source.array()); Packet.encodeFullMessage((byte) 0, outboundMessage, dest); int lengthOfNonUserdata = 5 + 4 + 4 + 4 + 4; Assert.assertEquals(dest.readableBytes(), payloadSize + lengthOfNonUserdata, "Number of bytes in destination buffer is wrong"); Assert.assertEquals(dest.getUnsignedMedium(0), defaultMaxPacket, "First length should be maximum"); Assert.assertEquals(dest.getByte(3), (byte) 0, "First sequenceID should be zero"); Assert.assertEquals(dest.getByte(4), (byte) MSPComQueryRequestMessage.TYPE_IDENTIFIER, "First byte of payload should be MSPComQueryRequestMessage.TYPE_IDENTIFIER"); ByteBuf sliceFirstPayload = dest.slice(5, (0xFFFFFF - 1)); Assert.assertEquals(sliceFirstPayload, source.slice(0, 0xFFFFFF - 1)); }
From source file:com.tesora.dve.sql.LargeMaxPktTest.java
License:Open Source License
@Test public void testComQueryMessageContinuationExact() throws Exception { int defaultMaxPacket = 0xFFFFFF; int payloadSize = (defaultMaxPacket * 4); //four full packets exactly, requires an empty trailing packet. ByteBuf source = Unpooled.buffer(payloadSize, payloadSize); Random rand = new Random(239873L); while (source.isWritable()) source.writeInt(rand.nextInt()); Assert.assertEquals(source.writableBytes(), 0, "Oops, I intended to fill up the source buffer"); ByteBuf dest = Unpooled.buffer(payloadSize); MSPComQueryRequestMessage outboundMessage = MSPComQueryRequestMessage.newMessage(source.array()); Packet.encodeFullMessage((byte) 0, outboundMessage, dest); int lengthOfNonUserdata = 5 + 4 + 4 + 4 + 4;//last packet has zero length payload Assert.assertEquals(dest.readableBytes(), payloadSize + lengthOfNonUserdata, "Number of bytes in destination buffer is wrong"); Assert.assertEquals(dest.getUnsignedMedium(0), defaultMaxPacket, "First length should be maximum"); Assert.assertEquals(dest.getByte(3), (byte) 0, "First sequenceID should be zero"); Assert.assertEquals(dest.getByte(4), (byte) MSPComQueryRequestMessage.TYPE_IDENTIFIER, "First byte of payload should be MSPComQueryRequestMessage.TYPE_IDENTIFIER"); ByteBuf sliceFirstPayload = dest.slice(5, (0xFFFFFF - 1)); Assert.assertEquals(sliceFirstPayload, source.slice(0, 0xFFFFFF - 1)); }
From source file:com.theoriginalbit.moarperipherals.common.network.message.MessageGeneric.java
License:Apache License
@Override public void toBytes(ByteBuf buf) { // Write any string data if (stringData != null) { buf.writeInt(stringData.length); for (String str : stringData) { ByteBufUtils.writeUTF8String(buf, str); }//from w ww .j av a2 s .c o m } else { buf.writeInt(0); } // Write any integer data if (intData != null) { buf.writeInt(intData.length); for (int i : intData) { buf.writeInt(i); } } else { buf.writeInt(0); } // Write any byte data if (byteData != null) { buf.writeInt(byteData.length); buf.writeBytes(byteData); } else { buf.writeInt(0); } // Write any char data if (charData != null) { buf.writeInt(charData.length); for (char c : charData) { buf.writeChar(c); } } else { buf.writeInt(0); } // Write any float data if (floatData != null) { buf.writeInt(floatData.length); for (float f : floatData) { buf.writeFloat(f); } } else { buf.writeInt(0); } // Write any double data if (doubleData != null) { buf.writeInt(doubleData.length); for (double d : doubleData) { buf.writeDouble(d); } } else { buf.writeInt(0); } // Write any NBT data if (nbtData != null) { buf.writeBoolean(true); ByteBufUtils.writeTag(buf, nbtData); } else { buf.writeBoolean(false); } }
From source file:com.torchmind.netty.msgpack.codec.MessageFrameCodec.java
License:Apache License
/** * {@inheritDoc}//www. j a va 2 s .c om */ @Override protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception { // ensure buffer is writable out.ensureWritable((msg.readableBytes() + 4)); // write length out.writeInt(msg.readableBytes()); // write data out.writeBytes(msg); }
From source file:com.torchmind.netty.msgpack.test.MessageFrameCodecTest.java
License:Apache License
/** * Tests encoding.//w w w . j av a 2 s .c om */ @Test public void encoding() { // create a test message ByteBuf message = this.channel.alloc().buffer(); message.writeInt(42); // write data Assert.assertTrue(this.channel.writeOutbound(message)); // grab output ByteBuf output = this.channel.readOutbound(); // verify packet Assert.assertEquals("Overall packet size does not match", 8, output.readableBytes()); Assert.assertEquals("Packet #0: Packet length header does not match", 4, output.readInt()); Assert.assertEquals("Packet #0: Packet body does not match", 42, output.readInt()); }
From source file:com.torchmind.netty.msgpack.test.MessageFrameCodecTest.java
License:Apache License
/** * Tests decoding./* ww w . j a va 2 s . c o m*/ */ @Test public void decoding() { // create a test message ByteBuf message = this.channel.alloc().buffer(); message.writeInt(8); message.writeInt(42); message.writeInt(21); message.writeInt(8); message.writeInt(42); message.writeInt(21); // write data Assert.assertTrue(this.channel.writeInbound(message)); // initialize count int i = 0; // iterate over elements in stream while (this.channel.inboundMessages().peek() != null) { ByteBuf input = ((ByteBuf) this.channel.inboundMessages().poll()); // check length Assert.assertEquals("Packet #" + i + ": Decoded packet size does not match", 8, input.readableBytes()); Assert.assertEquals("Packet #" + i + ": First field does not match", 42, input.readInt()); Assert.assertEquals("Packet #" + i + ": Second field does not match", 21, input.readInt()); // update count i++; } }
From source file:com.turn.ttorrent.client.io.PeerMessageCodec.java
License:Apache License
@Override protected void encode(ChannelHandlerContext ctx, PeerMessage value, ByteBuf out) throws Exception { // LOG.info("encode: " + value); int lengthIndex = out.writerIndex(); out.writeInt(0); int startIndex = out.writerIndex(); value.toWire(out, listener.getExtendedMessageTypes()); out.setInt(lengthIndex, out.writerIndex() - startIndex); }
From source file:com.turn.ttorrent.common.protocol.udp.UDPAnnounceRequestMessage.java
License:Apache License
@Override public void toWire(ByteBuf out) { _toWire(out);//w ww. j a v a2s. c om out.writeBytes(infoHash); out.writeBytes(getPeerId()); out.writeLong(downloaded); out.writeLong(uploaded); out.writeLong(left); out.writeInt(event.getId()); out.writeBytes(getIp4Address(peerAddress)); out.writeInt(key); out.writeInt(numWant); out.writeShort(peerAddress.getPort()); }
From source file:com.turn.ttorrent.common.protocol.udp.UDPAnnounceResponseMessage.java
License:Apache License
@Override public void toWire(ByteBuf out) { _toWire(out);/*w w w .j ava2 s . c om*/ out.writeInt(interval); /** * Leechers (incomplete) are first, before seeders (complete) in the packet. */ out.writeInt(incomplete); out.writeInt(complete); for (Peer peer : peers) { byte[] ip = peer.getIpBytes(); if (ip == null || ip.length != 4) continue; out.writeBytes(ip); out.writeShort((short) peer.getPort()); } }
From source file:com.twitter.http2.HttpFrameDecoderTest.java
License:Apache License
@Test public void testHttpRstStreamFrame() throws Exception { int length = 4; byte flags = 0; int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01; int errorCode = RANDOM.nextInt(); ByteBuf frame = rstStreamFrame(length, flags, streamId); frame.writeInt(errorCode); decoder.decode(frame);// w ww . j av a 2s .c o m verify(delegate).readRstStreamFrame(streamId, errorCode); verifyNoMoreInteractions(delegate); }