diff options
Diffstat (limited to 'src/mscorlib/src/System/IO/UnmanagedMemoryStream.cs')
-rw-r--r-- | src/mscorlib/src/System/IO/UnmanagedMemoryStream.cs | 481 |
1 files changed, 274 insertions, 207 deletions
diff --git a/src/mscorlib/src/System/IO/UnmanagedMemoryStream.cs b/src/mscorlib/src/System/IO/UnmanagedMemoryStream.cs index 165b6d2b19..f21fe47371 100644 --- a/src/mscorlib/src/System/IO/UnmanagedMemoryStream.cs +++ b/src/mscorlib/src/System/IO/UnmanagedMemoryStream.cs @@ -12,6 +12,7 @@ ** ** ===========================================================*/ + using System; using System.Runtime; using System.Runtime.CompilerServices; @@ -20,11 +21,11 @@ using System.Security; using System.Threading; using System.Diagnostics; using System.Diagnostics.Contracts; -using System.Threading.Tasks; - +using System.Threading.Tasks; -namespace System.IO { +namespace System.IO +{ /* * This class is used to access a contiguous block of memory, likely outside * the GC heap (or pinned in place in the GC heap, but a MemoryStream may @@ -93,61 +94,76 @@ namespace System.IO { private long _offset; private FileAccess _access; internal bool _isOpen; - [NonSerialized] + [NonSerialized] private Task<Int32> _lastReadTask; // The last successful task returned from ReadAsync // Needed for subclasses that need to map a file, etc. protected UnmanagedMemoryStream() { - unsafe { + unsafe + { _mem = null; } _isOpen = false; } - public UnmanagedMemoryStream(SafeBuffer buffer, long offset, long length) { + public UnmanagedMemoryStream(SafeBuffer buffer, long offset, long length) + { Initialize(buffer, offset, length, FileAccess.Read); } - public UnmanagedMemoryStream(SafeBuffer buffer, long offset, long length, FileAccess access) { + public UnmanagedMemoryStream(SafeBuffer buffer, long offset, long length, FileAccess access) + { Initialize(buffer, offset, length, access); } - protected void Initialize(SafeBuffer buffer, long offset, long length, FileAccess access) { - if (buffer == null) { + protected void Initialize(SafeBuffer buffer, long offset, long length, FileAccess access) + { + if (buffer == null) + { throw new ArgumentNullException(nameof(buffer)); } - if (offset < 0) { - throw new ArgumentOutOfRangeException(nameof(offset), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + if (offset < 0) + { + throw new ArgumentOutOfRangeException(nameof(offset), SR.ArgumentOutOfRange_NeedNonNegNum); } - if (length < 0) { - throw new ArgumentOutOfRangeException(nameof(length), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + if (length < 0) + { + throw new ArgumentOutOfRangeException(nameof(length), SR.ArgumentOutOfRange_NeedNonNegNum); } - if (buffer.ByteLength < (ulong)(offset + length)) { - throw new ArgumentException(Environment.GetResourceString("Argument_InvalidSafeBufferOffLen")); + if (buffer.ByteLength < (ulong)(offset + length)) + { + throw new ArgumentException(SR.Argument_InvalidSafeBufferOffLen); } - if (access < FileAccess.Read || access > FileAccess.ReadWrite) { + if (access < FileAccess.Read || access > FileAccess.ReadWrite) + { throw new ArgumentOutOfRangeException(nameof(access)); } Contract.EndContractBlock(); - if (_isOpen) { - throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_CalledTwice")); + if (_isOpen) + { + throw new InvalidOperationException(SR.InvalidOperation_CalledTwice); } // check for wraparound - unsafe { + unsafe + { byte* pointer = null; RuntimeHelpers.PrepareConstrainedRegions(); - try { + try + { buffer.AcquirePointer(ref pointer); - if ( (pointer + offset + length) < pointer) { - throw new ArgumentException(Environment.GetResourceString("ArgumentOutOfRange_UnmanagedMemStreamWrapAround")); + if ((pointer + offset + length) < pointer) + { + throw new ArgumentException(SR.ArgumentOutOfRange_UnmanagedMemStreamWrapAround); } } - finally { - if (pointer != null) { + finally + { + if (pointer != null) + { buffer.ReleasePointer(); } } @@ -168,28 +184,28 @@ namespace System.IO { } [CLSCompliant(false)] - public unsafe UnmanagedMemoryStream(byte* pointer, long length, long capacity, FileAccess access) + public unsafe UnmanagedMemoryStream(byte* pointer, long length, long capacity, FileAccess access) { Initialize(pointer, length, capacity, access); } [CLSCompliant(false)] - protected unsafe void Initialize(byte* pointer, long length, long capacity, FileAccess access) + protected unsafe void Initialize(byte* pointer, long length, long capacity, FileAccess access) { if (pointer == null) throw new ArgumentNullException(nameof(pointer)); if (length < 0 || capacity < 0) - throw new ArgumentOutOfRangeException((length < 0) ? nameof(length) : nameof(capacity), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + throw new ArgumentOutOfRangeException((length < 0) ? nameof(length) : nameof(capacity), SR.ArgumentOutOfRange_NeedNonNegNum); if (length > capacity) - throw new ArgumentOutOfRangeException(nameof(length), Environment.GetResourceString("ArgumentOutOfRange_LengthGreaterThanCapacity")); + throw new ArgumentOutOfRangeException(nameof(length), SR.ArgumentOutOfRange_LengthGreaterThanCapacity); Contract.EndContractBlock(); // Check for wraparound. - if (((byte*) ((long)pointer + capacity)) < pointer) - throw new ArgumentOutOfRangeException(nameof(capacity), Environment.GetResourceString("ArgumentOutOfRange_UnmanagedMemStreamWrapAround")); + if (((byte*)((long)pointer + capacity)) < pointer) + throw new ArgumentOutOfRangeException(nameof(capacity), SR.ArgumentOutOfRange_UnmanagedMemStreamWrapAround); if (access < FileAccess.Read || access > FileAccess.ReadWrite) - throw new ArgumentOutOfRangeException(nameof(access), Environment.GetResourceString("ArgumentOutOfRange_Enum")); + throw new ArgumentOutOfRangeException(nameof(access), SR.ArgumentOutOfRange_Enum); if (_isOpen) - throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_CalledTwice")); + throw new InvalidOperationException(SR.InvalidOperation_CalledTwice); _mem = pointer; _offset = 0; @@ -199,17 +215,20 @@ namespace System.IO { _isOpen = true; } - public override bool CanRead { + public override bool CanRead + { [Pure] get { return _isOpen && (_access & FileAccess.Read) != 0; } } - public override bool CanSeek { + public override bool CanSeek + { [Pure] get { return _isOpen; } } - public override bool CanWrite { + public override bool CanWrite + { [Pure] get { return _isOpen && (_access & FileAccess.Write) != 0; } } @@ -225,58 +244,66 @@ namespace System.IO { base.Dispose(disposing); } - public override void Flush() { + public override void Flush() + { if (!_isOpen) __Error.StreamIsClosed(); } - - public override Task FlushAsync(CancellationToken cancellationToken) { - - if (cancellationToken.IsCancellationRequested) - return Task.FromCanceled(cancellationToken); - - try { - - Flush(); - return Task.CompletedTask; - - } catch(Exception ex) { - - return Task.FromException(ex); - } - } - - - public override long Length { - get { + + public override Task FlushAsync(CancellationToken cancellationToken) + { + if (cancellationToken.IsCancellationRequested) + return Task.FromCanceled(cancellationToken); + + try + { + Flush(); + return Task.CompletedTask; + } + catch (Exception ex) + { + return Task.FromException(ex); + } + } + + + public override long Length + { + get + { if (!_isOpen) __Error.StreamIsClosed(); return Interlocked.Read(ref _length); } } - public long Capacity { - get { + public long Capacity + { + get + { if (!_isOpen) __Error.StreamIsClosed(); return _capacity; } } - public override long Position { - get { + public override long Position + { + get + { if (!CanSeek) __Error.StreamIsClosed(); Contract.EndContractBlock(); return Interlocked.Read(ref _position); } - set { + set + { if (value < 0) - throw new ArgumentOutOfRangeException(nameof(value), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + throw new ArgumentOutOfRangeException(nameof(value), SR.ArgumentOutOfRange_NeedNonNegNum); Contract.EndContractBlock(); if (!CanSeek) __Error.StreamIsClosed(); - + #if !BIT64 unsafe { // On 32 bit machines, ensure we don't wrap around. if (value > (long) Int32.MaxValue || _mem + value < _mem) - throw new ArgumentOutOfRangeException(nameof(value), Environment.GetResourceString("ArgumentOutOfRange_StreamLength")); + throw new ArgumentOutOfRangeException(nameof(value), SR.ArgumentOutOfRange_StreamLength); } #endif Interlocked.Exchange(ref _position, value); @@ -284,54 +311,61 @@ namespace System.IO { } [CLSCompliant(false)] - public unsafe byte* PositionPointer { - get { - if (_buffer != null) { - throw new NotSupportedException(Environment.GetResourceString("NotSupported_UmsSafeBuffer")); + public unsafe byte* PositionPointer + { + get + { + if (_buffer != null) + { + throw new NotSupportedException(SR.NotSupported_UmsSafeBuffer); } // Use a temp to avoid a race long pos = Interlocked.Read(ref _position); if (pos > _capacity) - throw new IndexOutOfRangeException(Environment.GetResourceString("IndexOutOfRange_UMSPosition")); - byte * ptr = _mem + pos; + throw new IndexOutOfRangeException(SR.IndexOutOfRange_UMSPosition); + byte* ptr = _mem + pos; if (!_isOpen) __Error.StreamIsClosed(); return ptr; } - set { + set + { if (_buffer != null) - throw new NotSupportedException(Environment.GetResourceString("NotSupported_UmsSafeBuffer")); + throw new NotSupportedException(SR.NotSupported_UmsSafeBuffer); if (!_isOpen) __Error.StreamIsClosed(); // Note: subtracting pointers returns an Int64. Working around // to avoid hitting compiler warning CS0652 on this line. if (new IntPtr(value - _mem).ToInt64() > UnmanagedMemStreamMaxLength) - throw new ArgumentOutOfRangeException("offset", Environment.GetResourceString("ArgumentOutOfRange_UnmanagedMemStreamLength")); + throw new ArgumentOutOfRangeException("offset", SR.ArgumentOutOfRange_UnmanagedMemStreamLength); if (value < _mem) - throw new IOException(Environment.GetResourceString("IO.IO_SeekBeforeBegin")); + throw new IOException(SR.IO_SeekBeforeBegin); Interlocked.Exchange(ref _position, value - _mem); } } - internal unsafe byte* Pointer { - get { + internal unsafe byte* Pointer + { + get + { if (_buffer != null) - throw new NotSupportedException(Environment.GetResourceString("NotSupported_UmsSafeBuffer")); + throw new NotSupportedException(SR.NotSupported_UmsSafeBuffer); return _mem; } } - - public override int Read([In, Out] byte[] buffer, int offset, int count) { - if (buffer==null) - throw new ArgumentNullException(nameof(buffer), Environment.GetResourceString("ArgumentNull_Buffer")); + + public override int Read([In, Out] byte[] buffer, int offset, int count) + { + if (buffer == null) + throw new ArgumentNullException(nameof(buffer), SR.ArgumentNull_Buffer); if (offset < 0) - throw new ArgumentOutOfRangeException(nameof(offset), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + throw new ArgumentOutOfRangeException(nameof(offset), SR.ArgumentOutOfRange_NeedNonNegNum); if (count < 0) - throw new ArgumentOutOfRangeException(nameof(count), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_NeedNonNegNum); if (buffer.Length - offset < count) - throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); + throw new ArgumentException(SR.Argument_InvalidOffLen); Contract.EndContractBlock(); // Keep this in sync with contract validation in ReadAsync if (!_isOpen) __Error.StreamIsClosed(); @@ -347,7 +381,7 @@ namespace System.IO { if (n <= 0) return 0; - int nInt = (int) n; // Safe because n <= count, which is an Int32 + int nInt = (int)n; // Safe because n <= count, which is an Int32 if (nInt < 0) return 0; // _position could be beyond EOF Debug.Assert(pos + nInt >= 0, "_position + n >= 0"); // len is less than 2^63 -1. @@ -383,35 +417,37 @@ namespace System.IO { Interlocked.Exchange(ref _position, pos + n); return nInt; } - - public override Task<Int32> ReadAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken) { - if (buffer==null) - throw new ArgumentNullException(nameof(buffer), Environment.GetResourceString("ArgumentNull_Buffer")); + + public override Task<Int32> ReadAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken) + { + if (buffer == null) + throw new ArgumentNullException(nameof(buffer), SR.ArgumentNull_Buffer); if (offset < 0) - throw new ArgumentOutOfRangeException(nameof(offset), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + throw new ArgumentOutOfRangeException(nameof(offset), SR.ArgumentOutOfRange_NeedNonNegNum); if (count < 0) - throw new ArgumentOutOfRangeException(nameof(count), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_NeedNonNegNum); if (buffer.Length - offset < count) - throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); + throw new ArgumentException(SR.Argument_InvalidOffLen); Contract.EndContractBlock(); // contract validation copied from Read(...) - - if (cancellationToken.IsCancellationRequested) - return Task.FromCanceled<Int32>(cancellationToken); - - try { - - Int32 n = Read(buffer, offset, count); + + if (cancellationToken.IsCancellationRequested) + return Task.FromCanceled<Int32>(cancellationToken); + + try + { + Int32 n = Read(buffer, offset, count); Task<Int32> t = _lastReadTask; - return (t != null && t.Result == n) ? t : (_lastReadTask = Task.FromResult<Int32>(n)); - - } catch (Exception ex) { - - Debug.Assert(! (ex is OperationCanceledException)); - return Task.FromException<Int32>(ex); - } - } - - public override int ReadByte() { + return (t != null && t.Result == n) ? t : (_lastReadTask = Task.FromResult<Int32>(n)); + } + catch (Exception ex) + { + Debug.Assert(!(ex is OperationCanceledException)); + return Task.FromException<Int32>(ex); + } + } + + public override int ReadByte() + { if (!_isOpen) __Error.StreamIsClosed(); if (!CanRead) __Error.ReadNotSupported(); @@ -421,56 +457,65 @@ namespace System.IO { return -1; Interlocked.Exchange(ref _position, pos + 1); int result; - if (_buffer != null) { - unsafe { + if (_buffer != null) + { + unsafe + { byte* pointer = null; RuntimeHelpers.PrepareConstrainedRegions(); - try { + try + { _buffer.AcquirePointer(ref pointer); result = *(pointer + pos + _offset); } - finally { - if (pointer != null) { + finally + { + if (pointer != null) + { _buffer.ReleasePointer(); } } } } - else { - unsafe { + else + { + unsafe + { result = _mem[pos]; } } return result; } - public override long Seek(long offset, SeekOrigin loc) { + public override long Seek(long offset, SeekOrigin loc) + { if (!_isOpen) __Error.StreamIsClosed(); if (offset > UnmanagedMemStreamMaxLength) - throw new ArgumentOutOfRangeException(nameof(offset), Environment.GetResourceString("ArgumentOutOfRange_UnmanagedMemStreamLength")); - switch(loc) { - case SeekOrigin.Begin: - if (offset < 0) - throw new IOException(Environment.GetResourceString("IO.IO_SeekBeforeBegin")); - Interlocked.Exchange(ref _position, offset); - break; - - case SeekOrigin.Current: - long pos = Interlocked.Read(ref _position); - if (offset + pos < 0) - throw new IOException(Environment.GetResourceString("IO.IO_SeekBeforeBegin")); - Interlocked.Exchange(ref _position, offset + pos); - break; - - case SeekOrigin.End: - long len = Interlocked.Read(ref _length); - if (len + offset < 0) - throw new IOException(Environment.GetResourceString("IO.IO_SeekBeforeBegin")); - Interlocked.Exchange(ref _position, len + offset); - break; - - default: - throw new ArgumentException(Environment.GetResourceString("Argument_InvalidSeekOrigin")); + throw new ArgumentOutOfRangeException(nameof(offset), SR.ArgumentOutOfRange_UnmanagedMemStreamLength); + switch (loc) + { + case SeekOrigin.Begin: + if (offset < 0) + throw new IOException(SR.IO_SeekBeforeBegin); + Interlocked.Exchange(ref _position, offset); + break; + + case SeekOrigin.Current: + long pos = Interlocked.Read(ref _position); + if (offset + pos < 0) + throw new IOException(SR.IO_SeekBeforeBegin); + Interlocked.Exchange(ref _position, offset + pos); + break; + + case SeekOrigin.End: + long len = Interlocked.Read(ref _length); + if (len + offset < 0) + throw new IOException(SR.IO_SeekBeforeBegin); + Interlocked.Exchange(ref _position, len + offset); + break; + + default: + throw new ArgumentException(SR.Argument_InvalidSeekOrigin); } long finalPos = Interlocked.Read(ref _position); @@ -478,40 +523,45 @@ namespace System.IO { return finalPos; } - public override void SetLength(long value) { + public override void SetLength(long value) + { if (value < 0) - throw new ArgumentOutOfRangeException("length", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + throw new ArgumentOutOfRangeException("length", SR.ArgumentOutOfRange_NeedNonNegNum); Contract.EndContractBlock(); if (_buffer != null) - throw new NotSupportedException(Environment.GetResourceString("NotSupported_UmsSafeBuffer")); + throw new NotSupportedException(SR.NotSupported_UmsSafeBuffer); if (!_isOpen) __Error.StreamIsClosed(); if (!CanWrite) __Error.WriteNotSupported(); if (value > _capacity) - throw new IOException(Environment.GetResourceString("IO.IO_FixedCapacity")); + throw new IOException(SR.IO_FixedCapacity); long pos = Interlocked.Read(ref _position); long len = Interlocked.Read(ref _length); - if (value > len) { - unsafe { - Buffer.ZeroMemory(_mem+len, value-len); + if (value > len) + { + unsafe + { + Buffer.ZeroMemory(_mem + len, value - len); } } Interlocked.Exchange(ref _length, value); - if (pos > value) { + if (pos > value) + { Interlocked.Exchange(ref _position, value); - } + } } - public override void Write(byte[] buffer, int offset, int count) { - if (buffer==null) - throw new ArgumentNullException(nameof(buffer), Environment.GetResourceString("ArgumentNull_Buffer")); + public override void Write(byte[] buffer, int offset, int count) + { + if (buffer == null) + throw new ArgumentNullException(nameof(buffer), SR.ArgumentNull_Buffer); if (offset < 0) - throw new ArgumentOutOfRangeException(nameof(offset), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + throw new ArgumentOutOfRangeException(nameof(offset), SR.ArgumentOutOfRange_NeedNonNegNum); if (count < 0) - throw new ArgumentOutOfRangeException(nameof(count), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_NeedNonNegNum); if (buffer.Length - offset < count) - throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); + throw new ArgumentException(SR.Argument_InvalidOffLen); Contract.EndContractBlock(); // Keep contract validation in sync with WriteAsync(..) if (!_isOpen) __Error.StreamIsClosed(); @@ -522,23 +572,28 @@ namespace System.IO { long n = pos + count; // Check for overflow if (n < 0) - throw new IOException(Environment.GetResourceString("IO.IO_StreamTooLong")); + throw new IOException(SR.IO_StreamTooLong); - if (n > _capacity) { - throw new NotSupportedException(Environment.GetResourceString("IO.IO_FixedCapacity")); + if (n > _capacity) + { + throw new NotSupportedException(SR.IO_FixedCapacity); } - if (_buffer == null) { + if (_buffer == null) + { // Check to see whether we are now expanding the stream and must // zero any memory in the middle. - if (pos > len) { - unsafe { - Buffer.ZeroMemory(_mem+len, pos-len); + if (pos > len) + { + unsafe + { + Buffer.ZeroMemory(_mem + len, pos - len); } } // set length after zeroing memory to avoid race condition of accessing unzeroed memory - if (n > len) { + if (n > len) + { Interlocked.Exchange(ref _length, n); } } @@ -552,7 +607,7 @@ namespace System.IO { long bytesLeft = _capacity - pos; if (bytesLeft < count) { - throw new ArgumentException(Environment.GetResourceString("Arg_BufferTooSmall")); + throw new ArgumentException(SR.Arg_BufferTooSmall); } byte* pointer = null; @@ -579,57 +634,62 @@ namespace System.IO { Interlocked.Exchange(ref _position, n); return; } - - public override Task WriteAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken) { - - if (buffer==null) - throw new ArgumentNullException(nameof(buffer), Environment.GetResourceString("ArgumentNull_Buffer")); + + public override Task WriteAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken) + { + if (buffer == null) + throw new ArgumentNullException(nameof(buffer), SR.ArgumentNull_Buffer); if (offset < 0) - throw new ArgumentOutOfRangeException(nameof(offset), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + throw new ArgumentOutOfRangeException(nameof(offset), SR.ArgumentOutOfRange_NeedNonNegNum); if (count < 0) - throw new ArgumentOutOfRangeException(nameof(count), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_NeedNonNegNum); if (buffer.Length - offset < count) - throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); + throw new ArgumentException(SR.Argument_InvalidOffLen); Contract.EndContractBlock(); // contract validation copied from Write(..) - - if (cancellationToken.IsCancellationRequested) - return Task.FromCanceled(cancellationToken); - - try { - - Write(buffer, offset, count); - return Task.CompletedTask; - - } catch (Exception ex) { - - Debug.Assert(! (ex is OperationCanceledException)); - return Task.FromException<Int32>(ex); - } - } - - - public override void WriteByte(byte value) { + + if (cancellationToken.IsCancellationRequested) + return Task.FromCanceled(cancellationToken); + + try + { + Write(buffer, offset, count); + return Task.CompletedTask; + } + catch (Exception ex) + { + Debug.Assert(!(ex is OperationCanceledException)); + return Task.FromException<Int32>(ex); + } + } + + + public override void WriteByte(byte value) + { if (!_isOpen) __Error.StreamIsClosed(); if (!CanWrite) __Error.WriteNotSupported(); 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) { + if (pos >= len) + { // Check for overflow if (n < 0) - throw new IOException(Environment.GetResourceString("IO.IO_StreamTooLong")); - + throw new IOException(SR.IO_StreamTooLong); + if (n > _capacity) - throw new NotSupportedException(Environment.GetResourceString("IO.IO_FixedCapacity")); + throw new NotSupportedException(SR.IO_FixedCapacity); // Check to see whether we are now expanding the stream and must // zero any memory in the middle. // don't do if created from SafeBuffer - if (_buffer == null) { - if (pos > len) { - unsafe { - Buffer.ZeroMemory(_mem+len, pos-len); + if (_buffer == null) + { + if (pos > len) + { + unsafe + { + Buffer.ZeroMemory(_mem + len, pos - len); } } @@ -638,24 +698,31 @@ namespace System.IO { } } - if (_buffer != null) { - unsafe { + if (_buffer != null) + { + unsafe + { byte* pointer = null; RuntimeHelpers.PrepareConstrainedRegions(); - try { + try + { _buffer.AcquirePointer(ref pointer); *(pointer + pos + _offset) = value; } - finally { - if (pointer != null) { + finally + { + if (pointer != null) + { _buffer.ReleasePointer(); } } } } - else { - unsafe { - _mem[pos] = value; + else + { + unsafe + { + _mem[pos] = value; } } Interlocked.Exchange(ref _position, n); |