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:io.airlift.drift.transport.netty.codec.TestHeaderTransport.java

License:Apache License

@Test
public void testTryDecodeSequenceId() throws Exception {
    try (TestingPooledByteBufAllocator allocator = new TestingPooledByteBufAllocator()) {
        ByteBuf message = createTestFrame(allocator, "method", CALL, 0xFFAA, BINARY, true);
        try {//  ww  w .  ja v  a 2  s .  co m
            assertDecodeFrameInfo(message.retainedSlice(0, 0), Optional.empty());
            assertDecodeFrameInfo(message.retainedSlice(0, 1), Optional.empty());
            assertDecodeFrameInfo(message.retainedSlice(0, 5), Optional.empty());
            assertDecodeFrameInfo(message.retainedSlice(0, 10), Optional.empty());
            assertDecodeFrameInfo(message.retainedSlice(0, 15), Optional.empty());
            assertDecodeFrameInfo(message.retainedDuplicate(),
                    Optional.of(new FrameInfo("method", CALL, 0xFFAA, HEADER, BINARY, true)));
        } finally {
            message.release();
        }
        assertDecodeFrameInfo(createTestFrame(allocator, "method1", ONEWAY, 123, FB_COMPACT, false),
                Optional.of(new FrameInfo("method1", ONEWAY, 123, HEADER, FB_COMPACT, false)));
    }
}

From source file:io.airlift.drift.transport.netty.codec.TestHeaderTransport.java

License:Apache License

private static void assertDecodeFrameInfo(ByteBuf message, Optional<FrameInfo> frameInfo) {
    try {/*from   www  . j a  va 2 s .c o  m*/
        assertEquals(tryDecodeFrameInfo(message), frameInfo);
    } finally {
        message.release();
    }
}

From source file:io.airlift.drift.transport.netty.codec.TestSimpleFrameInfoDecoder.java

License:Apache License

private static void testDecodeSequenceId(ByteBufAllocator allocator, Protocol protocol) throws TException {
    FrameInfoDecoder decoder = new SimpleFrameInfoDecoder(FRAMED, protocol, true);
    ByteBuf message = createTestMessage(allocator, protocol);
    try {// ww  w  .  j a  v  a2s.c om
        assertFalse(decoder.tryDecodeFrameInfo(message.slice(0, 0)).isPresent());
        assertFalse(decoder.tryDecodeFrameInfo(message.slice(0, 1)).isPresent());
        assertFalse(decoder.tryDecodeFrameInfo(message.slice(0, 2)).isPresent());
        assertFalse(decoder.tryDecodeFrameInfo(message.slice(0, 5)).isPresent());
        assertTrue(decoder.tryDecodeFrameInfo(message.slice(0, message.readableBytes())).isPresent());
        assertTrue(decoder.tryDecodeFrameInfo(message).isPresent());
        assertEquals(decoder.tryDecodeFrameInfo(message).get(),
                new FrameInfo(METHOD_NAME, CALL, SEQUENCE_ID, FRAMED, protocol, true));
    } finally {
        message.release();
    }
}

From source file:io.airlift.drift.transport.netty.codec.TestThriftFramedDecoder.java

License:Apache License

@Test
public void testBelowLimit() {
    byte[] first = new byte[] { 1, 2, 3, 4, 5 };
    byte[] second = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    byte[] third = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

    try (TestingPooledByteBufAllocator allocator = new TestingPooledByteBufAllocator()) {
        ByteBuf buffer = allocator.buffer(1024);

        writeLengthPrefixedFrame(buffer, first);
        writeLengthPrefixedFrame(buffer, second);
        writeLengthPrefixedFrame(buffer, third);

        ThriftFramedDecoder decoder = createDecoder(third.length);

        ByteBuf decodedFirst = decode(decoder, buffer);
        assertNotNull(decodedFirst);//from w  w  w  .j av  a2  s  . co  m
        assertContentEquals(decodedFirst, first);
        decodedFirst.release();

        ByteBuf decodedSecond = decode(decoder, buffer);
        assertNotNull(decodedSecond);
        assertContentEquals(decodedSecond, second);
        decodedSecond.release();

        ByteBuf decodedThird = decode(decoder, buffer);
        assertNotNull(decodedThird);
        assertContentEquals(decodedThird, third);
        decodedThird.release();

        buffer.release();
    }
}

From source file:io.airlift.drift.transport.netty.codec.TestThriftFramedDecoder.java

License:Apache License

