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
typeTypeA type that is located in the same namespace as the embedded resource.
namestringThe name of the embedded resource.
pathstringThe 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.
[SuppressMessage("Microsoft.Naming", "CA1711:IdentifiersShouldNotHaveIncorrectSuffix")]
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
typeTypeA type that is located in the same namespace as the embedded resource.
namestringThe 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
typeTypeA type that is located in the same namespace as the embedded resource.
namestringThe 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
typeTypeA type that is located in the same namespace as the embedded resource.
namestringThe 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
streamStreamThe stream to read from.
bufferArraySegment<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
streamStreamThe 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
streamStreamThe stream to read from.
encodingEncodingThe encoding of the string; leave
nullto 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
streamStreamThe stream to read from.
Returns
- MemoryStream
A new stream or the original
streamif 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
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.
[Pure]
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.
[Pure]
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.
[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
streamStreamThe stream.
bufferSizeintThe 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
streamStreamThe stream to write to.
bufferArraySegment<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)