Class ArrayPoolMemoryStream
- Namespace
- CryptoHives.Foundation.Memory.Buffers
- Assembly
- CryptoHives.Foundation.Memory.dll
Class to create a MemoryStream which uses ArrayPool buffers.
public sealed class ArrayPoolMemoryStream : MemoryStream, IDisposable
- Inheritance
-
ArrayPoolMemoryStream
- Implements
- Inherited Members
Constructors
ArrayPoolMemoryStream()
Initializes a new instance of the ArrayPoolMemoryStream class. Creates a writeable stream that creates buffers as necessary using buffer defaults.
public ArrayPoolMemoryStream()
ArrayPoolMemoryStream(IEnumerable<ArraySegment<byte>>)
Initializes a new instance of the ArrayPoolMemoryStream class. Attaches the stream to read from a enumerable of buffers wrapped in ArraySegment<T>. Buffers are not returned to the ArrayPool when the stream is disposed.
public ArrayPoolMemoryStream(IEnumerable<ArraySegment<byte>> buffers)
Parameters
buffersIEnumerable<ArraySegment<byte>>
ArrayPoolMemoryStream(int)
Initializes a new instance of the ArrayPoolMemoryStream class. Creates a writeable stream that creates buffers as necessary using buffer list size defaults.
public ArrayPoolMemoryStream(int bufferSize)
Parameters
bufferSizeint
ArrayPoolMemoryStream(int, int)
Initializes a new instance of the ArrayPoolMemoryStream class. Creates a writeable stream that creates buffers as necessary.
public ArrayPoolMemoryStream(int bufferListSize, int bufferSize)
Parameters
ArrayPoolMemoryStream(int, int, int, int)
Initializes a new instance of the ArrayPoolMemoryStream class. Creates a writeable stream that rents ArrayPool buffers as necessary.
public ArrayPoolMemoryStream(int bufferListSize, int bufferSize, int start, int count)
Parameters
bufferListSizeintThe initial size of the buffer list
bufferSizeintThe size of the buffers
startintThe start of the ArraySegment in a buffer
countintThe count of bytes in the ArraySegment that is used in the buffer
Exceptions
Fields
DefaultBufferListSize
The default list size for the array segments.
public static readonly int DefaultBufferListSize
Field Value
DefaultBufferSize
The default buffer size of the allocated array pool buffers.
public static readonly int DefaultBufferSize
Field Value
Properties
CanRead
Gets a value indicating whether the current stream supports reading.
public override bool CanRead { get; }
Property Value
- bool
true if the stream is open.
CanSeek
Gets a value indicating whether the current stream supports seeking.
public override bool CanSeek { get; }
Property Value
- bool
true if the stream is open.
CanWrite
Gets a value indicating whether the current stream supports writing.
public override bool CanWrite { get; }
Property Value
- bool
true if the stream supports writing; otherwise, false.
Length
Gets the length of the stream in bytes.
public override long Length { get; }
Property Value
- long
The length of the stream in bytes.
Exceptions
- ObjectDisposedException
The stream is closed.
Position
Gets or sets the current position within the stream.
public override long Position { get; set; }
Property Value
- long
The current position within the stream.
Exceptions
- ArgumentOutOfRangeException
The position is set to a negative value or a value greater than MaxValue.
- ObjectDisposedException
The stream is closed.
Methods
Dispose(bool)
Releases the unmanaged resources used by the MemoryStream class and optionally releases the managed resources.
protected override void Dispose(bool disposing)
Parameters
disposingbooltrue to release both managed and unmanaged resources; false to release only unmanaged resources.
Flush()
Overrides the Flush() method so that no action is performed.
public override void Flush()
GetReadOnlySequence()
Returns a ReadOnlySequence<T> of the buffers stored in the stream. ReadOnlySequence is only valid as long as the stream is not disposed and no more data is written.
public ReadOnlySequence<byte> GetReadOnlySequence()
Returns
Read(byte[], int, int)
Reads a block of bytes from the current stream and writes the data to a buffer.
public override int Read(byte[] buffer, int offset, int count)
Parameters
bufferbyte[]When this method returns, contains the specified byte array with the values between
offsetand (offset+count- 1) replaced by the characters read from the current stream.offsetintThe zero-based byte offset in
bufferat which to begin storing data from the current stream.countintThe maximum number of bytes to read.
Returns
- int
The total number of bytes written into the buffer. This can be less than the number of bytes requested if that number of bytes are not currently available, or zero if the end of the stream is reached before any bytes are read.
Exceptions
- ArgumentNullException
bufferis null.- ArgumentOutOfRangeException
offsetorcountis negative.- ArgumentException
offsetsubtracted from the buffer length is less thancount.- ObjectDisposedException
The current stream instance is closed.
Read(Span<byte>)
public int Read(Span<byte> destination)
Parameters
Returns
ReadByte()
Reads a byte from the current stream.
public override int ReadByte()
Returns
Exceptions
- ObjectDisposedException
The current stream instance is closed.
ReadExactly(byte[], int, int)
Reads count bytes from the current stream and advances the position
within the stream.
public void ReadExactly(byte[] buffer, int offset, int count)
Parameters
bufferbyte[]An array of bytes. When this method returns, the buffer contains the specified byte array with the values between
offsetand (offset+count- 1) replaced by the bytes read from the current source.offsetintThe byte offset in
bufferat which to begin storing the data read from the current stream.countintThe number of bytes to be read from the current stream.
Exceptions
- EndOfStreamException
The end of the stream is reached before reading
countbytes.
ReadExactly(Span<byte>)
Reads bytes from the current stream and advances the position within the stream until
the buffer is filled.
public void ReadExactly(Span<byte> buffer)
Parameters
bufferSpan<byte>A region of memory. When this method returns, the contents of this region are replaced by the bytes read from the current stream.
Exceptions
- EndOfStreamException
The end of the stream is reached before filling the
buffer.
Seek(long, SeekOrigin)
Sets the position within the current stream to the specified value.
public override long Seek(long offset, SeekOrigin loc)
Parameters
offsetlongThe new position within the stream. This is relative to the
locparameter, and can be positive or negative.locSeekOriginA value of type SeekOrigin, which acts as the seek reference point.
Returns
- long
The new position within the stream, calculated by combining the initial reference point and the offset.
Exceptions
- IOException
Seeking is attempted before the beginning of the stream.
- ArgumentOutOfRangeException
offsetis greater than MaxValue.- ArgumentException
There is an invalid SeekOrigin. -or-
offsetcaused an arithmetic overflow.- ObjectDisposedException
The current stream instance is closed.
SetLength(long)
Sets the length of the current stream to the specified value.
public override void SetLength(long value)
Parameters
valuelongThe value at which to set the length.
Exceptions
- NotSupportedException
The current stream is not resizable and
valueis larger than the current capacity.-or- The current stream does not support writing.- ArgumentOutOfRangeException
valueis negative or is greater than the maximum length of the MemoryStream, where the maximum length is(MaxValue - origin), and origin is the index into the underlying buffer at which the stream starts.
ToArray()
Writes the stream contents to a byte array, regardless of the Position property.
public override byte[] ToArray()
Returns
- byte[]
A new byte array.
TryCopyTo(Span<byte>, out int)
Attempts to copy the stream content into the provided destination span. Returns true on success, false if the destination is too small.
public bool TryCopyTo(Span<byte> destination, out int bytesWritten)
Parameters
destinationSpan<byte>The destination span that receives the stream content.
bytesWrittenintWhen this method returns, contains the number of bytes written to
destination.
Returns
Write(byte[], int, int)
Writes a block of bytes to the current stream using data read from a buffer.
public override void Write(byte[] buffer, int offset, int count)
Parameters
bufferbyte[]The buffer to write data from.
offsetintThe zero-based byte offset in
bufferat which to begin copying bytes to the current stream.countintThe maximum number of bytes to write.
Exceptions
- ArgumentNullException
bufferis null.- NotSupportedException
The stream does not support writing. For additional information see CanWrite.-or- The current position is closer than
countbytes to the end of the stream, and the capacity cannot be modified.- ArgumentException
offsetsubtracted from the buffer length is less thancount.- ArgumentOutOfRangeException
offsetorcountare negative.- IOException
An I/O error occurs.
- ObjectDisposedException
The current stream instance is closed.
Write(ReadOnlySpan<byte>)
public void Write(ReadOnlySpan<byte> destination)
Parameters
destinationReadOnlySpan<byte>
WriteByte(byte)
Writes a byte to the current stream at the current position.
public override void WriteByte(byte value)
Parameters
valuebyteThe byte to write.
Exceptions
- NotSupportedException
The stream does not support writing. For additional information see CanWrite.-or- The current position is at the end of the stream, and the capacity cannot be modified.
- ObjectDisposedException
The current stream is closed.