@Test
public void testChunked() {
    byte[] first = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    byte[] second = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    byte[] third = new byte[] { 5, 4, 3 };

    try (TestingPooledByteBufAllocator allocator = new TestingPooledByteBufAllocator()) {
        ByteBuf buffer = allocator.buffer(1024);

        ThriftFramedDecoder decoder = createDecoder(second.length);
        ByteBuf decoded = decode(decoder, buffer);
        assertNull(decoded);//  www  . j a  v a2  s.  com

        // write a partial frame length
        buffer.writeByte(0xAB);
        decoded = decode(decoder, buffer);
        assertNull(decoded);
        assertEquals(buffer.readerIndex(), 0);
        assertEquals(buffer.writerIndex(), 1);

        // write only a frame length
        buffer.writerIndex(0);
        buffer.writeInt(first.length);
        decoded = decode(decoder, buffer);
        assertNull(decoded);
        assertEquals(buffer.readerIndex(), 0);
        assertEquals(buffer.writerIndex(), Integer.BYTES);

        // start writing a frame
        buffer.writeBytes(first, 0, 5);
        decoded = decode(decoder, buffer);
        assertNull(decoded);
        assertEquals(buffer.readerIndex(), 0);
        assertEquals(buffer.writerIndex(), Integer.BYTES + 5);

        // finish writing a frame
        buffer.writeBytes(first, 5, first.length - 5);
        decoded = decode(decoder, buffer);
        assertNotNull(decoded);
        assertContentEquals(decoded, first);
        decoded.release();

        // write the first frame
        writeLengthPrefixedFrame(buffer, second);
        // start writing the second frame
        buffer.writeInt(third.length);
        buffer.writeBytes(third, 0, 1);
        // decode the first frame
        decoded = decode(decoder, buffer);
        assertNotNull(decoded);
        assertContentEquals(decoded, second);
        decoded.release();

        // try decode the second frame
        decoded = decode(decoder, buffer);
        assertNull(decoded);

        // finish writing the second frame
        buffer.writeBytes(third, 1, third.length - 1);
        decoded = decode(decoder, buffer);
        assertNotNull(decoded);
        assertContentEquals(decoded, third);
        decoded.release();

        assertEquals(buffer.readerIndex(), buffer.writerIndex());

        buffer.release();
    }
}

From source file:io.airlift.drift.transport.netty.codec.TestThriftFramedDecoder.java

License:Apache License

@Test
public void testBeyondLimit() throws Exception {
    try (TestingPooledByteBufAllocator allocator = new TestingPooledByteBufAllocator()) {
        byte[] small = new byte[] { 5, 4, 3 };
        byte[] firstLargeFrame = createTestFrame(allocator, "first_method", 1, CALL);
        byte[] secondLargeFrame = createTestFrame(allocator, "second_method", 2, ONEWAY);
        byte[] invalidLargeFrame = createInvalidFrame();
        FrameInfo firstFrameInfo = new FrameInfo("first_method", CALL, 1, FRAMED, BINARY, true);
        FrameInfo secondFrameInfo = new FrameInfo("second_method", ONEWAY, 2, FRAMED, BINARY, true);

        ByteBuf buffer = allocator.buffer(1024);

        ThriftFramedDecoder decoder = createDecoder(firstLargeFrame.length - 5);

        // write a small frame
        writeLengthPrefixedFrame(buffer, small);
        ByteBuf decoded = decode(decoder, buffer);
        assertNotNull(decoded);//from w w w .ja v  a2 s.c o m
        assertContentEquals(decoded, small);
        decoded.release();

        // write a large frame in a single chunk
        writeLengthPrefixedFrame(buffer, firstLargeFrame);
        writeLengthPrefixedFrame(buffer, small);
        try {
            decode(decoder, buffer);
            fail("failure expected");
        } catch (RuntimeException e) {
            assertThat(e).isInstanceOf(FrameTooLargeException.class).hasFieldOrPropertyWithValue("frameInfo",
                    Optional.of(firstFrameInfo));
        }
        assertEquals(buffer.readableBytes(), Integer.BYTES + small.length);
        decoded = decode(decoder, buffer);
        assertNotNull(decoded);
        assertContentEquals(decoded, small);
        decoded.release();

        // write the first large frame in multiple chunks
        buffer.writeInt(secondLargeFrame.length);
        decoded = decode(decoder, buffer);
        assertNull(decoded);
        buffer.writeBytes(secondLargeFrame, 0, 1);
        decoded = decode(decoder, buffer);
        assertNull(decoded);
        buffer.writeBytes(secondLargeFrame, 1, 2);
        decoded = decode(decoder, buffer);
        assertNull(decoded);
        // write the second large frame in multiple chunks
        buffer.writeBytes(secondLargeFrame, 3, secondLargeFrame.length - 3);
        buffer.writeInt(firstLargeFrame.length);
        buffer.writeBytes(firstLargeFrame, 0, 5);

        // decode the first large frame
        try {
            decode(decoder, buffer);
            fail("failure expected");
        } catch (RuntimeException e) {
            assertThat(e).isInstanceOf(FrameTooLargeException.class).hasFieldOrPropertyWithValue("frameInfo",
                    Optional.of(secondFrameInfo));
        }
        assertEquals(buffer.readableBytes(), Integer.BYTES + 5);

        // try decode the second large frame
        decoded = decode(decoder, buffer);
        assertNull(decoded);

        // finish the second large frame
        buffer.writeBytes(firstLargeFrame, 5, firstLargeFrame.length - 5);

        // decode the second large frame
        try {
            decode(decoder, buffer);
            fail("failure expected");
        } catch (RuntimeException e) {
            assertThat(e).isInstanceOf(FrameTooLargeException.class).hasFieldOrPropertyWithValue("frameInfo",
                    Optional.of(firstFrameInfo));
        }
        assertEquals(buffer.readableBytes(), 0);

        // write an invalid large frame in a single chunk
        writeLengthPrefixedFrame(buffer, invalidLargeFrame);
        writeLengthPrefixedFrame(buffer, small);
        try {
            decode(decoder, buffer);
            fail("failure expected");
        } catch (RuntimeException e) {
            assertThat(e).isInstanceOf(FrameTooLargeException.class)
                    // frameInfo cannot be decoded for an invalid frame
                    .hasFieldOrPropertyWithValue("frameInfo", Optional.empty());
        }
        assertEquals(buffer.readableBytes(), Integer.BYTES + small.length);
        decoded = decode(decoder, buffer);
        assertNotNull(decoded);
        assertContentEquals(decoded, small);
        decoded.release();

        // write an invalid large frame in multiple chunks
        buffer.writeInt(invalidLargeFrame.length);
        buffer.writeBytes(invalidLargeFrame, 0, invalidLargeFrame.length / 2);
        decoded = decode(decoder, buffer);
        assertNull(decoded);

        buffer.writeBytes(invalidLargeFrame, invalidLargeFrame.length / 2,
                invalidLargeFrame.length - invalidLargeFrame.length / 2);
        try {
            decode(decoder, buffer);
            fail("failure expected");
        } catch (RuntimeException e) {
            assertThat(e).isInstanceOf(FrameTooLargeException.class)
                    // frame info cannot be decoded for an invalid frame
                    .hasFieldOrPropertyWithValue("frameInfo", Optional.empty());
        }
        assertEquals(buffer.readableBytes(), 0);

        buffer.release();
    }
}

