public IByteBuffer Take()
        {
            ByteBufferSlab slab = _slab.Take(0);

            if (slab == null)
            {
                //We need to copy :(
                var dest = new byte[Count];
                Array.Copy(_segment.Array, _segment.Offset + _position, dest, 0, dest.Length);
                return(ByteBufferArray.FromBufferData(dest, 0, dest.Length));
            }

            int cStart = slab.Start;
            int cEnd   = slab.End;
            ArraySegment <byte> cSegment = slab.Segment;

            slab.Start   = Start;
            slab.End     = End;
            slab.Segment = Segment;

            Start   = cStart;
            End     = cEnd; //Should always be 0
            Segment = cSegment;

            return(slab);
        }
        public void TestCopyToSlab()
        {
            ByteBufferSlab slab = dispenser.Take(0);

            slab.Write("Test");

            Assert.AreEqual((byte)'T', slab[0]);
            Assert.AreEqual((byte)'e', slab[1]);
            Assert.AreEqual((byte)'s', slab[2]);
            Assert.AreEqual((byte)'t', slab[3]);

            IByteBuffer newBuffer = slab.Take();

            Assert.AreEqual((byte)'T', newBuffer[0]);
            Assert.AreEqual((byte)'e', newBuffer[1]);
            Assert.AreEqual((byte)'s', newBuffer[2]);
            Assert.AreEqual((byte)'t', newBuffer[3]);

            newBuffer.Start = 1;

            Assert.AreEqual((byte)'e', newBuffer[0]);
            Assert.AreEqual((byte)'s', newBuffer[1]);
            Assert.AreEqual((byte)'t', newBuffer[2]);

            dispenser.Return(slab);
        }
Exemple #3
0
 public void Return(ByteBufferSlab buffer)
 {
     buffer.Clear();
     _buffers.Push(buffer);
 }