Ejemplo n.º 1
0
        internal static IByteBuffer EncodeString0(IByteBufferAllocator alloc, bool enforceHeap, string src, Encoding encoding, int extraCapacity)
        {
            int  length  = encoding.GetMaxByteCount(src.Length) + extraCapacity;
            bool release = true;

            IByteBuffer dst = enforceHeap ? alloc.HeapBuffer(length) : alloc.Buffer(length);

            Contract.Assert(dst.HasArray, "Operation expects allocator to operate array-based buffers.");

            try
            {
                int written = encoding.GetBytes(src, 0, src.Length, dst.Array, dst.ArrayOffset + dst.WriterIndex);
                dst.SetWriterIndex(dst.WriterIndex + written);
                release = false;

                return(dst);
            }
            finally
            {
                if (release)
                {
                    dst.Release();
                }
            }
        }
Ejemplo n.º 2
0
        public void UnsafeHeapBufferAndUnsafeDirectBuffer()
        {
            IByteBufferAllocator allocator    = this.NewUnpooledAllocator();
            IByteBuffer          directBuffer = allocator.DirectBuffer();

            AssertInstanceOf <UnpooledUnsafeDirectByteBuffer>(directBuffer);
            directBuffer.Release();

            IByteBuffer heapBuffer = allocator.HeapBuffer();

            AssertInstanceOf <UnpooledHeapByteBuffer>(heapBuffer);
            heapBuffer.Release();
        }
Ejemplo n.º 3
0
        public void HeapBufferWithCapacity(bool preferDirect, int maxCapacity)
        {
            IByteBufferAllocator allocator = this.NewAllocator(preferDirect);
            IByteBuffer          buffer    = allocator.HeapBuffer(1, maxCapacity);

            try
            {
                AssertBuffer(buffer, false, 1, maxCapacity);
            }
            finally
            {
                buffer.Release();
            }
        }
Ejemplo n.º 4
0
        public void HeapBuffer(bool preferDirect)
        {
            IByteBufferAllocator allocator = this.NewAllocator(preferDirect);
            IByteBuffer          buffer    = allocator.HeapBuffer(1);

            try
            {
                AssertBuffer(buffer, false, 1, this.DefaultMaxCapacity);
            }
            finally
            {
                buffer.Release();
            }
        }
Ejemplo n.º 5
0
        public void HeapBufferWithCapacity()
        {
            IByteBufferAllocator allocator = this.NewAllocator();
            IByteBuffer          buffer    = allocator.HeapBuffer(1, 8);

            try
            {
                AssertBuffer(buffer, 1, 8);
            }
            finally
            {
                buffer.Release();
            }
        }
        public void PooledHeapBuffer()
        {
            IByteBufferAllocator allocator = this.NewAllocator();

            IByteBuffer heapBuffer = allocator.HeapBuffer();

            try
            {
                Assert.IsAssignableFrom <PooledHeapByteBuffer>(heapBuffer);
            }
            finally
            {
                heapBuffer.Release();
            }
        }
Ejemplo n.º 7
0
        public void UsedHeapMemory()
        {
            IByteBufferAllocator       allocator = this.NewAllocator(true);
            IByteBufferAllocatorMetric metric    = ((IByteBufferAllocatorMetricProvider)allocator).Metric;

            Assert.Equal(0, metric.UsedHeapMemory);
            IByteBuffer buffer   = allocator.HeapBuffer(1024, 4096);
            int         capacity = buffer.Capacity;

            Assert.Equal(this.ExpectedUsedMemory(allocator, capacity), metric.UsedHeapMemory);

            // Double the size of the buffer
            buffer.AdjustCapacity(capacity << 1);
            capacity = buffer.Capacity;
            Assert.Equal(this.ExpectedUsedMemory(allocator, capacity), metric.UsedHeapMemory);

            buffer.Release();
            Assert.Equal(this.ExpectedUsedMemoryAfterRelease(allocator, capacity), metric.UsedHeapMemory);
        }
Ejemplo n.º 8
0
 public IByteBuffer Buffer() => _allocator.HeapBuffer();