Class StreamUtils
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
TypeA type that is located in the same namespace as the embedded resource.
name
stringThe name of the embedded resource.
path
stringThe 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
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
GetEmbeddedBytes(Type, string)
Returns an embedded resource as a byte array.
[Pure]
public static byte[] GetEmbeddedBytes(this Type type, string name)
Parameters
type
TypeA type that is located in the same namespace as the embedded resource.
name
stringThe 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
TypeA type that is located in the same namespace as the embedded resource.
name
stringThe name of the embedded resource.
Returns
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
TypeA type that is located in the same namespace as the embedded resource.
name
stringThe name of the embedded resource.
encoding
EncodingThe encoding of the string; leave
null
to default to Utf8.
Returns
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
StreamThe 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
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
StreamThe 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
StreamThe stream to read from.
encoding
EncodingThe 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
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
StreamThe 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
boolControls whether the stream is writable (i.e., can modify the array).
Returns
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
boolControls whether the stream is writable (i.e., can modify the array).
Returns
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
stringThe data to fill the stream with.
encoding
EncodingThe 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
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
Returns
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
StreamThe stream.
bufferSize
intThe maximum number of bytes to buffer for seeking backwards. Set this to 0 to allow forward but no backward seeking.
Returns
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
StreamThe 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)