Example usage for io.netty.buffer ByteBuf release

List of usage examples for io.netty.buffer ByteBuf release

Introduction

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

Prototype

boolean release();

Source Link

Document

Decreases the reference count by 1 and deallocates this object if the reference count reaches at 0 .

Usage

From source file:com.couchbase.client.core.util.HttpUtils.java

License:Open Source License

/**
 * Add the HTTP basic auth headers to a netty request.
 *
 * @param request the request to modify.
 * @param user the user of the request.//from www. j a v a 2s.  co  m
 * @param password the password of the request.
 */
public static void addAuth(final HttpRequest request, final String user, final String password) {
    ByteBuf rawBuf = Unpooled.copiedBuffer(user + ":" + password, CharsetUtil.UTF_8);
    try {
        ByteBuf encoded = Base64.encode(rawBuf);
        request.headers().add(HttpHeaders.Names.AUTHORIZATION, encoded.toString(CharsetUtil.UTF_8));
    } finally {
        rawBuf.release();
    }
}

From source file:com.couchbase.client.core.utils.BuffersTest.java

License:Apache License

@Test
public void shouldReleaseIfUnsubscribed() {
    ReplaySubject<ByteBuf> source = ReplaySubject.create();
    Observable<ByteBuf> wrapped = Buffers.wrapColdWithAutoRelease(source);

    for (int i = 0; i < 5; i++) {
        source.onNext(Unpooled.buffer());
    }/*  w  ww .  j a  v  a  2 s .co m*/
    source.onCompleted();

    source.toBlocking().forEach(new Action1<ByteBuf>() {
        @Override
        public void call(ByteBuf byteBuf) {
            assertEquals(1, byteBuf.refCnt());
        }
    });

    wrapped.take(2).toBlocking().forEach(new Action1<ByteBuf>() {
        @Override
        public void call(ByteBuf byteBuf) {
            byteBuf.release();
        }
    });

    source.toBlocking().forEach(new Action1<ByteBuf>() {
        @Override
        public void call(ByteBuf byteBuf) {
            assertEquals(0, byteBuf.refCnt());
        }
    });

}

From source file:com.couchbase.client.core.utils.UnicastAutoReleaseSubjectTest.java

License:Apache License

@Test
public void testByteBufReleaseWithNoTimeout() throws Exception {
    UnicastAutoReleaseSubject<ByteBuf> subject = UnicastAutoReleaseSubject.createWithoutNoSubscriptionTimeout();
    ByteBuf buffer = Unpooled.buffer();/*from ww w  . jav a2 s.  co m*/
    Assert.assertEquals("Created byte buffer not retained.", 1, buffer.refCnt());
    subject.onNext(buffer);
    subject.onCompleted();
    final AtomicInteger byteBufRefCnt = new AtomicInteger(-1);

    ByteBuf last = subject.doOnNext(new Action1<ByteBuf>() {
        @Override
        public void call(ByteBuf byteBuf) {
            byteBufRefCnt.set(byteBuf.refCnt());
            byteBuf.release();// Simulate consumption as ByteBuf refcount is 1 when created.
        }
    }).toBlocking().last();

    Assert.assertEquals("Unexpected ByteBuf ref count when received.", 1, byteBufRefCnt.get());
    Assert.assertSame("Unexpected byte buffer received.", buffer, last);
    Assert.assertEquals("Byte buffer not released.", 0, last.refCnt());
}

From source file:com.datastax.driver.core.LZ4Compressor.java

License:Apache License

private ByteBuf compressDirect(ByteBuf input) throws IOException {
    int maxCompressedLength = compressor.maxCompressedLength(input.readableBytes());
    // If the input is direct we will allocate a direct output buffer as well as this will allow us to use
    // LZ4Compressor.compress and so eliminate memory copies.
    ByteBuf output = input.alloc().directBuffer(INTEGER_BYTES + maxCompressedLength);
    try {//w  w w. j av a  2 s.c o m
        ByteBuffer in = inputNioBuffer(input);
        // Increase reader index.
        input.readerIndex(input.writerIndex());

        output.writeInt(in.remaining());

        ByteBuffer out = outputNioBuffer(output);
        int written = compressor.compress(in, in.position(), in.remaining(), out, out.position(),
                out.remaining());
        // Set the writer index so the amount of written bytes is reflected
        output.writerIndex(output.writerIndex() + written);
    } catch (Exception e) {
        // release output buffer so we not leak and rethrow exception.
        output.release();
        throw new IOException(e);
    }
    return output;
}

From source file:com.datastax.driver.core.LZ4Compressor.java

License:Apache License

private ByteBuf compressHeap(ByteBuf input) throws IOException {
    int maxCompressedLength = compressor.maxCompressedLength(input.readableBytes());

    // Not a direct buffer so use byte arrays...
    int inOffset = input.arrayOffset() + input.readerIndex();
    byte[] in = input.array();
    int len = input.readableBytes();
    // Increase reader index.
    input.readerIndex(input.writerIndex());

    // Allocate a heap buffer from the ByteBufAllocator as we may use a PooledByteBufAllocator and so
    // can eliminate the overhead of allocate a new byte[].
    ByteBuf output = input.alloc().heapBuffer(INTEGER_BYTES + maxCompressedLength);
    try {/*from  w  w  w  . j av a 2s.c  o  m*/
        output.writeInt(len);
        // calculate the correct offset.
        int offset = output.arrayOffset() + output.writerIndex();
        byte[] out = output.array();
        int written = compressor.compress(in, inOffset, len, out, offset);

        // Set the writer index so the amount of written bytes is reflected
        output.writerIndex(output.writerIndex() + written);
    } catch (Exception e) {
        // release output buffer so we not leak and rethrow exception.
        output.release();
        throw new IOException(e);
    }
    return output;
}

