Java Code Examples for io.netty.buffer.ByteBufAllocator#compositeBuffer()
The following examples show how to use
io.netty.buffer.ByteBufAllocator#compositeBuffer() .
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: UnixChannelUtilTest.java From netty-4.1.22 with Apache License 2.0 | 6 votes |
private static void assertCompositeByteBufIsBufferCopyNeededForWrite(ByteBufAllocator alloc, int numDirect, int numHeap, boolean expected) { CompositeByteBuf comp = alloc.compositeBuffer(numDirect + numHeap); List<ByteBuf> byteBufs = new LinkedList<ByteBuf>(); while (numDirect > 0) { byteBufs.add(alloc.directBuffer(1)); numDirect--; } while (numHeap > 0) { byteBufs.add(alloc.heapBuffer(1)); numHeap--; } Collections.shuffle(byteBufs); for (ByteBuf byteBuf : byteBufs) { comp.addComponent(byteBuf); } assertEquals(byteBufs.toString(), expected, isBufferCopyNeededForWrite(comp, IOV_MAX)); assertTrue(comp.release()); }
Example 2
Source File: FileOperationEncoder.java From azeroth with Apache License 2.0 | 6 votes |
@Override public List<Object> encode(ByteBufAllocator alloc) { ByteBuf meta = metadata(alloc); ByteBuf head = alloc.buffer(FDFS_HEAD_LEN); head.writeLong(meta.readableBytes() + size); head.writeByte(cmd()); head.writeByte(ERRNO_OK); CompositeByteBuf cbb = alloc.compositeBuffer(); cbb.addComponents(head, meta); cbb.writerIndex(head.readableBytes() + meta.readableBytes()); List<Object> requests = new LinkedList<>(); requests.add(cbb); requests.add(content); return requests; }
Example 3
Source File: FileOperationEncoder.java From fastdfs-client with Apache License 2.0 | 6 votes |
@Override public List<Object> encode(ByteBufAllocator alloc) { ByteBuf meta = metadata(alloc); ByteBuf head = alloc.buffer(FDFS_HEAD_LEN); head.writeLong(meta.readableBytes() + size); head.writeByte(cmd()); head.writeByte(ERRNO_OK); CompositeByteBuf cbb = alloc.compositeBuffer(); cbb.addComponents(head, meta); cbb.writerIndex(head.readableBytes() + meta.readableBytes()); List<Object> requests = new LinkedList<>(); requests.add(cbb); requests.add(content); return requests; }
Example 4
Source File: LargeFieldReader.java From r2dbc-mysql with Apache License 2.0 | 5 votes |
private static ByteBuf retainedMerge(ByteBufAllocator allocator, List<ByteBuf> parts) { int i; int successSentinel = 0; int size = parts.size(); CompositeByteBuf byteBuf = allocator.compositeBuffer(size); try { for (i = 0; i < size; ++i) { parts.get(i).retain(); successSentinel = i + 1; } // Auto-releasing failed Buffer if addComponents called. return byteBuf.addComponents(true, parts); } catch (Throwable e) { // Also release components which append succeed. ReferenceCountUtil.safeRelease(byteBuf); if (successSentinel < size) { // Retains failed, even not call addComponents. // So release all retained buffers. // Of course, this still does not solve call-stack // overflow when calling addComponents. for (i = 0; i < successSentinel; ++i) { ReferenceCountUtil.safeRelease(parts.get(i)); } } throw e; } }
Example 5
Source File: CompositeBufferGatheringWriteTest.java From netty-4.1.22 with Apache License 2.0 | 5 votes |
private static ByteBuf newCompositeBuffer(ByteBufAllocator alloc) { CompositeByteBuf compositeByteBuf = alloc.compositeBuffer(); compositeByteBuf.addComponent(true, alloc.directBuffer(4).writeInt(100)); compositeByteBuf.addComponent(true, alloc.directBuffer(8).writeLong(123)); compositeByteBuf.addComponent(true, alloc.directBuffer(8).writeLong(456)); assertEquals(EXPECTED_BYTES, compositeByteBuf.readableBytes()); return compositeByteBuf; }
Example 6
Source File: AbstractCoalescingBufferQueue.java From netty-4.1.22 with Apache License 2.0 | 5 votes |
/** * 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 7
Source File: LargeFieldReader.java From r2dbc-mysql with Apache License 2.0 | 5 votes |
private static ByteBuf retainedMerge(ByteBufAllocator allocator, List<ByteBuf> parts) { int i; int successSentinel = 0; int size = parts.size(); CompositeByteBuf byteBuf = allocator.compositeBuffer(size); try { for (i = 0; i < size; ++i) { parts.get(i).retain(); successSentinel = i + 1; } // Auto-releasing failed Buffer if addComponents called. return byteBuf.addComponents(true, parts); } catch (Throwable e) { // Also release components which append succeed. ReferenceCountUtil.safeRelease(byteBuf); if (successSentinel < size) { // Retains failed, even not call addComponents. // So release all retained buffers. // Of course, this still does not solve call-stack // overflow when calling addComponents. for (i = 0; i < successSentinel; ++i) { ReferenceCountUtil.safeRelease(parts.get(i)); } } throw e; } }
Example 8
Source File: DotStuffing.java From NioSmtpClient with Apache License 2.0 | 5 votes |
/** * Returns a {@link CompositeByteBuf} that contains the same data as {@code sourceBuffer}, but with * SMTP dot-stuffing applied, and (if {@code} appendCRLF is true) a CRLF appended. * * <p>If dot-stuffing is not required, and {@code appendCRLF} is false, {@code sourceBuffer} is * returned. In all other cases, {@code allocator} will be used to create a new {@code ByteBuf} * with a {@code refCnt} of one. * * <p>The {@code previousBytes} parameter is used to maintain dot-stuffing across a series * of buffers. Pass the last two bytes of a previous buffer here to ensure an initial dot * will be escaped if necessary. Passing null indicates this is the first or only buffer * for this message. * * @param allocator the {@code ByteBufAllocator} to use for new {@code ByteBuf}s * @param sourceBuffer the source message data * @param previousBytes the previous two bytes of the message, or null * @param termination whether to append CRLF to the end of the returned buffer */ public static ByteBuf createDotStuffedBuffer(ByteBufAllocator allocator, ByteBuf sourceBuffer, byte[] previousBytes, MessageTermination termination) { int dotIndex = findDotAtBeginningOfLine(sourceBuffer, 0, normalisePreviousBytes(previousBytes)); try { if (dotIndex == -1) { if (termination == MessageTermination.ADD_CRLF) { return allocator.compositeBuffer(2).addComponents(true, sourceBuffer.retainedSlice(), CR_LF_BUFFER.slice()); } else { return sourceBuffer.retainedSlice(); } } // Build a CompositeByteBuf to avoid copying CompositeByteBuf compositeByteBuf = allocator.compositeBuffer(); compositeByteBuf.addComponents(true, sourceBuffer.retainedSlice(0, dotIndex), DOT_DOT_BUFFER.slice()); int nextDotIndex; while ((nextDotIndex = findDotAtBeginningOfLine(sourceBuffer, dotIndex + 1, NOT_CR_LF)) != -1) { compositeByteBuf.addComponents(true, sourceBuffer.retainedSlice(dotIndex + 1, nextDotIndex - dotIndex - 1), DOT_DOT_BUFFER.slice()); dotIndex = nextDotIndex; } compositeByteBuf.addComponent(true, sourceBuffer.retainedSlice(dotIndex + 1, sourceBuffer.readableBytes() - dotIndex - 1)); if (termination == MessageTermination.ADD_CRLF) { compositeByteBuf.addComponent(true, CR_LF_BUFFER.slice()); } return compositeByteBuf; } finally { sourceBuffer.release(); } }
Example 9
Source File: TaggingMetadataCodec.java From rsocket-java with Apache License 2.0 | 5 votes |
/** * create tagging content * * @param allocator the {@link ByteBufAllocator} to use to create intermediate buffers as needed. * @param tags tag values * @return tagging content */ public static ByteBuf createTaggingContent(ByteBufAllocator allocator, Collection<String> tags) { CompositeByteBuf taggingContent = allocator.compositeBuffer(); for (String key : tags) { int length = ByteBufUtil.utf8Bytes(key); if (length == 0 || length > TAG_LENGTH_MAX) { continue; } ByteBuf byteBuf = allocator.buffer().writeByte(length); byteBuf.writeCharSequence(key, StandardCharsets.UTF_8); taggingContent.addComponent(true, byteBuf); } return taggingContent; }
Example 10
Source File: CodecUtils.java From tchannel-java with MIT License | 4 votes |
public static @NotNull ByteBuf writeArgs( @NotNull ByteBufAllocator allocator, @NotNull ByteBuf header, @NotNull List<ByteBuf> args ) { int writableBytes = TFrame.MAX_FRAME_PAYLOAD_LENGTH - header.readableBytes(); List<ByteBuf> allocatedBufs = new ArrayList<>(7); List<ByteBuf> bufs = new ArrayList<>(7); bufs.add(header); boolean release = true; try { while (!args.isEmpty()) { ByteBuf arg = args.get(0); int len = writeArg(allocator, arg, writableBytes, bufs, allocatedBufs); writableBytes -= len; if (writableBytes <= TFrame.FRAME_SIZE_LENGTH) { break; } if (arg.readableBytes() == 0) { args.remove(0); } } CompositeByteBuf comp = allocator.compositeBuffer(); comp.addComponents(bufs); comp.writerIndex(TFrame.MAX_FRAME_PAYLOAD_LENGTH - writableBytes); release = false; return comp; } finally { if (release) { for (ByteBuf buf : allocatedBufs) { if (buf != null) { try { buf.release(); } catch (Exception e) { logger.warn("Failed to release", e); } } } } } }