Class RandomAccessStream
Represents a stream over the storage that supports random access.
Inherited Members
Namespace: DotNext.IO
Assembly: DotNext.IO.dll
Syntax
public abstract class RandomAccessStream : Stream, IAsyncDisposable, IDisposable, IFlushable, IValueTaskSource, IValueTaskSource<int>
Properties
| Edit this page View SourcePosition
When overridden in a derived class, gets or sets the position within the current stream.
Declaration
public override sealed long Position { get; set; }
Property Value
Type | Description |
---|---|
long | The current position within the stream. |
Overrides
Exceptions
Type | Condition |
---|---|
IOException | An I/O error occurs. |
NotSupportedException | The stream does not support seeking. |
ObjectDisposedException | Methods were called after the stream was closed. |
Methods
| Edit this page View SourceDispose(bool)
Releases the unmanaged resources used by the Stream and optionally releases the managed resources.
Declaration
protected override void Dispose(bool disposing)
Parameters
Type | Name | Description |
---|---|---|
bool | disposing | true to release both managed and unmanaged resources; false to release only unmanaged resources. |
Overrides
| Edit this page View SourceRead(byte[], int, int)
When overridden in a derived class, reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
Declaration
public override sealed int Read(byte[] buffer, int offset, int count)
Parameters
Type | Name | Description |
---|---|---|
byte[] | buffer | An array of bytes. When this method returns, the buffer contains the specified byte array with the values between |
int | offset | The zero-based byte offset in |
int | count | The maximum number of bytes to be read from the current stream. |
Returns
Type | Description |
---|---|
int | The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if |
Overrides
Exceptions
Type | Condition |
---|---|
ArgumentException | The sum of |
ArgumentNullException |
|
ArgumentOutOfRangeException |
|
IOException | An I/O error occurs. |
NotSupportedException | The stream does not support reading. |
ObjectDisposedException | Methods were called after the stream was closed. |
Read(Span<byte>)
When overridden in a derived class, reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
Declaration
public override sealed int Read(Span<byte> buffer)
Parameters
Type | Name | Description |
---|---|---|
Span<byte> | buffer | A region of memory. When this method returns, the contents of this region are replaced by the bytes read from the current source. |
Returns
Type | Description |
---|---|
int | The total number of bytes read into the buffer. This can be less than the size of the buffer if that many bytes are not currently available, or zero (0) if the buffer's length is zero or the end of the stream has been reached. |
Overrides
| Edit this page View SourceRead(Span<byte>, long)
Reads bytes to the specified buffer.
Declaration
protected abstract int Read(Span<byte> buffer, long offset)
Parameters
Type | Name | Description |
---|---|---|
Span<byte> | buffer | The buffer to be modified. |
long | offset | The offset within the underlying data storage. |
Returns
Type | Description |
---|---|
int | The number of bytes read. |
ReadAsync(byte[], int, int, CancellationToken)
Asynchronously reads a sequence of bytes from the current stream, advances the position within the stream by the number of bytes read, and monitors cancellation requests.
Declaration
public override sealed Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken token)
Parameters
Type | Name | Description |
---|---|---|
byte[] | buffer | The buffer to write the data into. |
int | offset | The byte offset in |
int | count | The maximum number of bytes to read. |
CancellationToken | token |
Returns
Type | Description |
---|---|
Task<int> | A task that represents the asynchronous read operation. The value of the |
Overrides
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentOutOfRangeException |
|
ArgumentException | The sum of |
NotSupportedException | The stream does not support reading. |
ObjectDisposedException | The stream has been disposed. |
InvalidOperationException | The stream is currently in use by a previous read operation. |
ReadAsync(Memory<byte>, long, CancellationToken)
Reads bytes to the specified buffer.
Declaration
protected abstract ValueTask<int> ReadAsync(Memory<byte> buffer, long offset, CancellationToken token)
Parameters
Type | Name | Description |
---|---|---|
Memory<byte> | buffer | The buffer to be modified. |
long | offset | The offset within the underlying data storage. |
CancellationToken | token | The token that can be used to cancel the operation. |
Returns
Type | Description |
---|---|
ValueTask<int> | The number of bytes read. |
ReadAsync(Memory<byte>, CancellationToken)
Asynchronously reads a sequence of bytes from the current stream, advances the position within the stream by the number of bytes read, and monitors cancellation requests.
Declaration
public override sealed ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken token = default)
Parameters
Type | Name | Description |
---|---|---|
Memory<byte> | buffer | The region of memory to write the data into. |
CancellationToken | token |
Returns
Type | Description |
---|---|
ValueTask<int> | A task that represents the asynchronous read operation. The value of its Result property contains the total number of bytes read into the buffer. The result value can be less than the length of the buffer if that many bytes are not currently available, or it can be 0 (zero) if the length of the buffer is 0 or if the end of the stream has been reached. |
Overrides
| Edit this page View SourceReadByte()
Reads a byte from the stream and advances the position within the stream by one byte, or returns -1 if at the end of the stream.
Declaration
public override sealed int ReadByte()
Returns
Type | Description |
---|---|
int | The unsigned byte cast to an int, or -1 if at the end of the stream. |
Overrides
Exceptions
Type | Condition |
---|---|
NotSupportedException | The stream does not support reading. |
ObjectDisposedException | Methods were called after the stream was closed. |
Seek(long, SeekOrigin)
When overridden in a derived class, sets the position within the current stream.
Declaration
public override long Seek(long offset, SeekOrigin origin)
Parameters
Type | Name | Description |
---|---|---|
long | offset | A byte offset relative to the |
SeekOrigin | origin | A value of type SeekOrigin indicating the reference point used to obtain the new position. |
Returns
Type | Description |
---|---|
long | The new position within the current stream. |
Overrides
Exceptions
Type | Condition |
---|---|
IOException | An I/O error occurs. |
NotSupportedException | The stream does not support seeking, such as if the stream is constructed from a pipe or console output. |
ObjectDisposedException | Methods were called after the stream was closed. |
Write(byte[], int, int)
When overridden in a derived class, writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
Declaration
public override sealed void Write(byte[] buffer, int offset, int count)
Parameters
Type | Name | Description |
---|---|---|
byte[] | buffer | An array of bytes. This method copies |
int | offset | The zero-based byte offset in |
int | count | The number of bytes to be written to the current stream. |
Overrides
Exceptions
Type | Condition |
---|---|
ArgumentException | The sum of |
ArgumentNullException |
|
ArgumentOutOfRangeException |
|
IOException | An I/O error occurred, such as the specified file cannot be found. |
NotSupportedException | The stream does not support writing. |
ObjectDisposedException | Write(byte[], int, int) was called after the stream was closed. |
Write(ReadOnlySpan<byte>)
When overridden in a derived class, writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
Declaration
public override sealed void Write(ReadOnlySpan<byte> buffer)
Parameters
Type | Name | Description |
---|---|---|
ReadOnlySpan<byte> | buffer | A region of memory. This method copies the contents of this region to the current stream. |
Overrides
| Edit this page View SourceWrite(ReadOnlySpan<byte>, long)
Writes the bytes at the specified offset.
Declaration
protected abstract void Write(ReadOnlySpan<byte> buffer, long offset)
Parameters
Type | Name | Description |
---|---|---|
ReadOnlySpan<byte> | buffer | The buffer to write. |
long | offset | The offset within the underlying data storage. |
WriteAsync(byte[], int, int, CancellationToken)
Asynchronously writes a sequence of bytes to the current stream, advances the current position within this stream by the number of bytes written, and monitors cancellation requests.
Declaration
public override sealed Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken token)
Parameters
Type | Name | Description |
---|---|---|
byte[] | buffer | The buffer to write data from. |
int | offset | The zero-based byte offset in |
int | count | The maximum number of bytes to write. |
CancellationToken | token |
Returns
Type | Description |
---|---|
Task | A task that represents the asynchronous write operation. |
Overrides
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentOutOfRangeException |
|
ArgumentException | The sum of |
NotSupportedException | The stream does not support writing. |
ObjectDisposedException | The stream has been disposed. |
InvalidOperationException | The stream is currently in use by a previous write operation. |
WriteAsync(ReadOnlyMemory<byte>, long, CancellationToken)
Writes the bytes at the specified offset.
Declaration
protected abstract ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, long offset, CancellationToken token)
Parameters
Type | Name | Description |
---|---|---|
ReadOnlyMemory<byte> | buffer | The buffer to write. |
long | offset | The offset within the underlying data storage. |
CancellationToken | token | The token that can be used to cancel the operation. |
Returns
Type | Description |
---|---|
ValueTask | The task representing asynchronous operation. |
WriteAsync(ReadOnlyMemory<byte>, CancellationToken)
Asynchronously writes a sequence of bytes to the current stream, advances the current position within this stream by the number of bytes written, and monitors cancellation requests.
Declaration
public override sealed ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken token = default)
Parameters
Type | Name | Description |
---|---|---|
ReadOnlyMemory<byte> | buffer | The region of memory to write data from. |
CancellationToken | token |
Returns
Type | Description |
---|---|
ValueTask | A task that represents the asynchronous write operation. |
Overrides
| Edit this page View SourceWriteByte(byte)
Writes a byte to the current position in the stream and advances the position within the stream by one byte.
Declaration
public override sealed void WriteByte(byte value)
Parameters
Type | Name | Description |
---|---|---|
byte | value | The byte to write to the stream. |
Overrides
Exceptions
Type | Condition |
---|---|
IOException | An I/O error occurs. |
NotSupportedException | The stream does not support writing, or the stream is already closed. |
ObjectDisposedException | Methods were called after the stream was closed. |