Java Code Examples for io.netty.buffer.CompositeByteBuf#release()

The following examples show how to use io.netty.buffer.CompositeByteBuf#release() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BufUnwrapperTest.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@Test
public void writableNioBuffers_worksWithComposite() {
  CompositeByteBuf buf = alloc.compositeBuffer();
  buf.addComponent(alloc.buffer(1));
  buf.capacity(1);
  try (BufUnwrapper unwrapper = new BufUnwrapper()) {
    ByteBuffer[] internalBufs = unwrapper.writableNioBuffers(buf);
    Truth.assertThat(internalBufs).hasLength(1);

    internalBufs[0].put((byte) 'a');

    buf.writerIndex(1);
    assertEquals('a', buf.readByte());
  } finally {
    buf.release();
  }
}
 
Example 2
Source File: AbstractIntegrationTest.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
protected void testIdentity(final byte[] data) {
    final ByteBuf in = Unpooled.wrappedBuffer(data);
    assertTrue(encoder.writeOutbound(in.retain()));
    assertTrue(encoder.finish());

    final CompositeByteBuf compressed = Unpooled.compositeBuffer();
    ByteBuf msg;
    while ((msg = encoder.readOutbound()) != null) {
        compressed.addComponent(true, msg);
    }
    assertThat(compressed, is(notNullValue()));

    decoder.writeInbound(compressed.retain());
    assertFalse(compressed.isReadable());
    final CompositeByteBuf decompressed = Unpooled.compositeBuffer();
    while ((msg = decoder.readInbound()) != null) {
        decompressed.addComponent(true, msg);
    }
    assertEquals(in.resetReaderIndex(), decompressed);

    compressed.release();
    decompressed.release();
    in.release();
}
 
Example 3
Source File: BufUnwrapperTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Test
public void writableNioBuffers_worksWithComposite() {
  CompositeByteBuf buf = alloc.compositeBuffer();
  buf.addComponent(alloc.buffer(1));
  buf.capacity(1);
  try (BufUnwrapper unwrapper = new BufUnwrapper()) {
    ByteBuffer[] internalBufs = unwrapper.writableNioBuffers(buf);
    Truth.assertThat(internalBufs).hasLength(1);

    internalBufs[0].put((byte) 'a');

    buf.writerIndex(1);
    assertEquals('a', buf.readByte());
  } finally {
    buf.release();
  }
}
 
Example 4
Source File: BufUnwrapperTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Test
public void readableNioBuffers_worksWithComposite() {
  CompositeByteBuf buf = alloc.compositeBuffer();
  buf.addComponent(true, alloc.buffer(1).writeByte('a'));
  try (BufUnwrapper unwrapper = new BufUnwrapper()) {
    ByteBuffer[] internalBufs = unwrapper.readableNioBuffers(buf);
    Truth.assertThat(internalBufs).hasLength(1);

    assertEquals('a', internalBufs[0].get(0));
  } finally {
    buf.release();
  }
}
 
Example 5
Source File: HandshakeV10Request.java    From r2dbc-mysql with Apache License 2.0 5 votes vote down vote up
static HandshakeV10Request decodeV10(ByteBuf buf, HandshakeHeader header) {
    Builder builder = new Builder().header(header);
    CompositeByteBuf salt = buf.alloc().compositeBuffer(2);

    try {
        // After handshake header, MySQL give salt first part (should be 8-bytes always).
        salt.addComponent(true, readCStringRetainedSlice(buf));

        int serverCapabilities;
        CompositeByteBuf capabilities = buf.alloc().compositeBuffer(2);

        try {
            // After salt first part, MySQL give the Server Capabilities first part (always 2-bytes).
            capabilities.addComponent(true, buf.readRetainedSlice(2));

            // New protocol with 16 bytes to describe server character, but MySQL give lower 8-bits only.
            builder.collationLow8Bits(buf.readByte())
                .serverStatuses(buf.readShortLE());

            // No need release `capabilities` second part, it will release with `capabilities`
            serverCapabilities = capabilities.addComponent(true, buf.readRetainedSlice(2))
                .readIntLE();

            builder.serverCapabilities(serverCapabilities);
        } finally {
            capabilities.release();
        }

        return afterCapabilities(builder, buf, serverCapabilities, salt);
    } finally {
        salt.release();
    }
}
 
