ReadSpan(new Span<byte>(array, offset, count));
}
- public override int Read(Span<byte> destination)
+ public override int Read(Span<byte> buffer)
{
if (GetType() == typeof(FileStream) && !_useAsyncIO)
{
{
throw Error.GetFileNotOpen();
}
- return ReadSpan(destination);
+ return ReadSpan(buffer);
}
else
{
// of Read(byte[],int,int) overload. Or if the stream is in async mode, we can't call the
// synchronous ReadSpan, so we similarly call the base Read, which will turn delegate to
// Read(byte[],int,int), which will do the right thing if we're in async mode.
- return base.Read(destination);
+ return base.Read(buffer);
}
}
return ReadAsyncTask(buffer, offset, count, cancellationToken);
}
- public override ValueTask<int> ReadAsync(Memory<byte> destination, CancellationToken cancellationToken = default(CancellationToken))
+ public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default(CancellationToken))
{
if (!_useAsyncIO || GetType() != typeof(FileStream))
{
// If we're not using async I/O, delegate to the base, which will queue a call to Read.
// Or if this isn't a concrete FileStream, a derived type may have overridden ReadAsync(byte[],...),
// which was introduced first, so delegate to the base which will delegate to that.
- return base.ReadAsync(destination, cancellationToken);
+ return base.ReadAsync(buffer, cancellationToken);
}
if (cancellationToken.IsCancellationRequested)
throw Error.GetFileNotOpen();
}
- Task<int> t = ReadAsyncInternal(destination, cancellationToken, out int synchronousResult);
+ Task<int> t = ReadAsyncInternal(buffer, cancellationToken, out int synchronousResult);
return t != null ?
new ValueTask<int>(t) :
new ValueTask<int>(synchronousResult);
}
}
- public override void Write(ReadOnlySpan<byte> destination)
+ public override void Write(ReadOnlySpan<byte> buffer)
{
if (GetType() == typeof(FileStream) && !_useAsyncIO)
{
{
throw Error.GetFileNotOpen();
}
- WriteSpan(destination);
+ WriteSpan(buffer);
}
else
{
// of Write(byte[],int,int) overload. Or if the stream is in async mode, we can't call the
// synchronous WriteSpan, so we similarly call the base Write, which will turn delegate to
// Write(byte[],int,int), which will do the right thing if we're in async mode.
- base.Write(destination);
+ base.Write(buffer);
}
}
return WriteAsyncInternal(new ReadOnlyMemory<byte>(buffer, offset, count), cancellationToken).AsTask();
}
- public override ValueTask WriteAsync(ReadOnlyMemory<byte> source, CancellationToken cancellationToken = default(CancellationToken))
+ public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default(CancellationToken))
{
if (!_useAsyncIO || GetType() != typeof(FileStream))
{
// If we're not using async I/O, delegate to the base, which will queue a call to Write.
// Or if this isn't a concrete FileStream, a derived type may have overridden WriteAsync(byte[],...),
// which was introduced first, so delegate to the base which will delegate to that.
- return base.WriteAsync(source, cancellationToken);
+ return base.WriteAsync(buffer, cancellationToken);
}
if (cancellationToken.IsCancellationRequested)
throw Error.GetFileNotOpen();
}
- return WriteAsyncInternal(source, cancellationToken);
+ return WriteAsyncInternal(buffer, cancellationToken);
}
/// <summary>
return n;
}
- public override int Read(Span<byte> destination)
+ public override int Read(Span<byte> buffer)
{
if (GetType() != typeof(MemoryStream))
{
// MemoryStream is not sealed, and a derived type may have overridden Read(byte[], int, int) prior
// to this Read(Span<byte>) overload being introduced. In that case, this Read(Span<byte>) overload
// should use the behavior of Read(byte[],int,int) overload.
- return base.Read(destination);
+ return base.Read(buffer);
}
EnsureNotClosed();
- int n = Math.Min(_length - _position, destination.Length);
+ int n = Math.Min(_length - _position, buffer.Length);
if (n <= 0)
return 0;
// Read(byte[], int, int) has an n <= 8 optimization, presumably based
// on benchmarking. Determine if/where such a cut-off is here and add
// an equivalent optimization if necessary.
- new Span<byte>(_buffer, _position, n).CopyTo(destination);
+ new Span<byte>(_buffer, _position, n).CopyTo(buffer);
_position += n;
return n;
}
}
- public override ValueTask<int> ReadAsync(Memory<byte> destination, CancellationToken cancellationToken = default(CancellationToken))
+ public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default(CancellationToken))
{
if (cancellationToken.IsCancellationRequested)
{
// something other than an array and this is a MemoryStream-derived type that doesn't override Read(Span<byte>) will
// it then fall back to doing the ArrayPool/copy behavior.
return new ValueTask<int>(
- MemoryMarshal.TryGetArray(destination, out ArraySegment<byte> destinationArray) ?
+ MemoryMarshal.TryGetArray(buffer, out ArraySegment<byte> destinationArray) ?
Read(destinationArray.Array, destinationArray.Offset, destinationArray.Count) :
- Read(destination.Span));
+ Read(buffer.Span));
}
catch (OperationCanceledException oce)
{
_position = i;
}
- public override void Write(ReadOnlySpan<byte> source)
+ public override void Write(ReadOnlySpan<byte> buffer)
{
if (GetType() != typeof(MemoryStream))
{
// MemoryStream is not sealed, and a derived type may have overridden Write(byte[], int, int) prior
// to this Write(Span<byte>) overload being introduced. In that case, this Write(Span<byte>) overload
// should use the behavior of Write(byte[],int,int) overload.
- base.Write(source);
+ base.Write(buffer);
return;
}
EnsureWriteable();
// Check for overflow
- int i = _position + source.Length;
+ int i = _position + buffer.Length;
if (i < 0)
throw new IOException(SR.IO_StreamTooLong);
_length = i;
}
- source.CopyTo(new Span<byte>(_buffer, _position, source.Length));
+ buffer.CopyTo(new Span<byte>(_buffer, _position, buffer.Length));
_position = i;
}
}
}
- public override ValueTask WriteAsync(ReadOnlyMemory<byte> source, CancellationToken cancellationToken = default(CancellationToken))
+ public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default(CancellationToken))
{
if (cancellationToken.IsCancellationRequested)
{
{
// See corresponding comment in ReadAsync for why we don't just always use Write(ReadOnlySpan<byte>).
// Unlike ReadAsync, we could delegate to WriteAsync(byte[], ...) here, but we don't for consistency.
- if (MemoryMarshal.TryGetArray(source, out ArraySegment<byte> sourceArray))
+ if (MemoryMarshal.TryGetArray(buffer, out ArraySegment<byte> sourceArray))
{
Write(sourceArray.Array, sourceArray.Offset, sourceArray.Count);
}
else
{
- Write(source.Span);
+ Write(buffer.Span);
}
return default;
}
Initialize(ptr, len, len, FileAccess.Read);
}
- public override int Read(Span<byte> destination) => ReadCore(destination);
+ public override int Read(Span<byte> buffer) => ReadCore(buffer);
- public override void Write(ReadOnlySpan<byte> source) => WriteCore(source);
+ public override void Write(ReadOnlySpan<byte> buffer) => WriteCore(buffer);
~PinnedBufferMemoryStream()
{
return ReadCore(new Span<byte>(buffer, offset, count));
}
- public override int Read(Span<byte> destination)
+ public override int Read(Span<byte> buffer)
{
if (GetType() == typeof(UnmanagedMemoryStream))
{
- return ReadCore(destination);
+ return ReadCore(buffer);
}
else
{
// UnmanagedMemoryStream is not sealed, and a derived type may have overridden Read(byte[], int, int) prior
// to this Read(Span<byte>) overload being introduced. In that case, this Read(Span<byte>) overload
// should use the behavior of Read(byte[],int,int) overload.
- return base.Read(destination);
+ return base.Read(buffer);
}
}
- internal int ReadCore(Span<byte> destination)
+ internal int ReadCore(Span<byte> buffer)
{
EnsureNotClosed();
EnsureReadable();
// changes our position after we decide we can read some bytes.
long pos = Interlocked.Read(ref _position);
long len = Interlocked.Read(ref _length);
- long n = Math.Min(len - pos, destination.Length);
+ long n = Math.Min(len - pos, buffer.Length);
if (n <= 0)
{
return 0;
unsafe
{
- fixed (byte* pBuffer = &MemoryMarshal.GetReference(destination))
+ fixed (byte* pBuffer = &MemoryMarshal.GetReference(buffer))
{
if (_buffer != null)
{
/// <summary>
/// Reads bytes from stream and puts them into the buffer
/// </summary>
- /// <param name="destination">Buffer to read the bytes to.</param>
+ /// <param name="buffer">Buffer to read the bytes to.</param>
/// <param name="cancellationToken">Token that can be used to cancel this operation.</param>
- public override ValueTask<int> ReadAsync(Memory<byte> destination, CancellationToken cancellationToken = default(CancellationToken))
+ public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default(CancellationToken))
{
if (cancellationToken.IsCancellationRequested)
{
// something other than an array and this is an UnmanagedMemoryStream-derived type that doesn't override Read(Span<byte>) will
// it then fall back to doing the ArrayPool/copy behavior.
return new ValueTask<int>(
- MemoryMarshal.TryGetArray(destination, out ArraySegment<byte> destinationArray) ?
+ MemoryMarshal.TryGetArray(buffer, out ArraySegment<byte> destinationArray) ?
Read(destinationArray.Array, destinationArray.Offset, destinationArray.Count) :
- Read(destination.Span));
+ Read(buffer.Span));
}
catch (Exception ex)
{
WriteCore(new Span<byte>(buffer, offset, count));
}
- public override void Write(ReadOnlySpan<byte> source)
+ public override void Write(ReadOnlySpan<byte> buffer)
{
if (GetType() == typeof(UnmanagedMemoryStream))
{
- WriteCore(source);
+ WriteCore(buffer);
}
else
{
// UnmanagedMemoryStream is not sealed, and a derived type may have overridden Write(byte[], int, int) prior
// to this Write(Span<byte>) overload being introduced. In that case, this Write(Span<byte>) overload
// should use the behavior of Write(byte[],int,int) overload.
- base.Write(source);
+ base.Write(buffer);
}
}
- internal unsafe void WriteCore(ReadOnlySpan<byte> source)
+ internal unsafe void WriteCore(ReadOnlySpan<byte> buffer)
{
EnsureNotClosed();
EnsureWriteable();
long pos = Interlocked.Read(ref _position); // Use a local to avoid a race condition
long len = Interlocked.Read(ref _length);
- long n = pos + source.Length;
+ long n = pos + buffer.Length;
// Check for overflow
if (n < 0)
{
}
}
- fixed (byte* pBuffer = &MemoryMarshal.GetReference(source))
+ fixed (byte* pBuffer = &MemoryMarshal.GetReference(buffer))
{
if (_buffer != null)
{
long bytesLeft = _capacity - pos;
- if (bytesLeft < source.Length)
+ if (bytesLeft < buffer.Length)
{
throw new ArgumentException(SR.Arg_BufferTooSmall);
}
try
{
_buffer.AcquirePointer(ref pointer);
- Buffer.Memcpy(pointer + pos + _offset, pBuffer, source.Length);
+ Buffer.Memcpy(pointer + pos + _offset, pBuffer, buffer.Length);
}
finally
{
}
else
{
- Buffer.Memcpy(_mem + pos, pBuffer, source.Length);
+ Buffer.Memcpy(_mem + pos, pBuffer, buffer.Length);
}
}
/// </summary>
/// <param name="buffer">Buffer that will be written.</param>
/// <param name="cancellationToken">Token that can be used to cancel the operation.</param>
- public override ValueTask WriteAsync(ReadOnlyMemory<byte> source, CancellationToken cancellationToken = default(CancellationToken))
+ public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default(CancellationToken))
{
if (cancellationToken.IsCancellationRequested)
{
{
// See corresponding comment in ReadAsync for why we don't just always use Write(ReadOnlySpan<byte>).
// Unlike ReadAsync, we could delegate to WriteAsync(byte[], ...) here, but we don't for consistency.
- if (MemoryMarshal.TryGetArray(source, out ArraySegment<byte> sourceArray))
+ if (MemoryMarshal.TryGetArray(buffer, out ArraySegment<byte> sourceArray))
{
Write(sourceArray.Array, sourceArray.Offset, sourceArray.Count);
}
else
{
- Write(source.Span);
+ Write(buffer.Span);
}
return default;
}
return _unmanagedStream.Read(buffer, offset, count);
}
- public override int Read(Span<byte> destination)
+ public override int Read(Span<byte> buffer)
{
- return _unmanagedStream.Read(destination);
+ return _unmanagedStream.Read(buffer);
}
public override int ReadByte()
_unmanagedStream.Write(buffer, offset, count);
}
- public override void Write(ReadOnlySpan<byte> source)
+ public override void Write(ReadOnlySpan<byte> buffer)
{
- _unmanagedStream.Write(source);
+ _unmanagedStream.Write(buffer);
}
public override void WriteByte(byte value)
return _unmanagedStream.ReadAsync(buffer, offset, count, cancellationToken);
}
- public override ValueTask<int> ReadAsync(Memory<byte> destination, CancellationToken cancellationToken = default(CancellationToken))
+ public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default(CancellationToken))
{
- return _unmanagedStream.ReadAsync(destination, cancellationToken);
+ return _unmanagedStream.ReadAsync(buffer, cancellationToken);
}
return _unmanagedStream.WriteAsync(buffer, offset, count, cancellationToken);
}
- public override ValueTask WriteAsync(ReadOnlyMemory<byte> source, CancellationToken cancellationToken = default(CancellationToken))
+ public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default(CancellationToken))
{
- return _unmanagedStream.WriteAsync(source, cancellationToken);
+ return _unmanagedStream.WriteAsync(buffer, cancellationToken);
}
} // class UnmanagedMemoryStreamWrapper
} // namespace
: BeginEndReadAsync(buffer, offset, count);
}
- public virtual ValueTask<int> ReadAsync(Memory<byte> destination, CancellationToken cancellationToken = default(CancellationToken))
+ public virtual ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default(CancellationToken))
{
- if (MemoryMarshal.TryGetArray(destination, out ArraySegment<byte> array))
+ if (MemoryMarshal.TryGetArray(buffer, out ArraySegment<byte> array))
{
return new ValueTask<int>(ReadAsync(array.Array, array.Offset, array.Count, cancellationToken));
}
else
{
- byte[] buffer = ArrayPool<byte>.Shared.Rent(destination.Length);
- return FinishReadAsync(ReadAsync(buffer, 0, destination.Length, cancellationToken), buffer, destination);
+ byte[] sharedBuffer = ArrayPool<byte>.Shared.Rent(buffer.Length);
+ return FinishReadAsync(ReadAsync(sharedBuffer, 0, buffer.Length, cancellationToken), sharedBuffer, buffer);
async ValueTask<int> FinishReadAsync(Task<int> readTask, byte[] localBuffer, Memory<byte> localDestination)
{
: BeginEndWriteAsync(buffer, offset, count);
}
- public virtual ValueTask WriteAsync(ReadOnlyMemory<byte> source, CancellationToken cancellationToken = default(CancellationToken))
+ public virtual ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default(CancellationToken))
{
- if (MemoryMarshal.TryGetArray(source, out ArraySegment<byte> array))
+ if (MemoryMarshal.TryGetArray(buffer, out ArraySegment<byte> array))
{
return new ValueTask(WriteAsync(array.Array, array.Offset, array.Count, cancellationToken));
}
else
{
- byte[] buffer = ArrayPool<byte>.Shared.Rent(source.Length);
- source.Span.CopyTo(buffer);
- return new ValueTask(FinishWriteAsync(WriteAsync(buffer, 0, source.Length, cancellationToken), buffer));
+ byte[] sharedBuffer = ArrayPool<byte>.Shared.Rent(buffer.Length);
+ buffer.Span.CopyTo(sharedBuffer);
+ return new ValueTask(FinishWriteAsync(WriteAsync(sharedBuffer, 0, buffer.Length, cancellationToken), sharedBuffer));
}
}
public abstract int Read(byte[] buffer, int offset, int count);
- public virtual int Read(Span<byte> destination)
+ public virtual int Read(Span<byte> buffer)
{
- byte[] buffer = ArrayPool<byte>.Shared.Rent(destination.Length);
+ byte[] sharedBuffer = ArrayPool<byte>.Shared.Rent(buffer.Length);
try
{
- int numRead = Read(buffer, 0, destination.Length);
- if ((uint)numRead > destination.Length)
+ int numRead = Read(sharedBuffer, 0, buffer.Length);
+ if ((uint)numRead > buffer.Length)
{
throw new IOException(SR.IO_StreamTooLong);
}
- new Span<byte>(buffer, 0, numRead).CopyTo(destination);
+ new Span<byte>(sharedBuffer, 0, numRead).CopyTo(buffer);
return numRead;
}
- finally { ArrayPool<byte>.Shared.Return(buffer); }
+ finally { ArrayPool<byte>.Shared.Return(sharedBuffer); }
}
// Reads one byte from the stream by calling Read(byte[], int, int).
public abstract void Write(byte[] buffer, int offset, int count);
- public virtual void Write(ReadOnlySpan<byte> source)
+ public virtual void Write(ReadOnlySpan<byte> buffer)
{
- byte[] buffer = ArrayPool<byte>.Shared.Rent(source.Length);
+ byte[] sharedBuffer = ArrayPool<byte>.Shared.Rent(buffer.Length);
try
{
- source.CopyTo(buffer);
- Write(buffer, 0, source.Length);
+ buffer.CopyTo(sharedBuffer);
+ Write(sharedBuffer, 0, buffer.Length);
}
- finally { ArrayPool<byte>.Shared.Return(buffer); }
+ finally { ArrayPool<byte>.Shared.Return(sharedBuffer); }
}
// Writes one byte from the stream by calling Write(byte[], int, int).
return 0;
}
- public override int Read(Span<byte> destination)
+ public override int Read(Span<byte> buffer)
{
return 0;
}
return AsyncTaskMethodBuilder<int>.s_defaultResultTask;
}
- public override ValueTask<int> ReadAsync(Memory<byte> destination, CancellationToken cancellationToken = default(CancellationToken))
+ public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default(CancellationToken))
{
return new ValueTask<int>(0);
}
{
}
- public override void Write(ReadOnlySpan<byte> source)
+ public override void Write(ReadOnlySpan<byte> buffer)
{
}
Task.CompletedTask;
}
- public override ValueTask WriteAsync(ReadOnlyMemory<byte> source, CancellationToken cancellationToken = default(CancellationToken))
+ public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default(CancellationToken))
{
return cancellationToken.IsCancellationRequested ?
new ValueTask(Task.FromCanceled(cancellationToken)) :
return _stream.Read(bytes, offset, count);
}
- public override int Read(Span<byte> destination)
+ public override int Read(Span<byte> buffer)
{
lock (_stream)
- return _stream.Read(destination);
+ return _stream.Read(buffer);
}
public override int ReadByte()
_stream.Write(bytes, offset, count);
}
- public override void Write(ReadOnlySpan<byte> source)
+ public override void Write(ReadOnlySpan<byte> buffer)
{
lock (_stream)
- _stream.Write(source);
+ _stream.Write(buffer);
}
public override void WriteByte(byte b)