List of usage examples for io.netty.buffer CompositeByteBuf addComponent
public CompositeByteBuf addComponent(boolean increaseWriterIndex, ByteBuf buffer)
From source file:alluxio.grpc.GrpcSerializationUtils.java
License:Apache License
/** * Gets a Netty buffer directly from a gRPC ReadableBuffer. * * @param buffer the input buffer//from w ww . ja va 2 s . c o m * @return the raw ByteBuf, or null if the ByteBuf cannot be extracted */ public static ByteBuf getByteBufFromReadableBuffer(ReadableBuffer buffer) { if (!sZeroCopyReceiveSupported) { return null; } try { if (buffer instanceof CompositeReadableBuffer) { Queue<ReadableBuffer> buffers = (Queue<ReadableBuffer>) sCompositeBuffers.get(buffer); if (buffers.size() == 1) { return getByteBufFromReadableBuffer(buffers.peek()); } else { CompositeByteBuf buf = PooledByteBufAllocator.DEFAULT.compositeBuffer(); for (ReadableBuffer readableBuffer : buffers) { ByteBuf subBuffer = getByteBufFromReadableBuffer(readableBuffer); if (subBuffer == null) { return null; } buf.addComponent(true, subBuffer); } return buf; } } else if (buffer.getClass().equals(sReadableByteBuf.getDeclaringClass())) { return (ByteBuf) sReadableByteBuf.get(buffer); } } catch (Exception e) { LOG.warn("Failed to get data buffer from stream: {}.", e.getMessage()); return null; } return null; }
From source file:com.linecorp.armeria.client.encoding.ZlibStreamDecoder.java
License:Apache License
private byte[] fetchDecoderOutput() { CompositeByteBuf decoded = Unpooled.compositeBuffer(); for (;;) {// w ww.j a v a 2 s .c o m ByteBuf buf = decoder.readInbound(); if (buf == null) { break; } if (!buf.isReadable()) { buf.release(); continue; } decoded.addComponent(true, buf); } byte[] ret = ByteBufUtil.getBytes(decoded); decoded.release(); return ret; }
From source file:com.uber.tchannel.codecs.TFrameCodec.java
License:Open Source License
public static ByteBuf encode(ByteBufAllocator allocator, TFrame frame) { ByteBuf buffer = allocator.buffer(TFrame.FRAME_HEADER_LENGTH, TFrame.FRAME_HEADER_LENGTH); // size:2//ww w . j a v a2 s . com buffer.writeShort(frame.size + TFrame.FRAME_HEADER_LENGTH); // type:1 buffer.writeByte(frame.type); // reserved:1 buffer.writeZero(1); // id:4 buffer.writeInt((int) frame.id); // reserved:8 buffer.writeZero(8); // TODO: refactor if (frame.payload instanceof CompositeByteBuf) { CompositeByteBuf cbf = (CompositeByteBuf) frame.payload; cbf.addComponent(0, buffer); cbf.writerIndex(cbf.writerIndex() + TFrame.FRAME_HEADER_LENGTH); return cbf; } return Unpooled.wrappedBuffer(buffer, frame.payload); }
From source file:io.datty.msgpack.test.CompositeByteBufTest.java
License:Apache License
@Test public void testTwoComponents() { ByteBuf first = Unpooled.wrappedBuffer("a".getBytes()); ByteBuf second = Unpooled.wrappedBuffer("b".getBytes()); CompositeByteBuf result = first.alloc().compositeBuffer(); result.addComponent(true, first); result.addComponent(true, second);/*from w ww. j a va2 s. co m*/ byte[] actual = ByteBufUtil.getBytes(result); Assert.assertEquals(2, actual.length); Assert.assertEquals('a', actual[0]); Assert.assertEquals('b', actual[1]); }
From source file:io.datty.msgpack.test.CompositeByteBufTest.java
License:Apache License
@Test public void testCompositeWrite() { ByteBuf first = Unpooled.buffer();// w w w .j av a 2 s . c o m first.writeByte('a'); CompositeByteBuf result = first.alloc().compositeBuffer(); result.addComponent(true, first); result.writeByte('b'); byte[] actual = ByteBufUtil.getBytes(result); Assert.assertEquals(2, actual.length); Assert.assertEquals('a', actual[0]); Assert.assertEquals('b', actual[1]); }
From source file:io.grpc.alts.internal.BufUnwrapperTest.java
License:Apache License
@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 {/*from w ww . j a va 2 s . co m*/ buf.release(); } }
From source file:io.reactiverse.pgclient.impl.codec.decoder.MessageDecoder.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) { ByteBuf buff = (ByteBuf) msg;/*from w w w .j a v a 2s . c om*/ if (in == null) { in = buff; } else { CompositeByteBuf composite; if (in instanceof CompositeByteBuf) { composite = (CompositeByteBuf) in; } else { composite = alloc.compositeBuffer(); composite.addComponent(true, in); in = composite; } composite.addComponent(true, buff); } while (true) { int available = in.readableBytes(); if (available < 5) { break; } int beginIdx = in.readerIndex(); int length = in.getInt(beginIdx + 1); if (length + 1 > available) { break; } byte id = in.getByte(beginIdx); int endIdx = beginIdx + length + 1; final int writerIndex = in.writerIndex(); try { in.setIndex(beginIdx + 5, endIdx); switch (id) { case MessageType.READY_FOR_QUERY: { decodeReadyForQuery(in); break; } case MessageType.DATA_ROW: { decodeDataRow(in); break; } case MessageType.COMMAND_COMPLETE: { decodeCommandComplete(in); break; } case MessageType.BIND_COMPLETE: { decodeBindComplete(); break; } default: { decodeMessage(ctx, id, in); } } } finally { in.setIndex(endIdx, writerIndex); } } if (in != null && !in.isReadable()) { in.release(); in = null; } }
From source file:io.servicecomb.foundation.vertx.tcp.TcpConnection.java
License:Apache License
protected void writeInContext() { CompositeByteBuf cbb = ByteBufAllocator.DEFAULT.compositeBuffer(); for (;;) {/*w w w. j ava 2 s . c o m*/ ByteBuf buf = writeQueue.poll(); if (buf == null) { break; } writeQueueSize.decrementAndGet(); cbb.addComponent(true, buf); if (cbb.numComponents() == cbb.maxNumComponents()) { netSocket.write(Buffer.buffer(cbb)); cbb = ByteBufAllocator.DEFAULT.compositeBuffer(); } } if (cbb.isReadable()) { netSocket.write(Buffer.buffer(cbb)); } }
From source file:org.springframework.core.io.buffer.NettyDataBufferFactory.java
License:Apache License
/** * {@inheritDoc}/*w w w . j a v a 2s .c o m*/ * <p>This implementation uses Netty's {@link CompositeByteBuf}. */ @Override public DataBuffer join(List<? extends DataBuffer> dataBuffers) { Assert.notNull(dataBuffers, "'dataBuffers' must not be null"); CompositeByteBuf composite = this.byteBufAllocator.compositeBuffer(dataBuffers.size()); for (DataBuffer dataBuffer : dataBuffers) { Assert.isInstanceOf(NettyDataBuffer.class, dataBuffer); composite.addComponent(true, ((NettyDataBuffer) dataBuffer).getNativeBuffer()); } return new NettyDataBuffer(composite, this); }