Esempio n. 1
0
        /// <summary>
        /// Sets the length of the stream.
        /// </summary>
        /// <param name="value">The new length in bytes.</param>
        /// <remarks>
        /// The stream will be truncated if the new length is less than
        /// the current length.  The stream will be extended if the new
        /// length is greater than the current length.  In this case,
        /// the contents of the extended portion will be undefined.
        /// </remarks>
        public override void SetLength(long value)
        {
            int cb;

            if (value < 0 || value > int.MaxValue)
            {
                throw new IOException(TooLongError);
            }

            cb = (int)value;

            if (cb < length)
            {
                length = cb;
                blocks.TruncateTo(length);

                if (pos > length)
                {
                    pos = length;
                }
            }
            else if (cb > length)
            {
                length = cb;
                blocks.ExtendTo(length);
            }
        }
Esempio n. 2
0
        public void TruncateTo()
        {
            BlockArray blocks;

            blocks = new BlockArray();
            blocks.TruncateTo(0);
            Assert.Equal(0, blocks.Size);
            Assert.Empty(blocks.GetBlocks());

            blocks.ExtendTo(1);
            blocks.TruncateTo(1);
            Assert.Equal(blocks.BlockSize, blocks.Size);
            Assert.Single(blocks.GetBlocks());

            blocks.TruncateTo(1000000);
            blocks.TruncateTo(1);
            Assert.Equal(blocks.BlockSize, blocks.Size);
            Assert.Single(blocks.GetBlocks());

            blocks.TruncateTo(0);
            Assert.Equal(0, blocks.Size);
            Assert.Empty(blocks.GetBlocks());

            blocks.ExtendTo(blocks.BlockSize * 4);
            blocks.GetBlocks()[0].Buffer[0] = 0;
            blocks.GetBlocks()[1].Buffer[0] = 1;
            blocks.GetBlocks()[2].Buffer[0] = 2;
            blocks.GetBlocks()[3].Buffer[0] = 3;

            blocks.TruncateTo(blocks.BlockSize * 3);
            Assert.Equal(blocks.BlockSize * 3, blocks.Size);
            Assert.Equal(3, blocks.GetBlocks().Length);
            Assert.Equal(0, blocks.GetBlocks()[0].Buffer[0]);
            Assert.Equal(1, blocks.GetBlocks()[1].Buffer[0]);
            Assert.Equal(2, blocks.GetBlocks()[2].Buffer[0]);

            blocks.TruncateTo(blocks.BlockSize * 2 + 1);
            Assert.Equal(blocks.BlockSize * 3, blocks.Size);
            Assert.Equal(3, blocks.GetBlocks().Length);
            Assert.Equal(0, blocks.GetBlocks()[0].Buffer[0]);
            Assert.Equal(1, blocks.GetBlocks()[1].Buffer[0]);
            Assert.Equal(2, blocks.GetBlocks()[2].Buffer[0]);

            blocks.TruncateTo(blocks.BlockSize);
            Assert.Equal(blocks.BlockSize, blocks.Size);
            Assert.Single(blocks.GetBlocks());
            Assert.Equal(0, blocks.GetBlocks()[0].Buffer[0]);
        }