Class StreamUtils
Provides System.IO.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 System.IO.Stream.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 System.IO.Stream.CanSeek.
public static void CopyToEx(this Stream source, Stream destination)
Parameters
source
StreamThe source stream to copy from.
destination
StreamThe 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 System.IO.Stream.CanSeek.
public static void CopyToFile(this Stream stream, string path)
Parameters
stream
StreamThe stream to read from.
path
stringThe 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
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
- 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
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
- 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
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
stream
StreamThe stream to read from.
count
intThe 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 System.IO.Stream.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 System.IO.Stream.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 System.IO.Stream.Seek(long, System.IO.SeekOrigin) if supported, System.IO.Stream.Read(byte[], int, int) otherwise.
public static void Skip(this Stream stream, int count)
Parameters
stream
StreamThe stream to read from.
count
intThe 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 System.IO.MemoryStream. Seeks to the beginning of the stream if System.IO.Stream.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 System.IO.MemoryStream.
ToStream(ArraySegment<byte>, bool)
Creates a new System.IO.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
- MemoryStream
ToStream(byte[], bool)
Creates a new System.IO.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
- MemoryStream
ToStream(string, Encoding?)
Creates a new System.IO.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
stream
StreamThe stream to read from.
count
intThe 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 System.IO.Stream.Length.
[Pure]
public static Stream WithLength(this Stream stream, long length)
Parameters
stream
StreamThe stream.
length
longThe value to return for System.IO.Stream.Length.
Returns
- Stream
WithSeekBuffer(Stream, int)
Adds seek buffering to a stream unless it already System.IO.Stream.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
- 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
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)
Parameters
stream
StreamThe stream to write to.
data
byte[]The array containing the bytes to write.