use of io.netty.buffer.CompositeByteBuf in project elasticsearch by elastic.
the class Netty4UtilsTests method testToChannelBuffer.
public void testToChannelBuffer() throws IOException {
BytesReference ref = getRandomizedBytesReference(randomIntBetween(1, 3 * PAGE_SIZE));
ByteBuf buffer = Netty4Utils.toByteBuf(ref);
BytesReference bytesReference = Netty4Utils.toBytesReference(buffer);
if (ref instanceof ByteBufBytesReference) {
assertEquals(buffer, ((ByteBufBytesReference) ref).toByteBuf());
} else if (AbstractBytesReferenceTestCase.getNumPages(ref) > 1) {
// we gather the buffers into a channel buffer
assertTrue(buffer instanceof CompositeByteBuf);
}
assertArrayEquals(BytesReference.toBytes(ref), BytesReference.toBytes(bytesReference));
}
use of io.netty.buffer.CompositeByteBuf in project vert.x by eclipse.
the class Http2ConnectionBase method safeBuffer.
/**
* Return a buffer from HTTP/2 codec that Vert.x can use:
*
* - if it's a direct buffer (coming likely from OpenSSL) : we get a heap buffer version
* - if it's a composite buffer we do the same
* - otherwise we increase the ref count
*/
static ByteBuf safeBuffer(ByteBuf buf, ByteBufAllocator allocator) {
if (buf == Unpooled.EMPTY_BUFFER) {
return buf;
}
if (buf.isDirect() || buf instanceof CompositeByteBuf) {
if (buf.isReadable()) {
ByteBuf buffer = allocator.heapBuffer(buf.readableBytes());
buffer.writeBytes(buf);
return buffer;
} else {
return Unpooled.EMPTY_BUFFER;
}
}
return buf.retain();
}
use of io.netty.buffer.CompositeByteBuf in project netty by netty.
the class CoalescingBufferQueue method compose.
/**
* Compose the current buffer with another.
*/
private ByteBuf compose(ByteBuf current, ByteBuf next) {
if (current == null) {
return next;
}
if (current instanceof CompositeByteBuf) {
CompositeByteBuf composite = (CompositeByteBuf) current;
composite.addComponent(true, next);
return composite;
}
// 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 = channel.alloc().compositeBuffer(bufAndListenerPairs.size() + 2);
composite.addComponent(true, current);
composite.addComponent(true, next);
return composite;
}
use of io.netty.buffer.CompositeByteBuf in project java-in-action by xinghalo.
the class CompositeTest method main.
public static void main(String[] args) {
// 组合缓冲区
CompositeByteBuf compBuf = Unpooled.compositeBuffer();
ByteBuf header = Unpooled.buffer(8);
header.writeBytes("aaaabbbb".getBytes());
ByteBuf body = Unpooled.buffer(8);
body.writeBytes("11112222".getBytes());
// 添加ByteBuf到CompositeByteBuf
compBuf.addComponents(header, body);
System.out.println(compBuf.toString());
}
use of io.netty.buffer.CompositeByteBuf in project drill by axbaretto.
the class ChunkCreationHandler method encode.
@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
if (RpcConstants.EXTRA_DEBUGGING) {
logger.debug("ChunkCreationHandler called with msg {} of size {} with chunkSize {}", msg, msg.readableBytes(), chunkSize);
}
if (!ctx.channel().isOpen()) {
logger.debug("Channel closed, skipping encode inside {}.", RpcConstants.CHUNK_CREATION_HANDLER);
msg.release();
return;
}
// Calculate the number of chunks based on configured chunk size and input msg size
int numChunks = (int) Math.ceil((double) msg.readableBytes() / chunkSize);
// Initialize a composite buffer to hold numChunks chunk.
final CompositeByteBuf cbb = ctx.alloc().compositeBuffer(numChunks);
int cbbWriteIndex = 0;
int currentChunkLen = min(msg.readableBytes(), chunkSize);
// Create slices of chunkSize from input msg and add it to the composite buffer.
while (numChunks > 0) {
final ByteBuf chunkBuf = msg.slice(msg.readerIndex(), currentChunkLen);
chunkBuf.retain();
cbb.addComponent(chunkBuf);
cbbWriteIndex += currentChunkLen;
msg.skipBytes(currentChunkLen);
--numChunks;
currentChunkLen = min(msg.readableBytes(), chunkSize);
}
// Update the writerIndex of composite byte buffer. Netty doesn't do it automatically.
cbb.writerIndex(cbbWriteIndex);
// Add the final composite bytebuf into output buffer.
out.add(cbb);
}
Aggregations