static IByteBuffer NewCompositeBuffer(IByteBufferAllocator alloc) { CompositeByteBuffer 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)); Assert.Equal(ExpectedBytes, compositeByteBuf.ReadableBytes); return(compositeByteBuf); }
public void CompositeBuffer(bool preferDirect) { IByteBufferAllocator allocator = this.NewAllocator(preferDirect); CompositeByteBuffer buffer = allocator.CompositeBuffer(); try { this.AssertCompositeByteBuffer(buffer, this.DefaultMaxComponents); } finally { buffer.Release(); } }
public void CompositeBufferWithCapacity() { IByteBufferAllocator allocator = this.NewAllocator(); CompositeByteBuffer buffer = allocator.CompositeBuffer(8); try { this.AssertCompositeByteBuffer(buffer, 8); } finally { buffer.Release(); } }
public IByteBuffer Cumulate(IByteBufferAllocator alloc, IByteBuffer cumulation, IByteBuffer input) { if (!cumulation.IsReadable()) { _ = cumulation.Release(); return(input); } CompositeByteBuffer composite = null; try { composite = cumulation as CompositeByteBuffer; if (composite is object && 0u >= (uint)(cumulation.ReferenceCount - 1)) { // Writer index must equal capacity if we are going to "write" // new components to the end if (composite.WriterIndex != composite.Capacity) { _ = composite.AdjustCapacity(composite.WriterIndex); } } else { composite = alloc.CompositeBuffer(int.MaxValue).AddFlattenedComponents(true, cumulation); } _ = composite.AddFlattenedComponents(true, input); input = null; return(composite); } finally { if (input is object) { // We must release if the ownership was not transferred as otherwise it may produce a leak _ = input.Release(); // Also release any new buffer allocated if we're not returning it if (composite is object && composite != cumulation) { _ = composite.Release(); } } } }
/// <summary> /// Compose <paramref name="cumulation"/> and <paramref name="next"/> into a new <see cref="CompositeByteBuffer"/>. /// </summary> /// <param name="alloc"></param> /// <param name="cumulation"></param> /// <param name="next"></param> /// <returns></returns> protected IByteBuffer ComposeIntoComposite(IByteBufferAllocator alloc, IByteBuffer cumulation, IByteBuffer 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. var composite = alloc.CompositeBuffer(Size() + 2); try { _ = composite.AddComponent(true, cumulation); _ = composite.AddComponent(true, next); } catch (Exception) { _ = composite.Release(); ReferenceCountUtil.SafeRelease(next); throw; } return(composite); }
public CompositeByteBuffer CompositeBuffer() => DelegateAllocator.CompositeBuffer();