Exemple #1
0
        public void TestExpandPos()
        {
            IoBuffer buffer = IoBuffer.Allocate(10);

            buffer.Put(Encoding.Default.GetBytes("012345"));
            buffer.Flip();

            Assert.AreEqual(6, buffer.Remaining);

            // See if we can expand with a lower number of remaining bytes. We should not.
            IoBuffer newBuffer = buffer.Expand(3, 2);

            Assert.AreEqual(6, newBuffer.Limit);
            Assert.AreEqual(10, newBuffer.Capacity);
            Assert.AreEqual(0, newBuffer.Position);

            // Now, let's expand the buffer above the number of current bytes but below the limit
            buffer = IoBuffer.Allocate(10);
            buffer.Put(Encoding.Default.GetBytes("012345"));
            buffer.Flip();

            newBuffer = buffer.Expand(3, 5);
            Assert.AreEqual(8, newBuffer.Limit);
            Assert.AreEqual(10, newBuffer.Capacity);
            Assert.AreEqual(0, newBuffer.Position);

            // Last, expand the buffer above the limit
            buffer = IoBuffer.Allocate(10);

            buffer.Put(Encoding.Default.GetBytes("012345"));
            buffer.Flip();
            newBuffer = buffer.Expand(3, 9);
            Assert.AreEqual(12, newBuffer.Limit);
            Assert.AreEqual(12, newBuffer.Capacity);
            Assert.AreEqual(0, newBuffer.Position);

            // Now, move forward in the buffer
            buffer = IoBuffer.Allocate(10);

            buffer.Put(Encoding.Default.GetBytes("012345"));
            buffer.Flip();
            buffer.Position = 4;

            // See if we can expand with a lower number of remaining bytes. We should not be.
            newBuffer = buffer.Expand(5, 1);
            Assert.AreEqual(6, newBuffer.Limit);
            Assert.AreEqual(10, newBuffer.Capacity);
            Assert.AreEqual(4, newBuffer.Position);

            // Expand above the current limit
            buffer = IoBuffer.Allocate(10);

            buffer.Put(Encoding.Default.GetBytes("012345"));
            buffer.Flip();
            buffer.Position = 4;
            newBuffer       = buffer.Expand(5, 2);
            Assert.AreEqual(7, newBuffer.Limit);
            Assert.AreEqual(10, newBuffer.Capacity);
            Assert.AreEqual(4, newBuffer.Position);

            // Expand above the current capacity
            buffer = IoBuffer.Allocate(10);

            buffer.Put(Encoding.Default.GetBytes("012345"));
            buffer.Flip();
            buffer.Position = 4;
            newBuffer       = buffer.Expand(5, 6);
            Assert.AreEqual(11, newBuffer.Limit);
            Assert.AreEqual(11, newBuffer.Capacity);
            Assert.AreEqual(4, newBuffer.Position);
        }
Exemple #2
0
 /// <inheritdoc/>
 public override IoBuffer Expand(Int32 expectedRemaining)
 {
     _buf.Expand(expectedRemaining);
     return(this);
 }