Example usage for io.netty.buffer PooledByteBufAllocator DEFAULT

List of usage examples for io.netty.buffer PooledByteBufAllocator DEFAULT

Introduction

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

Prototype

PooledByteBufAllocator DEFAULT

To view the source code for io.netty.buffer PooledByteBufAllocator DEFAULT.

Click Source Link

Usage

From source file:org.apache.bookkeeper.util.ByteBufListTest.java

License:Apache License

@Test
public void testSingle() throws Exception {
    ByteBuf b1 = PooledByteBufAllocator.DEFAULT.heapBuffer(128, 128);
    b1.writerIndex(b1.capacity());/*  w w  w.j  av  a 2s. c  o m*/
    ByteBufList buf = ByteBufList.get(b1);

    assertEquals(1, buf.size());
    assertEquals(128, buf.readableBytes());
    assertEquals(b1, buf.getBuffer(0));

    assertEquals(buf.refCnt(), 1);
    assertEquals(b1.refCnt(), 1);

    buf.release();

    assertEquals(buf.refCnt(), 0);
    assertEquals(b1.refCnt(), 0);
}

From source file:org.apache.bookkeeper.util.ByteBufListTest.java

License:Apache License

@Test
public void testDouble() throws Exception {
    ByteBuf b1 = PooledByteBufAllocator.DEFAULT.heapBuffer(128, 128);
    b1.writerIndex(b1.capacity());//from   ww  w  . j a  va  2  s.  co m
    ByteBuf b2 = PooledByteBufAllocator.DEFAULT.heapBuffer(128, 128);
    b2.writerIndex(b2.capacity());
    ByteBufList buf = ByteBufList.get(b1, b2);

    assertEquals(2, buf.size());
    assertEquals(256, buf.readableBytes());
    assertEquals(b1, buf.getBuffer(0));
    assertEquals(b2, buf.getBuffer(1));

    assertEquals(buf.refCnt(), 1);
    assertEquals(b1.refCnt(), 1);
    assertEquals(b2.refCnt(), 1);

    buf.release();

    assertEquals(buf.refCnt(), 0);
    assertEquals(b1.refCnt(), 0);
    assertEquals(b2.refCnt(), 0);
}

From source file:org.apache.bookkeeper.util.ByteBufListTest.java

License:Apache License

@Test
public void testClone() throws Exception {
    ByteBuf b1 = PooledByteBufAllocator.DEFAULT.heapBuffer(128, 128);
    b1.writerIndex(b1.capacity());/*from  w  ww.j  a va  2  s .  c  om*/
    ByteBuf b2 = PooledByteBufAllocator.DEFAULT.heapBuffer(128, 128);
    b2.writerIndex(b2.capacity());
    ByteBufList buf = ByteBufList.get(b1, b2);

    ByteBufList clone = ByteBufList.clone(buf);

    assertEquals(2, buf.size());
    assertEquals(256, buf.readableBytes());
    assertEquals(b1, buf.getBuffer(0));
    assertEquals(b2, buf.getBuffer(1));

    assertEquals(2, clone.size());
    assertEquals(256, clone.readableBytes());
    assertEquals(b1, clone.getBuffer(0));
    assertEquals(b2, clone.getBuffer(1));

    assertEquals(buf.refCnt(), 1);
    assertEquals(clone.refCnt(), 1);
    assertEquals(b1.refCnt(), 2);
    assertEquals(b2.refCnt(), 2);

    buf.release();

    assertEquals(buf.refCnt(), 0);
    assertEquals(clone.refCnt(), 1);
    assertEquals(b1.refCnt(), 1);
    assertEquals(b2.refCnt(), 1);

    clone.release();

    assertEquals(buf.refCnt(), 0);
    assertEquals(clone.refCnt(), 0);
    assertEquals(b1.refCnt(), 0);
    assertEquals(b2.refCnt(), 0);
}

From source file:org.apache.bookkeeper.util.ByteBufListTest.java

License:Apache License

@Test
public void testRetain() throws Exception {
    ByteBuf b1 = PooledByteBufAllocator.DEFAULT.heapBuffer(128, 128);
    b1.writerIndex(b1.capacity());/*from w w  w .  j  a  v a2 s.c  o  m*/
    ByteBufList buf = ByteBufList.get(b1);

    assertEquals(1, buf.size());
    assertEquals(128, buf.readableBytes());
    assertEquals(b1, buf.getBuffer(0));

    assertEquals(buf.refCnt(), 1);
    assertEquals(b1.refCnt(), 1);

    buf.retain();

    assertEquals(buf.refCnt(), 2);
    assertEquals(b1.refCnt(), 1);

    buf.release();

    assertEquals(buf.refCnt(), 1);
    assertEquals(b1.refCnt(), 1);

    buf.release();

    assertEquals(buf.refCnt(), 0);
    assertEquals(b1.refCnt(), 0);
}

