GSF.IO.BlockAllocatedMemoryStream.SetLength C# (CSharp) Method

SetLength() public method

Sets the length of the current stream to the specified value.
If this length is larger than the previous length, the data is initialized to 0's between the previous length and the current length.
public SetLength ( long value ) : void
value long The value at which to set the length.
return void
        public override void SetLength(long value)
        {
            if (value > m_capacity)
                EnsureCapacity(value);

            if (m_length < value)
                InitializeToPosition(value);

            m_length = value;

            if (m_position > m_length)
                m_position = m_length;
        }

Usage Example

        public void Test3()
        {
            MemoryStream ms = new MemoryStream();
            BlockAllocatedMemoryStream ms2 = new BlockAllocatedMemoryStream();

            for (int x = 0; x < 10000; x++)
            {
                long position = Random.Int64Between(0, 100000);
                ms.Position = position;
                ms2.Position = position;

                int value = Random.Int32;
                ms.Write(value);
                ms2.Write(value);

                long length = Random.Int64Between(100000 >> 1, 100000);
                ms.SetLength(length);
                ms2.SetLength(length);
            }

            Compare(ms, ms2);
        }