Redzen.IO.MemoryBlockStream.SetLength C# (CSharp) Method

SetLength() public method

Sets the length of the stream to the specified value.
public SetLength ( long value ) : void
value long
return void
        public override void SetLength(long value)
        {
            if(value < 0 || value > Int32.MaxValue) {
                throw new ArgumentOutOfRangeException("value", "Stream length must be non-negative and less than 2^31 - 1.");
            }

            int newLength = (int)value;
            if(newLength == _length)
            {   // Do nothing.
                return;
            }

            if(newLength > _length)
            {
                // Handle case where new length is beyond the current length.
                // Ensure that any existing capacity after _length is zeroed.
                ZeroSpareCapacity();

                // Grow the capacity to ensure _length is within the bounds of allocated space that can be read.
                // Note. newly creatde blocks are zeroed by default.
                EnsureCapacity(newLength);
            }
            else if(newLength < _length)
            {
                _length = newLength;
            }

            // 'Snap back' the position. This is done to mimic the behaviour of MemoryStream, although the reason for doing this is
            // unclear since setting Position directly allows a position beyond the end of the stream.
            if(_position > newLength) {
                _position = newLength;
            }
        }