Class StreamExtensions
Represents high-level read/write methods for the stream.
Namespace: DotNext.IO
Assembly: DotNext.dll
Syntax
public static class StreamExtensions : object
Remarks
This class provides alternative way to read and write typed data from/to the stream
without instantiation of
Methods
| Improve this Doc View SourceCopyTo(Stream, Stream, Byte[], CancellationToken)
Synchronously reads the bytes from the source stream and writes them to another stream, using a specified buffer.
Declaration
public static long CopyTo(this Stream source, Stream destination, byte[] buffer, CancellationToken token = null)
Parameters
| Type | Name | Description |
|---|---|---|
| Stream | source | The source stream to read from. |
| Stream | destination | The destination stream to write into. |
| Byte[] | buffer | The buffer used to hold copied content temporarily. |
| CancellationToken | token | The token that can be used to cancel this operation. |
Returns
| Type | Description |
|---|---|
| Int64 | The total number of copied bytes. |
CopyToAsync(Stream, Stream, Byte[], CancellationToken)
Asynchronously reads the bytes from the source stream and writes them to another stream, using a specified buffer.
Declaration
public static Task<long> CopyToAsync(this Stream source, Stream destination, byte[] buffer, CancellationToken token = null)
Parameters
| Type | Name | Description |
|---|---|---|
| Stream | source | The source stream to read from. |
| Stream | destination | The destination stream to write into. |
| Byte[] | buffer | The buffer used to hold copied content temporarily. |
| CancellationToken | token | The token that can be used to cancel this operation. |
Returns
| Type | Description |
|---|---|
| Task<Int64> | The total number of copied bytes. |
Read<T>(Stream, Byte[])
Deserializes the value type from the stream.
Declaration
public static T Read<T>(this Stream stream, byte[] buffer)
where T : struct
Parameters
| Type | Name | Description |
|---|---|---|
| Stream | stream | The stream to read from. |
| Byte[] | buffer | The buffer that is allocated by the caller. |
Returns
| Type | Description |
|---|---|
| T | The value deserialized from the stream. |
Type Parameters
| Name | Description |
|---|---|
| T | The value type to be deserialized. |
ReadAsync<T>(Stream, Byte[], CancellationToken)
Asynchronously deserializes the value type from the stream.
Declaration
public static Task<T> ReadAsync<T>(this Stream stream, byte[] buffer, CancellationToken token = null)
where T : struct
Parameters
| Type | Name | Description |
|---|---|---|
| Stream | stream | The stream to read from. |
| Byte[] | buffer | The buffer that is allocated by the caller. |
| CancellationToken | token | The token that can be used to cancel asynchronous operation. |
Returns
| Type | Description |
|---|---|
| Task<T> | The value deserialized from the stream. |
Type Parameters
| Name | Description |
|---|---|
| T | The value type to be deserialized. |
ReadBytes(Stream, Int32, Byte[])
Reads the number of bytes using the pre-allocated buffer.
Declaration
public static ReadOnlySpan<byte> ReadBytes(this Stream stream, int count, byte[] buffer)
Parameters
| Type | Name | Description |
|---|---|---|
| Stream | stream | The stream to read from. |
| Int32 | count | The number of bytes to read. |
| Byte[] | buffer | The buffer that is allocated by the caller. |
Returns
| Type | Description |
|---|---|
| ReadOnlySpan<Byte> | The span of bytes representing buffer segment. |
Remarks
You can use
ReadBytesAsync(Stream, Int32, Byte[], CancellationToken)
Reads asynchronously the number of bytes using the pre-allocated buffer.
Declaration
public static Task<ReadOnlyMemory<byte>> ReadBytesAsync(this Stream stream, int count, byte[] buffer, CancellationToken token = null)
Parameters
| Type | Name | Description |
|---|---|---|
| Stream | stream | The stream to read from. |
| Int32 | count | The number of bytes to read. |
| Byte[] | buffer | The buffer that is allocated by the caller. |
| CancellationToken | token | The token that can be used to cancel asynchronous operation. |
Returns
| Type | Description |
|---|---|
| Task<ReadOnlyMemory<Byte>> | The span of bytes representing buffer segment. |
Remarks
You can use
ReadString(Stream, Int32, DecodingContext, Byte[])
Reads the string using the specified encoding.
Declaration
public static string ReadString(this Stream stream, int length, DecodingContext context, byte[] buffer)
Parameters
| Type | Name | Description |
|---|---|---|
| Stream | stream | The stream to read from. |
| Int32 | length | The length of the string, in bytes. |
| DecodingContext | context | The text decoding context. |
| Byte[] | buffer | The buffer that is allocated by the caller. |
Returns
| Type | Description |
|---|---|
| String | The string decoded from the log entry content stream. |
ReadStringAsync(Stream, Int32, DecodingContext, Byte[], CancellationToken)
Reads the string asynchronously using the specified encoding.
Declaration
public static Task<string> ReadStringAsync(this Stream stream, int length, DecodingContext context, byte[] buffer, CancellationToken token = null)
Parameters
| Type | Name | Description |
|---|---|---|
| Stream | stream | The stream to read from. |
| Int32 | length | The length of the string. |
| DecodingContext | context | The text decoding context. |
| Byte[] | buffer | The buffer that is allocated by the caller. |
| CancellationToken | token | The token that can be used to cancel asynchronous operation. |
Returns
| Type | Description |
|---|---|
| Task<String> | The string decoded from the log entry content stream. |
Write<T>(Stream, ref T, Byte[])
Serializes value to the stream.
Declaration
public static void Write<T>(this Stream stream, ref T value, byte[] buffer)
where T : struct
Parameters
| Type | Name | Description |
|---|---|---|
| Stream | stream | The stream to write into. |
| T | value | The value to be written into the stream. |
| Byte[] | buffer | The buffer that is allocated by the caller. |
Type Parameters
| Name | Description |
|---|---|
| T | The value type to be serialized. |
WriteAsync<T>(Stream, ref T, Byte[], CancellationToken)
Asynchronously serializes value to the stream.
Declaration
public static Task WriteAsync<T>(this Stream stream, ref T value, byte[] buffer, CancellationToken token = null)
where T : struct
Parameters
| Type | Name | Description |
|---|---|---|
| Stream | stream | The stream to write into. |
| T | value | The value to be written into the stream. |
| Byte[] | buffer | The buffer that is allocated by the caller. |
| CancellationToken | token | The token that can be used to cancel asynchronous operation. |
Returns
| Type | Description |
|---|---|
| Task | The task representing asynchronous st |
Type Parameters
| Name | Description |
|---|---|
| T | The value type to be serialized. |
WriteString(Stream, String, EncodingContext, Byte[])
Writes the string into the stream.
Declaration
public static void WriteString(this Stream stream, string value, EncodingContext context, byte[] buffer)
Parameters
| Type | Name | Description |
|---|---|---|
| Stream | stream | The stream to write into. |
| String | value | The string to be encoded. |
| EncodingContext | context | The encoding. |
| Byte[] | buffer | The buffer allocated by the caller needed for characters encoding. |
Remarks
This method doesn't encode the length of the string.
WriteStringAsync(Stream, String, EncodingContext, Byte[], CancellationToken)
Writes the string into the stream asynchronously.
Declaration
public static Task WriteStringAsync(this Stream stream, string value, EncodingContext context, byte[] buffer, CancellationToken token = null)
Parameters
| Type | Name | Description |
|---|---|---|
| Stream | stream | The stream to write into. |
| String | value | The string to be encoded. |
| EncodingContext | context | The encoding context. |
| Byte[] | buffer | The buffer allocated by the caller needed for characters encoding. |
| CancellationToken | token | The token that can be used to cancel the operation. |
Returns
| Type | Description |
|---|---|
| Task | The task representing asynchronous state of the operation. |
Remarks
This method doesn't encode the length of the string.