From source file:com.datastax.driver.core.LZ4Compressor.java

License:Apache License

private ByteBuf decompressDirect(ByteBuf input) throws IOException {
    // If the input is direct we will allocate a direct output buffer as well as this will allow us to use
    // LZ4Compressor.decompress and so eliminate memory copies.
    int readable = input.readableBytes();
    int uncompressedLength = input.readInt();
    ByteBuffer in = inputNioBuffer(input);
    // Increase reader index.
    input.readerIndex(input.writerIndex());
    ByteBuf output = input.alloc().directBuffer(uncompressedLength);
    try {//from  w  ww  .  j av  a  2  s.c  om
        ByteBuffer out = outputNioBuffer(output);
        int read = decompressor.decompress(in, in.position(), out, out.position(), out.remaining());
        if (read != readable - INTEGER_BYTES)
            throw new IOException("Compressed lengths mismatch");

        // Set the writer index so the amount of written bytes is reflected
        output.writerIndex(output.writerIndex() + uncompressedLength);
    } catch (Exception e) {
        // release output buffer so we not leak and rethrow exception.
        output.release();
        throw new IOException(e);
    }
    return output;
}

From source file:com.datastax.driver.core.LZ4Compressor.java

License:Apache License

private ByteBuf decompressHeap(ByteBuf input) throws IOException {
    // Not a direct buffer so use byte arrays...
    byte[] in = input.array();
    int len = input.readableBytes();
    int uncompressedLength = input.readInt();
    int inOffset = input.arrayOffset() + input.readerIndex();
    // Increase reader index.
    input.readerIndex(input.writerIndex());

    // Allocate a heap buffer from the ByteBufAllocator as we may use a PooledByteBufAllocator and so
    // can eliminate the overhead of allocate a new byte[].
    ByteBuf output = input.alloc().heapBuffer(uncompressedLength);
    try {/*  www  .  j  a  v a2s  .c om*/
        int offset = output.arrayOffset() + output.writerIndex();
        byte out[] = output.array();
        int read = decompressor.decompress(in, inOffset, out, offset, uncompressedLength);
        if (read != len - INTEGER_BYTES)
            throw new IOException("Compressed lengths mismatch");

        // Set the writer index so the amount of written bytes is reflected
        output.writerIndex(output.writerIndex() + uncompressedLength);
    } catch (Exception e) {
        // release output buffer so we not leak and rethrow exception.
        output.release();
        throw new IOException(e);
    }
    return output;
}

From source file:com.datastax.driver.core.SnappyCompressor.java

License:Apache License

private ByteBuf compressDirect(ByteBuf input) throws IOException {
    int maxCompressedLength = Snappy.maxCompressedLength(input.readableBytes());
    // If the input is direct we will allocate a direct output buffer as well as this will allow us to use
    // Snappy.compress(ByteBuffer, ByteBuffer) and so eliminate memory copies.
    ByteBuf output = input.alloc().directBuffer(maxCompressedLength);
    try {// w  w  w .j  a  v a  2s. c o m
        ByteBuffer in = inputNioBuffer(input);
        // Increase reader index.
        input.readerIndex(input.writerIndex());

        ByteBuffer out = outputNioBuffer(output);
        int written = Snappy.compress(in, out);
        // Set the writer index so the amount of written bytes is reflected
        output.writerIndex(output.writerIndex() + written);
    } catch (IOException e) {
        // release output buffer so we not leak and rethrow exception.
        output.release();
        throw e;
    }
    return output;
}

From source file:com.datastax.driver.core.SnappyCompressor.java

License:Apache License

private ByteBuf compressHeap(ByteBuf input) throws IOException {
    int maxCompressedLength = Snappy.maxCompressedLength(input.readableBytes());
    int inOffset = input.arrayOffset() + input.readerIndex();
    byte[] in = input.array();
    int len = input.readableBytes();
    // Increase reader index.
    input.readerIndex(input.writerIndex());

    // Allocate a heap buffer from the ByteBufAllocator as we may use a PooledByteBufAllocator and so
    // can eliminate the overhead of allocate a new byte[].
    ByteBuf output = input.alloc().heapBuffer(maxCompressedLength);
    try {/*from w w  w . j av  a 2s . c om*/
        // Calculate the correct offset.
        int offset = output.arrayOffset() + output.writerIndex();
        byte[] out = output.array();
        int written = Snappy.compress(in, inOffset, len, out, offset);

        // Increase the writerIndex with the written bytes.
        output.writerIndex(output.writerIndex() + written);
    } catch (IOException e) {
        // release output buffer so we not leak and rethrow exception.
        output.release();
        throw e;
    }
    return output;
}

From source file:com.datastax.driver.core.SnappyCompressor.java

License:Apache License

private ByteBuf decompressDirect(ByteBuf input) throws IOException {
    ByteBuffer in = inputNioBuffer(input);
    // Increase reader index.
    input.readerIndex(input.writerIndex());

    if (!Snappy.isValidCompressedBuffer(in))
        throw new DriverInternalError("Provided frame does not appear to be Snappy compressed");

    // If the input is direct we will allocate a direct output buffer as well as this will allow us to use
    // Snappy.compress(ByteBuffer, ByteBuffer) and so eliminate memory copies.
    ByteBuf output = input.alloc().directBuffer(Snappy.uncompressedLength(in));
    try {/*w w  w.j  a  v a2  s .  c o  m*/
        ByteBuffer out = outputNioBuffer(output);

        int size = Snappy.uncompress(in, out);
        // Set the writer index so the amount of written bytes is reflected
        output.writerIndex(output.writerIndex() + size);
    } catch (IOException e) {
        // release output buffer so we not leak and rethrow exception.
        output.release();
        throw e;
    }
    return output;
}