Example 6
Source File: ByteBufJoiner.java    From r2dbc-mysql with Apache License 2.0 5 votes vote down vote up
static ByteBufJoiner wrapped() {
    return (parts) -> {
        int size = parts.size();

        switch (size) {
            case 0:
                throw new IllegalStateException("No buffer available");
            case 1:
                try {
                    return parts.get(0);
                } finally {
                    parts.clear();
                }
            default:
                CompositeByteBuf composite = null;

                try {
                    composite = parts.get(0).alloc().compositeBuffer(size);
                    // Auto-releasing failed parts
                    return composite.addComponents(true, parts);
                } catch (Throwable e) {
                    if (composite == null) {
                        // Alloc failed, release parts.
                        for (ByteBuf part : parts) {
                            ReferenceCountUtil.safeRelease(part);
                        }
                    } else {
                        // Also release success parts.
                        composite.release();
                    }
                    throw e;
                } finally {
                    parts.clear();
                }
        }
    };
}
 
Example 7
Source File: SslHandler.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
protected ByteBuf composeFirst(ByteBufAllocator allocator, ByteBuf first) {
    if (first instanceof CompositeByteBuf) {
        CompositeByteBuf composite = (CompositeByteBuf) first;
        first = allocator.directBuffer(composite.readableBytes());
        try {
            first.writeBytes(composite);
        } catch (Throwable cause) {
            first.release();
            PlatformDependent.throwException(cause);
        }
        composite.release();
    }
    return first;
}
 
Example 8
Source File: AbstractCoalescingBufferQueue.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
/**
 * Compose {@code cumulation} and {@code next} into a new {@link CompositeByteBuf}.
 */
protected final ByteBuf composeIntoComposite(ByteBufAllocator alloc, ByteBuf cumulation, ByteBuf next) {
    // Create a composite buffer to accumulate this pair and potentially all the buffers
    // in the queue. Using +2 as we have already dequeued current and next.
    CompositeByteBuf composite = alloc.compositeBuffer(size() + 2);
    try {
        composite.addComponent(true, cumulation);
        composite.addComponent(true, next);
    } catch (Throwable cause) {
        composite.release();
        safeRelease(next);
        throwException(cause);
    }
    return composite;
}
 
Example 9
Source File: SnappyFrameEncoderTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test
public void testStreamStartIsOnlyWrittenOnce() throws Exception {
    ByteBuf in = Unpooled.wrappedBuffer(new byte[] {
        'n', 'e', 't', 't', 'y'
    });

    channel.writeOutbound(in.retain());
    in.resetReaderIndex(); // rewind the buffer to write the same data
    channel.writeOutbound(in);
    assertTrue(channel.finish());

    ByteBuf expected = Unpooled.wrappedBuffer(new byte[] {
        (byte) 0xff, 0x06, 0x00, 0x00, 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59,
         0x01, 0x09, 0x00, 0x00, 0x6f, -0x68, -0x7e, -0x5e, 'n', 'e', 't', 't', 'y',
         0x01, 0x09, 0x00, 0x00, 0x6f, -0x68, -0x7e, -0x5e, 'n', 'e', 't', 't', 'y',
    });

    CompositeByteBuf actual = Unpooled.compositeBuffer();
    for (;;) {
        ByteBuf m = channel.readOutbound();
        if (m == null) {
            break;
        }
        actual.addComponent(true, m);
    }
    assertEquals(expected, actual);

    expected.release();
    actual.release();
}
 
Example 10
Source File: HandshakeV10Request.java    From r2dbc-mysql with Apache License 2.0 5 votes vote down vote up
static HandshakeV10Request decodeV10(ByteBuf buf, HandshakeHeader header) {
    Builder builder = new Builder().header(header);
    CompositeByteBuf salt = buf.alloc().compositeBuffer(2);

    try {
        // After handshake header, MySQL give salt first part (should be 8-bytes always).
        salt.addComponent(true, readCStringRetainedSlice(buf));

        int serverCapabilities;
        CompositeByteBuf capabilities = buf.alloc().compositeBuffer(2);

        try {
            // After salt first part, MySQL give the Server Capabilities first part (always 2-bytes).
            capabilities.addComponent(true, buf.readRetainedSlice(2));

            // New protocol with 16 bytes to describe server character, but MySQL give lower 8-bits only.
            builder.collationLow8Bits(buf.readByte())
                .serverStatuses(buf.readShortLE());

            // No need release `capabilities` second part, it will release with `capabilities`
            serverCapabilities = capabilities.addComponent(true, buf.readRetainedSlice(2))
                .readIntLE();

            builder.serverCapabilities(serverCapabilities);
        } finally {
            capabilities.release();
        }

        return afterCapabilities(builder, buf, serverCapabilities, salt);
    } finally {
        salt.release();
    }
}
 