From source file:io.airlift.drift.transport.netty.codec.TestThriftFramedDecoder.java

License:Apache License

private static byte[] createTestFrame(ByteBufAllocator allocator, String methodName, int sequenceId,
        byte messageType) throws Exception {
    TChannelBufferOutputTransport transport = new TChannelBufferOutputTransport(allocator);
    try {//from w ww . j a v  a2 s  .  c o  m
        TProtocolWriter protocolWriter = BINARY.createProtocol(transport);
        protocolWriter.writeMessageBegin(new TMessage(methodName, messageType, sequenceId));

        // write the parameters
        ProtocolWriter writer = new ProtocolWriter(protocolWriter);
        writer.writeStructBegin(methodName + "_args");
        writer.writeStructEnd();

        protocolWriter.writeMessageEnd();
        ByteBuf buffer = transport.getBuffer();
        byte[] result = new byte[buffer.readableBytes()];
        buffer.readBytes(result);
        buffer.release();
        return result;
    } finally {
        transport.release();
    }
}

From source file:io.atomix.catalyst.transport.netty.NettyConnection.java

License:Apache License

/**
 * Handles a request./*from  www .ja  v a2 s .  co m*/
 */
void handleRequest(ByteBuf buffer) {
    long requestId = buffer.readLong();

    try {
        Object request = readRequest(buffer);
        HandlerHolder handler = handlers.get(request.getClass());
        if (handler != null) {
            handler.context.executor().execute(() -> handleRequest(requestId, request, handler));
        } else {
            handleRequestFailure(requestId,
                    new SerializationException("unknown message type: " + request.getClass()), this.context);
        }
    } catch (SerializationException e) {
        handleRequestFailure(requestId, e, this.context);
    } finally {
        buffer.release();
    }
}

From source file:io.atomix.catalyst.transport.netty.NettyConnection.java

License:Apache License

/**
 * Handles response./*  www.  j  a va2s .c  o m*/
 */
void handleResponse(ByteBuf response) {
    long requestId = response.readLong();
    byte status = response.readByte();
    switch (status) {
    case SUCCESS:
        try {
            handleResponseSuccess(requestId, readResponse(response));
        } catch (SerializationException e) {
            handleResponseFailure(requestId, e);
        }
        break;
    case FAILURE:
        try {
            handleResponseFailure(requestId, readError(response));
        } catch (SerializationException e) {
            handleResponseFailure(requestId, e);
        }
        break;
    }
    response.release();
}

From source file:io.atomix.catalyst.transport.NettyConnection.java

License:Apache License

/**
 * Handles a request.//from ww  w . j  ava  2 s. c  om
 */
void handleRequest(ByteBuf buffer) {
    long requestId = buffer.readLong();
    Object request = readRequest(buffer);
    HandlerHolder handler = handlers.get(request.getClass());
    if (handler != null) {
        handler.context.executor().execute(() -> handleRequest(requestId, request, handler));
    } else {
        handleRequestFailure(requestId,
                new IllegalStateException("unknown message type: " + request.getClass()));
    }
    buffer.release();
}