NanoByte.Common  2.6.1
NanoByte.Common.Streams.StreamUtils Class Reference

Provides Stream-related helper methods. More...

Static Public Member Functions

static byte[] Read (this Stream stream, int count)
 Reads a fixed number of bytes from a stream starting from the current offset. More...
 
static ? byte[] TryRead (this Stream stream, int count)
 Reads a fixed number of bytes from a stream starting from the current offset. More...
 
static byte[] ToArray (this Stream stream)
 Reads the entire content of a stream to an array. Seeks to the beginning of the stream if Stream.CanSeek. More...
 
static string ReadToString (this Stream stream, Encoding? encoding=null)
 Reads the entire content of a stream as string data. Seeks to the beginning of the stream if Stream.CanSeek. More...
 
static void CopyToEx (this Stream source, Stream destination, int bufferSize=4096, CancellationToken cancellationToken=default, IProgress< long >? progress=null)
 Copies the content of one stream to another. Seeks to the beginning of the source stream if Stream.CanSeek. More...
 
static void CopyToFile (this Stream stream, [Localizable(false)] string path, int bufferSize=4096, CancellationToken cancellationToken=default, IProgress< long >? progress=null)
 Writes the entire content of a stream to a file. More...
 
static void Write (this Stream stream, byte[] data)
 Writes the entire contents of an array to a stream. More...
 
static bool ContentEquals (this Stream stream1, Stream stream2)
 Compares two streams for bit-wise equality. Seeks to the beginnings of the streams if Stream.CanSeek. More...
 
static MemoryStream ToStream (this string data, Encoding? encoding=null)
 Creates a new MemoryStream and fills it with string data. More...
 
static Stream GetEmbeddedStream (this Type type, [Localizable(false)] string name)
 Returns an embedded resource as a stream. More...
 
static byte[] GetEmbeddedBytes (this Type type, [Localizable(false)] string name)
 Returns an embedded resource as a byte array. More...
 
static string GetEmbeddedString (this Type type, [Localizable(false)] string name, Encoding? encoding=null)
 Returns an embedded resource as a string. More...
 
static void CopyEmbeddedToFile (this Type type, [Localizable(false)] string name, [Localizable(false)] string path)
 Copies an embedded resource to a file. More...
 

Detailed Description

Provides Stream-related helper methods.

Member Function Documentation

◆ ContentEquals()

static bool NanoByte.Common.Streams.StreamUtils.ContentEquals ( this Stream  stream1,
Stream  stream2 
)
inlinestatic

Compares two streams for bit-wise equality. Seeks to the beginnings of the streams if Stream.CanSeek.

Will try to Stream.Seek to the start of both streams.

◆ CopyEmbeddedToFile()

static void NanoByte.Common.Streams.StreamUtils.CopyEmbeddedToFile ( this Type  type,
[Localizable(false)] string  name,
[Localizable(false)] string  path 
)
inlinestatic

Copies an embedded resource to a file.

Parameters
typeA type that is located in the same namespace as the embedded resource.
nameThe name of the embedded resource.
pathThe path of the file to write.
Exceptions
ArgumentExceptionThe specified embedded resource does not exist.

◆ CopyToEx()

static void NanoByte.Common.Streams.StreamUtils.CopyToEx ( this Stream  source,
Stream  destination,
int  bufferSize = 4096,
CancellationToken  cancellationToken = default,
IProgress< long >?  progress = null 
)
inlinestatic

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

Parameters
sourceThe source stream to copy from.
destinationThe destination stream to copy to.
bufferSizeThe size of the buffer to use for copying in bytes.
cancellationTokenUsed to signal when the user wishes to cancel the copy process.
progressUsed to report back the number of bytes that have been copied so far.

Will try to Stream.Seek to the start of source .

◆ CopyToFile()

static void NanoByte.Common.Streams.StreamUtils.CopyToFile ( this Stream  stream,
[Localizable(false)] string  path,
int  bufferSize = 4096,
CancellationToken  cancellationToken = default,
IProgress< long >?  progress = null 
)
inlinestatic

Writes the entire content of a stream to a file.

Parameters
streamThe stream to read from.
pathThe path of the file to write.
bufferSizeThe size of the buffer to use for copying in bytes.
cancellationTokenUsed to signal when the user wishes to cancel the copy process.
progressUsed to report back the number of bytes that have been copied so far. Callbacks are rate limited to once every 250ms.

◆ GetEmbeddedBytes()

static byte [] NanoByte.Common.Streams.StreamUtils.GetEmbeddedBytes ( this Type  type,
[Localizable(false)] string  name 
)
inlinestatic

Returns an embedded resource as a byte array.

Parameters
typeA type that is located in the same namespace as the embedded resource.
nameThe name of the embedded resource.
Exceptions
ArgumentExceptionThe specified embedded resource does not exist.

◆ GetEmbeddedStream()

static Stream NanoByte.Common.Streams.StreamUtils.GetEmbeddedStream ( this Type  type,
[Localizable(false)] string  name 
)
inlinestatic

Returns an embedded resource as a stream.

Parameters
typeA type that is located in the same namespace as the embedded resource.
nameThe name of the embedded resource.
Exceptions
ArgumentExceptionThe specified embedded resource does not exist.

◆ GetEmbeddedString()

static string NanoByte.Common.Streams.StreamUtils.GetEmbeddedString ( this Type  type,
[Localizable(false)] string  name,
Encoding?  encoding = null 
)
inlinestatic

Returns an embedded resource as a string.

Parameters
typeA type that is located in the same namespace as the embedded resource.
nameThe name of the embedded resource.
encodingThe encoding of the string; leave null to default to UTF8Encoding.
Exceptions
ArgumentExceptionThe specified embedded resource does not exist.

◆ Read()

static byte [] NanoByte.Common.Streams.StreamUtils.Read ( this Stream  stream,
int  count 
)
static

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

Parameters
streamThe stream to read from.
countThe number of bytes to read.
Returns
The bytes read from the stream.
Exceptions
IOExceptionThe desired number of bytes could not be read from the stream.

◆ ReadToString()

static string NanoByte.Common.Streams.StreamUtils.ReadToString ( this Stream  stream,
Encoding?  encoding = null 
)
inlinestatic

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

Parameters
streamThe stream to read from.
encodingThe encoding of the string; leave null to default to UTF8Encoding.
Returns
A entire content of the stream.

◆ ToArray()

static byte [] NanoByte.Common.Streams.StreamUtils.ToArray ( this Stream  stream)
inlinestatic

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

Parameters
streamThe stream to read from.
Returns
A entire content of the stream.

◆ ToStream()

static MemoryStream NanoByte.Common.Streams.StreamUtils.ToStream ( this string  data,
Encoding?  encoding = null 
)
inlinestatic

Creates a new MemoryStream and fills it with string data.

Parameters
dataThe data to fill the stream with.
encodingThe encoding of the string; leave null to default to UTF8Encoding.
Returns
A filled stream with the position set to zero.

◆ TryRead()

static ? byte [] NanoByte.Common.Streams.StreamUtils.TryRead ( this Stream  stream,
int  count 
)
inlinestatic

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

Parameters
streamThe stream to read from.
countThe number of bytes to read.
Returns
The bytes read from the stream; null if the desired number of bytes could not be read from the stream .

◆ Write()

static void NanoByte.Common.Streams.StreamUtils.Write ( this Stream  stream,
byte[]  data 
)
inlinestatic

Writes the entire contents of an array to a stream.

Parameters
streamThe stream to write to.
dataThe array containing the bytes to write.

The documentation for this class was generated from the following file: