public override void WriteByte(byte value)
{
if (!_isOpen) throw new ObjectDisposedException(null, SR.ObjectDisposed_StreamClosed);
if (!CanWrite) throw new NotSupportedException(SR.NotSupported_UnwritableStream);
long pos = Interlocked.Read(ref _position); // Use a local to avoid a race condition
long len = Interlocked.Read(ref _length);
long n = pos + 1;
if (pos >= len)
{
// Check for overflow
if (n < 0)
throw new IOException(SR.IO_StreamTooLong);
if (n > _capacity)
throw new NotSupportedException(SR.IO_FixedCapacity);
// Check to see whether we are now expanding the stream and must
// zero any memory in the middle.
if (pos > len)
{
unsafe
{
Helpers.ZeroMemory(_mem + len, pos - len);
}
}
// set length after zeroing memory to avoid race condition of accessing unzeroed memory
Interlocked.Exchange(ref _length, n);
}
unsafe
{
_mem[pos] = value;
}
Interlocked.Exchange(ref _position, n);
}
}