public override void WriteByte(byte value) { if (!_isOpen) { __Error.StreamIsClosed(); } EnsureWriteable(); if (_position >= _length) { int newLength = _position + 1; bool mustZero = _position > _length; if (newLength >= _capacity) { bool allocatedNewArray = EnsureCapacity(newLength); if (allocatedNewArray) { mustZero = false; } } if (mustZero) { ClusteredArray <byte> .Clear(_buffer, _length, _position - _length); } _length = newLength; } _buffer[_position++] = value; }
// Sets the length of the stream to a given value. The new // value must be nonnegative and less than the space remaining in // the array, Int32.MaxValue - origin // Origin is 0 in all cases other than a MemoryStream created on // top of an existing array and a specific starting offset was passed // into the MemoryStream constructor. The upper bounds prevents any // situations where a stream may be created on top of an array then // the stream is made longer than the maximum possible length of the // array (Int32.MaxValue). // public override void SetLength(long value) { if (value < 0 || value > Int32.MaxValue) { throw new ArgumentOutOfRangeException("value", ResourceHelper.GetResourceString("ArgumentOutOfRange_StreamLength")); } #if DEBUG Contract.Ensures(_length - _origin == value); Contract.EndContractBlock(); #endif EnsureWriteable(); // Origin wasn't publicly exposed above. #if DEBUG Contract.Assert(MemStreamMaxLength == Int32.MaxValue); // Check parameter validation logic in this method if this fails. #endif if (value > (Int32.MaxValue - _origin)) { throw new ArgumentOutOfRangeException("value", ResourceHelper.GetResourceString("ArgumentOutOfRange_StreamLength")); } int newLength = _origin + (int)value; bool allocatedNewArray = EnsureCapacity(newLength); if (!allocatedNewArray && newLength > _length) { ClusteredArray <byte> .Clear(_buffer, _length, newLength - _length); } _length = newLength; if (_position > newLength) { _position = newLength; } }
// Removes all Objects from the queue. /// <include file='doc\Queue.uex' path='docs/doc[@for="Queue.Clear"]/*' /> public virtual void Clear() { if (_head < _tail) { ClusteredArray <T> .Clear(_array, _head, _size); } else { ClusteredArray <T> .Clear(_array, _head, _array.Length - _head); ClusteredArray <T> .Clear(_array, 0, _tail); } _head = 0; _tail = 0; _size = 0; _version++; }
//Changelog: Used ClusteredArray's internal CopyFrom method. public override void Write(byte[] buffer, int offset, int count) { if (buffer == null) { throw new ArgumentNullException("buffer", ResourceHelper.GetResourceString("ArgumentNull_Buffer")); } if (offset < 0) { throw new ArgumentOutOfRangeException("offset", ResourceHelper.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } if (count < 0) { throw new ArgumentOutOfRangeException("count", ResourceHelper.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } if (buffer.Length - offset < count) { throw new ArgumentException(ResourceHelper.GetResourceString("Argument_InvalidOffLen")); } #if DEBUG Contract.EndContractBlock(); #endif if (!_isOpen) { __Error.StreamIsClosed(); } EnsureWriteable(); int i = _position + count; // Check for overflow if (i < 0) { throw new IOException(ResourceHelper.GetResourceString("IO.IO_StreamTooLong")); } if (i > _length) { bool mustZero = _position > _length; if (i > _capacity) { bool allocatedNewArray = EnsureCapacity(i); if (allocatedNewArray) { mustZero = false; } } if (mustZero) { ClusteredArray <byte> .Clear(_buffer, _length, i - _length); } _length = i; } if (count <= 8) { int byteCount = count; while (--byteCount >= 0) { _buffer[_position + byteCount] = buffer[offset + byteCount]; } } else { _buffer.CopyFrom(buffer, offset, _position, count); } _position = i; }