Example usage for io.netty.buffer Unpooled buffer

List of usage examples for io.netty.buffer Unpooled buffer

Introduction

In this page you can find the example usage for io.netty.buffer Unpooled buffer.

Prototype

public static ByteBuf buffer(int initialCapacity, int maxCapacity) 

Source Link

Document

Creates a new big-endian Java heap buffer with the specified initialCapacity , that may grow up to maxCapacity The new buffer's readerIndex and writerIndex are 0 .

Usage

From source file:com.tesora.dve.db.mysql.portal.protocol.Packet.java

License:Open Source License

public Packet(ByteBufAllocator alloc, int expectedSequence, Modifier mod, String context) {
    this.expectedSequence = expectedSequence;
    this.modifier = mod;
    this.context = context;
    if (modifier == Modifier.HEAPCOPY_ON_READ) {
        header = Unpooled.buffer(4, 4).order(ByteOrder.LITTLE_ENDIAN);
        payload = Unpooled.compositeBuffer(50);
    } else {//from  w w w .j av a2  s.co  m
        header = alloc.ioBuffer(4, 4).order(ByteOrder.LITTLE_ENDIAN);
        payload = alloc.compositeBuffer(50);
    }
}

From source file:com.tesora.dve.db.mysql.portal.protocol.Packet.java

License:Open Source License

public ByteBuf unwrapPayload() {
    if (modifier == Modifier.HEAPCOPY_ON_READ) {
        int maxCapacity = payload.readableBytes();
        ByteBuf copy = Unpooled.buffer(maxCapacity, maxCapacity);
        copy.writeBytes(payload);//from   www .  ja va2 s. c o m
        return copy;
    } else
        return payload;
}

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.yahoo.pulsar.client.impl.MessageIdImpl.java

License:Apache License

protected byte[] toByteArray(int batchIndex) {
    MessageIdData.Builder builder = MessageIdData.newBuilder();
    builder.setLedgerId(ledgerId);//from  w ww. j  a va 2 s  .c  om
    builder.setEntryId(entryId);
    if (partitionIndex >= 0) {
        builder.setPartition(partitionIndex);
    }

    if (batchIndex != -1) {
        builder.setBatchIndex(batchIndex);
    }

    MessageIdData msgId = builder.build();
    int size = msgId.getSerializedSize();
    ByteBuf serialized = Unpooled.buffer(size, size);
    ByteBufCodedOutputStream stream = ByteBufCodedOutputStream.get(serialized);
    try {
        msgId.writeTo(stream);
    } catch (IOException e) {
        // This is in-memory serialization, should not fail
        throw new RuntimeException(e);
    }

    msgId.recycle();
    builder.recycle();
    stream.recycle();
    return serialized.array();
}