Table of Contents

Class StreamUtils

Namespace
NanoByte.Common.Streams
Assembly
NanoByte.Common.dll

Provides Stream-related helper methods.

public static class StreamUtils
Inheritance
StreamUtils

Methods

AsArray(Stream)

The entire content of a stream as an array. Seeks to the beginning of the stream if CanSeek. Avoids copying the underlying array if possible.

public static byte[] AsArray(this Stream stream)

Parameters

stream Stream

Returns

byte[]

CopyEmbeddedToFile(Type, string, string)

Copies an embedded resource to a file.

public static void CopyEmbeddedToFile(this Type type, string name, string path)

Parameters

type Type

A type that is located in the same namespace as the embedded resource.

name string

The name of the embedded resource.

path string

The path of the file to write.

Exceptions

ArgumentException

The specified embedded resource does not exist.

CopyToEx(Stream, Stream)

Copies the content of one stream to another. Seeks to the beginning of the source stream if CanSeek.

public static void CopyToEx(this Stream source, Stream destination)

Parameters

source Stream

The source stream to copy from.

destination Stream

The destination stream to copy to.

CopyToFile(Stream, string)

Writes the entire content of a stream to a file. Seeks to the beginning of the stream if CanSeek.

public static void CopyToFile(this Stream stream, string path)

Parameters

stream Stream

The stream to read from.

path string

The path of the file to write.

GetEmbeddedBytes(Type, string)

Returns an embedded resource as a byte array.

[Pure]
public static byte[] GetEmbeddedBytes(this Type type, string name)

Parameters

type Type

A type that is located in the same namespace as the embedded resource.

name string

The name of the embedded resource.

Returns

byte[]

Exceptions

ArgumentException

The specified embedded resource does not exist.

GetEmbeddedStream(Type, string)

Returns an embedded resource as a stream.

[Pure]
public static Stream GetEmbeddedStream(this Type type, string name)

Parameters

type Type

A type that is located in the same namespace as the embedded resource.

name string

The name of the embedded resource.

Returns

Stream

Exceptions

ArgumentException

The specified embedded resource does not exist.

GetEmbeddedString(Type, string, Encoding?)

Returns an embedded resource as a string.

[Pure]
public static string GetEmbeddedString(this Type type, string name, Encoding? encoding = null)

Parameters

type Type

A type that is located in the same namespace as the embedded resource.

name string

The name of the embedded resource.

encoding Encoding

The encoding of the string; leave null to default to Utf8.

Returns

string

Exceptions

ArgumentException

The specified embedded resource does not exist.

Read(Stream, ArraySegment<byte>)

Reads a sequence of bytes from the stream.

public static int Read(this Stream stream, ArraySegment<byte> buffer)

Parameters

stream Stream

The stream to read from.

buffer ArraySegment<byte>

The buffer to read the bytes into.

Returns

int

The bytes read from the stream.

Exceptions

IOException

The desired number of bytes could not be read from the stream.

Read(Stream, int)

Reads a fixed number of bytes from a stream starting from the current offset.

public static byte[] Read(this Stream stream, int count)

Parameters

stream Stream

The stream to read from.

count int

The number of bytes to read.

Returns

byte[]

The bytes read from the stream.

Exceptions

IOException

The desired number of bytes could not be read from the stream.

ReadAll(Stream)

Reads the entire content of a stream. Seeks to the beginning of the stream if CanSeek.

public static ArraySegment<byte> ReadAll(this Stream stream)

Parameters

stream Stream

The stream to read from.

Returns

ArraySegment<byte>

The entire content of the stream.

ReadToString(Stream, Encoding?)

Reads the entire content of a stream as string data. Seeks to the beginning of the stream if CanSeek.

public static string ReadToString(this Stream stream, Encoding? encoding = null)

Parameters

stream Stream

The stream to read from.

encoding Encoding

The encoding of the string; leave null to default to Utf8.

Returns

string

A entire content of the stream.

Skip(Stream, int)

Skips a number of bytes in the stream. Uses Seek(long, SeekOrigin) if supported, Read(byte[], int, int) otherwise.

public static void Skip(this Stream stream, int count)

Parameters

stream Stream

The stream to read from.

count int

The number of bytes to skip.

Exceptions

IOException

The desired number of bytes could not be skipped in the stream.

ToMemory(Stream)

Copies the entire content of a stream to a MemoryStream. Seeks to the beginning of the stream if CanSeek.

public static MemoryStream ToMemory(this Stream stream)

Parameters

stream Stream

The stream to read from.

Returns

MemoryStream

A new stream or the original stream if it was already a MemoryStream.

ToStream(ArraySegment<byte>, bool)

Creates a new MemoryStream using the existing array segment as the underlying storage.

[Pure]
public static MemoryStream ToStream(this ArraySegment<byte> segment, bool writable = false)

Parameters

segment ArraySegment<byte>

The array segment to create the stream from.

writable bool

Controls whether the stream is writable (i.e., can modify the array).

Returns

MemoryStream

ToStream(byte[], bool)

Creates a new MemoryStream using the existing array as the underlying storage.

[Pure]
public static MemoryStream ToStream(this byte[] array, bool writable = false)

Parameters

array byte[]

The array to create the stream from.

writable bool

Controls whether the stream is writable (i.e., can modify the array).

Returns

MemoryStream

ToStream(string, Encoding?)

Creates a new MemoryStream and fills it with string data.

[Pure]
public static MemoryStream ToStream(this string data, Encoding? encoding = null)

Parameters

data string

The data to fill the stream with.

encoding Encoding

The encoding of the string; leave null to default to Utf8.

Returns

MemoryStream

A filled stream with the position set to zero.

TryRead(Stream, int)

Reads a fixed number of bytes from a stream starting from the current offset.

public static byte[]? TryRead(this Stream stream, int count)

Parameters

stream Stream

The stream to read from.

count int

The number of bytes to read.

Returns

byte[]

The bytes read from the stream; null if the desired number of bytes could not be read from the stream.

WithLength(Stream, long)

Overrides the value returned by Length.

[Pure]
public static Stream WithLength(this Stream stream, long length)

Parameters

stream Stream

The stream.

length long

The value to return for Length.

Returns

Stream

WithSeekBuffer(Stream, int)

Adds seek buffering to a stream unless it already CanSeek.

[Pure]
public static Stream WithSeekBuffer(this Stream stream, int bufferSize = 262144)

Parameters

stream Stream

The stream.

bufferSize int

The maximum number of bytes to buffer for seeking backwards. Set this to 0 to allow forward but no backward seeking.

Returns

Stream

Write(Stream, ArraySegment<byte>)

Writes the entire contents of a buffer to a stream.

public static void Write(this Stream stream, ArraySegment<byte> buffer)

Parameters

stream Stream

The stream to write to.

buffer ArraySegment<byte>

The buffer containing the bytes to write.

Write(Stream, params byte[])

Writes the entire contents of an array to a stream.

public static void Write(this Stream stream, params byte[] data)

Parameters

stream Stream

The stream to write to.

data byte[]

The array containing the bytes to write.