From source file:org.apache.bookkeeper.util.ByteBufListTest.java

License:Apache License

@Test
public void testEncoder() throws Exception {
    ByteBuf b1 = PooledByteBufAllocator.DEFAULT.heapBuffer(128, 128);
    b1.writerIndex(b1.capacity());//from   w  w  w .j a v a 2s .c  o m
    ByteBuf b2 = PooledByteBufAllocator.DEFAULT.heapBuffer(128, 128);
    b2.writerIndex(b2.capacity());
    ByteBufList buf = ByteBufList.get(b1, b2);

    ChannelHandlerContext ctx = new MockChannelHandlerContext();

    ByteBufList.ENCODER.write(ctx, buf, null);

    assertEquals(buf.refCnt(), 0);
    assertEquals(b1.refCnt(), 0);
    assertEquals(b2.refCnt(), 0);
}

From source file:org.apache.camel.component.netty4.NettyConverterTest.java

License:Apache License

@Before
public void startUp() {
    byte[] bytes = PAYLOAD.getBytes();
    buf = PooledByteBufAllocator.DEFAULT.buffer(bytes.length);
    buf.writeBytes(bytes);
}

From source file:org.apache.distributedlog.EnvelopedEntryWriter.java

License:Apache License

EnvelopedEntryWriter(String logName, int initialBufferSize, boolean envelopeBeforeTransmit,
        CompressionCodec.Type codec) {
    this.logName = logName;
    this.buffer = PooledByteBufAllocator.DEFAULT.buffer(
            Math.min(Math.max(initialBufferSize * 6 / 5, HEADER_LENGTH), MAX_LOGRECORDSET_SIZE),
            MAX_LOGRECORDSET_SIZE);/*from www .ja v a 2s. c  o m*/
    this.writer = new LogRecord.Writer(buffer);
    this.writeRequests = new LinkedList<WriteRequest>();
    this.envelopeBeforeTransmit = envelopeBeforeTransmit;
    this.codec = codec;
    this.flags = codec.code() & COMPRESSION_CODEC_MASK;
    if (envelopeBeforeTransmit) {
        this.buffer.writerIndex(HEADER_LENGTH);
    }
}

From source file:org.apache.distributedlog.EnvelopedRecordSetWriter.java

License:Apache License

EnvelopedRecordSetWriter(int initialBufferSize, CompressionCodec.Type codec) {
    this.buffer = PooledByteBufAllocator.DEFAULT.buffer(Math.max(initialBufferSize, HEADER_LEN),
            MAX_LOGRECORDSET_SIZE);/*from   ww w  .  j a  v a2 s.  c  o m*/
    this.promiseList = new LinkedList<CompletableFuture<DLSN>>();
    this.codec = codec;
    this.codecCode = codec.code();
    this.metadata = (VERSION & METADATA_VERSION_MASK) | (codecCode & METADATA_COMPRESSION_MASK);
    this.buffer.writeInt(metadata);
    this.buffer.writeInt(0); // count
    this.buffer.writeInt(0); // original len
    this.buffer.writeInt(0); // actual len
}

From source file:org.apache.distributedlog.io.IdentityCompressionCodec.java

License:Apache License

@Override
public ByteBuf compress(ByteBuf uncompressed, int headerLen) {
    checkNotNull(uncompressed);//from w  ww  .  jav a 2s .c o  m
    checkArgument(uncompressed.readableBytes() >= 0);
    if (headerLen == 0) {
        return uncompressed.retain();
    } else {
        CompositeByteBuf composited = PooledByteBufAllocator.DEFAULT.compositeBuffer(2);
        composited.addComponent(PooledByteBufAllocator.DEFAULT.buffer(headerLen, headerLen));
        composited.addComponent(uncompressed.retain());
        return composited;
    }
}

From source file:org.apache.distributedlog.io.LZ4CompressionCodec.java

License:Apache License

@Override
public ByteBuf compress(ByteBuf uncompressed, int headerLen) {
    checkNotNull(uncompressed);/*from   www .j  ava  2 s.c  om*/
    checkArgument(uncompressed.readableBytes() > 0);

    int uncompressedLen = uncompressed.readableBytes();
    int maxLen = compressor.maxCompressedLength(uncompressedLen);

    // get the source bytebuffer
    ByteBuffer uncompressedNio = uncompressed.nioBuffer(uncompressed.readerIndex(), uncompressedLen);
    ByteBuf compressed = PooledByteBufAllocator.DEFAULT.buffer(maxLen + headerLen, maxLen + headerLen);
    ByteBuffer compressedNio = compressed.nioBuffer(headerLen, maxLen);

    int compressedLen = compressor.compress(uncompressedNio, uncompressedNio.position(), uncompressedLen,
            compressedNio, compressedNio.position(), maxLen);
    compressed.writerIndex(compressedLen + headerLen);

    return compressed;
}