Exemplo n.º 1
0
        public void CalculateNewCapacity(bool preferDirect)
        {
            IByteBufferAllocator allocator = this.NewAllocator(preferDirect);

            Assert.Equal(8, allocator.CalculateNewCapacity(1, 8));
            Assert.Equal(7, allocator.CalculateNewCapacity(1, 7));
            Assert.Equal(64, allocator.CalculateNewCapacity(1, 129));

            Assert.Throws <ArgumentOutOfRangeException>(() => allocator.CalculateNewCapacity(8, 7));
            Assert.Throws <ArgumentOutOfRangeException>(() => allocator.CalculateNewCapacity(-1, 8));
        }
Exemplo n.º 2
0
        private static IByteBuffer ExpandCumulation(IByteBufferAllocator alloc, IByteBuffer oldCumulation, IByteBuffer input)
        {
            int         oldBytes      = oldCumulation.ReadableBytes;
            int         newBytes      = input.ReadableBytes;
            int         totalBytes    = oldBytes + newBytes;
            IByteBuffer newCumulation = alloc.Buffer(alloc.CalculateNewCapacity(totalBytes, int.MaxValue));
            IByteBuffer toRelease     = newCumulation;

            try
            {
                // This avoids redundant checks and stack depth compared to calling writeBytes(...)
                _ = newCumulation.SetBytes(0, oldCumulation, oldCumulation.ReaderIndex, oldBytes)
                    .SetBytes(oldBytes, input, input.ReaderIndex, newBytes)
                    .SetWriterIndex(totalBytes);
                _         = input.SetReaderIndex(input.WriterIndex);
                toRelease = oldCumulation;
                return(newCumulation);
            }
            finally
            {
                _ = toRelease.Release();
            }
        }
Exemplo n.º 3
0
 public int CalculateNewCapacity(int minNewCapacity, int maxCapacity) => _allocator.CalculateNewCapacity(minNewCapacity, maxCapacity);