NanoByte.Common  2.9.0
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 int Read (this Stream stream, ArraySegment< byte > buffer)
 Reads a sequence of bytes from the stream. 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 ArraySegment< byte > ReadAll (this Stream stream)
 Reads the entire content of a stream. Seeks to the beginning of the stream if Stream.CanSeek. More...
 
static void Write (this Stream stream, params byte[] data)
 Writes the entire contents of an array to a stream. More...
 
static void Write (this Stream stream, ArraySegment< byte > buffer)
 Writes the entire contents of a buffer to a stream. More...
 
static byte[] AsArray (this Stream stream)
 The entire content of a stream as an array. Seeks to the beginning of the stream if Stream.CanSeek. Avoids copying the underlying array if possible. More...
 
static MemoryStream ToMemory (this Stream stream)
 Copies the entire content of a stream to a MemoryStream. Seeks to the beginning of the stream if Stream.CanSeek. More...
 
static void CopyToEx (this Stream source, Stream destination, int bufferSize=81920)
 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=81920)
 Writes the entire content of a stream to a file. 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 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

◆ AsArray()

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

The entire content of a stream as an array. Seeks to the beginning of the stream if Stream.CanSeek. Avoids copying the underlying array if possible.

◆ 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 = 81920 
)
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.

◆ CopyToFile()

static void NanoByte.Common.Streams.StreamUtils.CopyToFile ( this Stream  stream,
[Localizable(false)] string  path,
int  bufferSize = 81920 
)
inlinestatic

Writes the entire content of a stream to a file. Seeks to the beginning of the stream if Stream.CanSeek.

Parameters
streamThe stream to read from.
pathThe path of the file to write.
bufferSizeThe size of the buffer to use for copying in bytes.

◆ GetEmbeddedBytes()

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

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() [1/2]

static int NanoByte.Common.Streams.StreamUtils.Read ( this Stream  stream,
ArraySegment< byte >  buffer 
)
inlinestatic

Reads a sequence of bytes from the stream.

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

◆ Read() [2/2]

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.

◆ ReadAll()

static ArraySegment<byte> NanoByte.Common.Streams.StreamUtils.ReadAll ( this Stream  stream)
inlinestatic

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

Parameters
streamThe stream to read from.
Returns
The entire content of 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.

◆ ToMemory()

static MemoryStream NanoByte.Common.Streams.StreamUtils.ToMemory ( this Stream  stream)
inlinestatic

Copies the entire content of a stream to a MemoryStream. Seeks to the beginning of the stream if Stream.CanSeek.

Parameters
streamThe stream to read from.
Returns
A new stream or the original stream if it was already a MemoryStream.

◆ ToStream()

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

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() [1/2]

static void NanoByte.Common.Streams.StreamUtils.Write ( this Stream  stream,
ArraySegment< byte >  buffer 
)
inlinestatic

Writes the entire contents of a buffer to a stream.

Parameters
streamThe stream to write to.
bufferThe buffer containing the bytes to write.

◆ Write() [2/2]

static void NanoByte.Common.Streams.StreamUtils.Write ( this Stream  stream,
params 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: