Class StreamUtils
Provides Stream-related helper methods.
public static class StreamUtils
- Inheritance
-
objectStreamUtils
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
streamStream
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
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 stream stream if CanSeek.
public static void CopyToEx(this Stream stream, 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.
public static byte[] GetEmbeddedBytes(this Type type, string name)
Parameters
Returns
- byte[]
Exceptions
- ArgumentException
The specified embedded resource does not exist.
GetEmbeddedStream(Type, string)
Returns an embedded resource as a stream.
public static Stream GetEmbeddedStream(this Type type, string name)
Parameters
Returns
Exceptions
- ArgumentException
The specified embedded resource does not exist.
GetEmbeddedString(Type, string, Encoding?)
Returns an embedded resource as a string.
public static string GetEmbeddedString(this Type type, string name, Encoding? encoding = null)
Parameters
typeTypenamestringThe name of the embedded resource.
encodingEncodingThe encoding of the string; leave
nullto 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
streamStreambufferArraySegment<byte>The buffer to read the bytes into.
Returns
- int
The number of bytes read from the stream. May be less than the size of the
buffer.
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
streamStream
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
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
streamStream
Returns
- MemoryStream
A new stream. Shares the underlying array with
streamif possible.
ToStream(ArraySegment<byte>, bool)
Creates a new MemoryStream using the existing array segment as the underlying storage.
public static MemoryStream ToStream(this ArraySegment<byte> segment, bool writable = false)
Parameters
segmentArraySegment<byte>The array segment to create the stream from.
writableboolControls 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.
public static MemoryStream ToStream(this byte[] array, bool writable = false)
Parameters
arraybyte[]The array to create the stream from.
writableboolControls 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.
public static MemoryStream ToStream(this string data, Encoding? encoding = null)
Parameters
datastringThe data to fill the stream with.
encodingEncodingThe encoding of the string; leave
nullto 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;
nullif the desired number of bytes could not be read from the stream.
WithLength(Stream, long)
Overrides the value returned by Length.
public static Stream WithLength(this Stream stream, long length)
Parameters
Returns
WithSeekBuffer(Stream, int)
Adds seek buffering to a stream unless it already CanSeek.
public static Stream WithSeekBuffer(this Stream stream, int bufferSize = 262144)
Parameters
streamStreambufferSizeintThe 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
streamStreambufferArraySegment<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)