Example 11
Source File: ByteBufJoiner.java    From r2dbc-mysql with Apache License 2.0 5 votes vote down vote up
static ByteBufJoiner wrapped() {
    return (parts) -> {
        int size = parts.size();

        switch (size) {
            case 0:
                throw new IllegalStateException("No buffer available");
            case 1:
                try {
                    return parts.get(0);
                } finally {
                    parts.clear();
                }
            default:
                CompositeByteBuf composite = null;

                try {
                    composite = parts.get(0).alloc().compositeBuffer(size);
                    // Auto-releasing failed parts
                    return composite.addComponents(true, parts);
                } catch (Throwable e) {
                    if (composite == null) {
                        // Alloc failed, release parts.
                        for (ByteBuf part : parts) {
                            ReferenceCountUtil.safeRelease(part);
                        }
                    } else {
                        // Also release success parts.
                        composite.release();
                    }
                    throw e;
                } finally {
                    parts.clear();
                }
        }
    };
}
 
Example 12
Source File: CrlfTerminatingChunkedStreamTest.java    From NioSmtpClient with Apache License 2.0 5 votes vote down vote up
private String terminate(String testString, int chunkSize) throws Exception {
  ByteArrayInputStream stream = new ByteArrayInputStream(testString.getBytes(StandardCharsets.UTF_8));
  CrlfTerminatingChunkedStream chunkedStream = new CrlfTerminatingChunkedStream(stream, chunkSize);

  CompositeByteBuf destBuffer = ALLOCATOR.compositeBuffer();
  while (!chunkedStream.isEndOfInput()) {
    destBuffer.addComponent(true, chunkedStream.readChunk(ALLOCATOR));
  }

  byte[] bytes = new byte[destBuffer.readableBytes()];
  destBuffer.getBytes(0, bytes);
  destBuffer.release();
  return new String(bytes, CharsetUtil.UTF_8);
}
 
Example 13
Source File: MarshallingTest.java    From log4j2-elasticsearch with Apache License 2.0 5 votes vote down vote up
@Test
public void canMarshallFailedItemSource() throws IOException {

    // given
    CompositeByteBuf byteBuf = UnpooledByteBufAllocator.DEFAULT.compositeHeapBuffer(2);
    byte[] bytes = new byte[512];
    random.nextBytes(bytes);

    String expectedPayload = new String(bytes, Charset.defaultCharset());

    byteBuf.writeBytes(bytes);

    assertEquals(expectedPayload, byteBuf.toString(Charset.defaultCharset()));

    ItemSource<ByteBuf> expectedSource = new ByteBufItemSource(byteBuf, (source) -> byteBuf.release());

    String targetName = UUID.randomUUID().toString();
    FailedItemSource<ByteBuf> failedItemSource = new FailedItemSource<>(
            expectedSource,
            new FailedItemInfo(targetName)
    );

    ChronicleMap<CharSequence, ItemSource> map = createDefaultTestChronicleMap();
    String key = UUID.randomUUID().toString();

    // when
    map.put(key, failedItemSource);
    FailedItemSource<ByteBuf> result = (FailedItemSource<ByteBuf>) map.get(key);

    // then
    assertEquals(expectedPayload, result.getSource().toString(Charset.defaultCharset()));
    assertEquals(failedItemSource.getInfo().getTargetName(), result.getInfo().getTargetName());

}
 
Example 14
Source File: BufUnwrapperTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void readableNioBuffers_worksWithComposite() {
  CompositeByteBuf buf = alloc.compositeBuffer();
  buf.addComponent(true, alloc.buffer(1).writeByte('a'));
  try (BufUnwrapper unwrapper = new BufUnwrapper()) {
    ByteBuffer[] internalBufs = unwrapper.readableNioBuffers(buf);
    Truth.assertThat(internalBufs).hasLength(1);

    assertEquals('a', internalBufs[0].get(0));
  } finally {
    buf.release();
  }
}