List of usage examples for io.netty.buffer ByteBuf capacity
public abstract int capacity();
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()); ByteBuf b2 = PooledByteBufAllocator.DEFAULT.heapBuffer(128, 128); b2.writerIndex(b2.capacity());/* www .j a va 2 s . c o m*/ 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()); ByteBuf b2 = PooledByteBufAllocator.DEFAULT.heapBuffer(128, 128); b2.writerIndex(b2.capacity());/*from w ww .ja va2 s .c o m*/ 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()); ByteBufList buf = ByteBufList.get(b1); assertEquals(1, buf.size());//from w w w . j a va 2 s . c o m 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()); ByteBuf b2 = PooledByteBufAllocator.DEFAULT.heapBuffer(128, 128); b2.writerIndex(b2.capacity());//from w ww .j a v a 2 s .co m 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.hyracks.http.server.ChunkedResponse.java
License:Apache License
public void error(ByteBuf error) { if (this.error == null) { this.error = error; } else {/*from w w w . j a v a 2 s .co m*/ this.error.capacity(this.error.capacity() + error.capacity()); this.error.writeBytes(error); } }
From source file:org.apache.pulsar.client.impl.RawBatchConverter.java
License:Apache License
/** * Take a batched message and a filter, and returns a message with the only the sub-messages * which match the filter. Returns an empty optional if no messages match. * * This takes ownership of the passes in message, and if the returned optional is not empty, * the ownership of that message is returned also. *///from w ww. j av a 2 s.c o m public static Optional<RawMessage> rebatchMessage(RawMessage msg, BiPredicate<String, MessageId> filter) throws IOException { checkArgument(msg.getMessageIdData().getBatchIndex() == -1); ByteBuf payload = msg.getHeadersAndPayload(); MessageMetadata metadata = Commands.parseMessageMetadata(payload); ByteBuf batchBuffer = PooledByteBufAllocator.DEFAULT.buffer(payload.capacity()); CompressionType compressionType = metadata.getCompression(); CompressionCodec codec = CompressionCodecProvider.getCompressionCodec(compressionType); int uncompressedSize = metadata.getUncompressedSize(); ByteBuf uncompressedPayload = codec.decode(payload, uncompressedSize); try { int batchSize = metadata.getNumMessagesInBatch(); int messagesRetained = 0; SingleMessageMetadata.Builder emptyMetadataBuilder = SingleMessageMetadata.newBuilder() .setCompactedOut(true); for (int i = 0; i < batchSize; i++) { SingleMessageMetadata.Builder singleMessageMetadataBuilder = SingleMessageMetadata.newBuilder(); ByteBuf singleMessagePayload = Commands.deSerializeSingleMessageInBatch(uncompressedPayload, singleMessageMetadataBuilder, 0, batchSize); MessageId id = new BatchMessageIdImpl(msg.getMessageIdData().getLedgerId(), msg.getMessageIdData().getEntryId(), msg.getMessageIdData().getPartition(), i); if (!singleMessageMetadataBuilder.hasPartitionKey()) { messagesRetained++; Commands.serializeSingleMessageInBatchWithPayload(singleMessageMetadataBuilder, singleMessagePayload, batchBuffer); } else if (filter.test(singleMessageMetadataBuilder.getPartitionKey(), id) && singleMessagePayload.readableBytes() > 0) { messagesRetained++; Commands.serializeSingleMessageInBatchWithPayload(singleMessageMetadataBuilder, singleMessagePayload, batchBuffer); } else { Commands.serializeSingleMessageInBatchWithPayload(emptyMetadataBuilder, Unpooled.EMPTY_BUFFER, batchBuffer); } singleMessageMetadataBuilder.recycle(); singleMessagePayload.release(); } emptyMetadataBuilder.recycle(); if (messagesRetained > 0) { int newUncompressedSize = batchBuffer.readableBytes(); ByteBuf compressedPayload = codec.encode(batchBuffer); MessageMetadata.Builder metadataBuilder = metadata.toBuilder(); metadataBuilder.setUncompressedSize(newUncompressedSize); MessageMetadata newMetadata = metadataBuilder.build(); ByteBuf metadataAndPayload = Commands.serializeMetadataAndPayload(Commands.ChecksumType.Crc32c, newMetadata, compressedPayload); Optional<RawMessage> result = Optional .of(new RawMessageImpl(msg.getMessageIdData(), metadataAndPayload)); metadataBuilder.recycle(); newMetadata.recycle(); metadataAndPayload.release(); compressedPayload.release(); return result; } else { return Optional.empty(); } } finally { batchBuffer.release(); metadata.recycle(); msg.close(); } }
From source file:org.apache.pulsar.common.api.ByteBufPairTest.java
License:Apache License
@Test public void testDoubleByteBuf() throws Exception { ByteBuf b1 = PooledByteBufAllocator.DEFAULT.heapBuffer(128, 128); b1.writerIndex(b1.capacity()); ByteBuf b2 = PooledByteBufAllocator.DEFAULT.heapBuffer(128, 128); b2.writerIndex(b2.capacity());/*from w ww .j a v a 2s.co m*/ ByteBufPair buf = ByteBufPair.get(b1, b2); assertEquals(buf.readableBytes(), 256); assertEquals(buf.getFirst(), b1); assertEquals(buf.getSecond(), b2); 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.pulsar.common.api.DoubleByteBufTest.java
License:Apache License
/** * Verify that readableBytes() returns writerIndex - readerIndex. In this case writerIndex is the end of the buffer * and readerIndex is increased by 64.//from ww w . j av a 2 s . co m * * @throws Exception */ @Test public void testReadableBytes() throws Exception { ByteBuf b1 = PooledByteBufAllocator.DEFAULT.heapBuffer(128, 128); b1.writerIndex(b1.capacity()); ByteBuf b2 = PooledByteBufAllocator.DEFAULT.heapBuffer(128, 128); b2.writerIndex(b2.capacity()); ByteBuf buf = DoubleByteBuf.get(b1, b2); assertEquals(buf.readerIndex(), 0); assertEquals(buf.writerIndex(), 256); assertEquals(buf.readableBytes(), 256); for (int i = 0; i < 4; ++i) { buf.skipBytes(64); assertEquals(buf.readableBytes(), 256 - 64 * (i + 1)); } }
From source file:org.apache.qpid.jms.provider.amqp.message.AmqpWritableBufferTest.java
License:Apache License
@Test public void testLimit() { ByteBuf buffer = Unpooled.buffer(1024); AmqpWritableBuffer writable = new AmqpWritableBuffer(buffer); assertEquals(buffer.capacity(), writable.limit()); }
From source file:org.apache.rocketmq.remoting.netty.FileRegionEncoder.java
License:Apache License
/** * Encode a message into a {@link io.netty.buffer.ByteBuf}. This method will be called for each written message that * can be handled by this encoder.//w w w . jav a2 s .c o m * * @param ctx the {@link io.netty.channel.ChannelHandlerContext} which this {@link * io.netty.handler.codec.MessageToByteEncoder} belongs to * @param msg the message to encode * @param out the {@link io.netty.buffer.ByteBuf} into which the encoded message will be written * @throws Exception is thrown if an error occurs */ @Override protected void encode(ChannelHandlerContext ctx, FileRegion msg, final ByteBuf out) throws Exception { WritableByteChannel writableByteChannel = new WritableByteChannel() { @Override public int write(ByteBuffer src) throws IOException { out.writeBytes(src); return out.capacity(); } @Override public boolean isOpen() { return true; } @Override public void close() throws IOException { } }; long toTransfer = msg.count(); while (true) { long transferred = msg.transfered(); if (toTransfer - transferred <= 0) { break; } msg.transferTo(writableByteChannel, transferred); } }