Skip to main content

StreamExtensions

Assembly: ServiceStack.Text.dll​
View Source​
Declaration
public static class StreamExtensions

Fields​

DefaultBufferSize​

@jonskeet: Collection of utility methods which operate on streams. r285, February 26th 2009: http://www.yoda.arachsys.com/csharp/miscutil/

View Source​
Declaration
public const int DefaultBufferSize = 8192

AsyncBufferSize​

View Source​
Declaration
public static int AsyncBufferSize

Methods​

WriteTo(Stream, Stream)​

View Source​
Declaration
public static long WriteTo(this Stream inStream, Stream outStream)
Returns​

System.Int64

Parameters​
TypeName
System.IO.StreaminStream
System.IO.StreamoutStream

ReadLines(Stream)​

View Source​
Declaration
public static IEnumerable<string> ReadLines(this Stream stream)
Returns​

System.Collections.Generic.IEnumerable<System.String>

Parameters​
TypeName
System.IO.Streamstream

ReadFully(Stream)​

Reads the given stream up to the end, returning the data as a byte array.

View Source​
Declaration
public static byte[] ReadFully(this Stream input)
Returns​

System.Byte[]

Parameters​
TypeName
System.IO.Streaminput

ReadFully(Stream, Int32)​

Reads the given stream up to the end, returning the data as a byte array, using the given buffer size.

View Source​
Declaration
public static byte[] ReadFully(this Stream input, int bufferSize)
Returns​

System.Byte[]

Parameters​
TypeName
System.IO.Streaminput
System.Int32bufferSize

ReadFully(Stream, Byte[])​

Reads the given stream up to the end, returning the data as a byte array, using the given buffer for transferring data. Note that the current contents of the buffer is ignored, so the buffer needn't be cleared beforehand.

View Source​
Declaration
public static byte[] ReadFully(this Stream input, byte[] buffer)
Returns​

System.Byte[]

Parameters​
TypeName
System.IO.Streaminput
System.Byte[]buffer

ReadFullyAsync(Stream, CancellationToken)​

Reads the given stream up to the end, returning the data as a byte array.

View Source​
Declaration
public static Task<byte[]> ReadFullyAsync(this Stream input, CancellationToken token = default(CancellationToken))
Returns​

System.Threading.Tasks.Task<System.Byte[]>

Parameters​
TypeName
System.IO.Streaminput
System.Threading.CancellationTokentoken

ReadFullyAsync(Stream, Int32, CancellationToken)​

Reads the given stream up to the end, returning the data as a byte array, using the given buffer size.

View Source​
Declaration
public static async Task<byte[]> ReadFullyAsync(this Stream input, int bufferSize, CancellationToken token = default(CancellationToken))
Returns​

System.Threading.Tasks.Task<System.Byte[]>

Parameters​
TypeName
System.IO.Streaminput
System.Int32bufferSize
System.Threading.CancellationTokentoken

ReadFullyAsync(Stream, Byte[], CancellationToken)​

Reads the given stream up to the end, returning the data as a byte array, using the given buffer for transferring data. Note that the current contents of the buffer is ignored, so the buffer needn't be cleared beforehand.

View Source​
Declaration
public static async Task<byte[]> ReadFullyAsync(this Stream input, byte[] buffer, CancellationToken token = default(CancellationToken))
Returns​

System.Threading.Tasks.Task<System.Byte[]>

Parameters​
TypeName
System.IO.Streaminput
System.Byte[]buffer
System.Threading.CancellationTokentoken

ReadFullyAsMemory(Stream)​

Reads the given stream up to the end, returning the MemoryStream Buffer as ReadOnlyMemory<byte>.

View Source​
Declaration
public static ReadOnlyMemory<byte> ReadFullyAsMemory(this Stream input)
Returns​

ReadOnlyMemory<System.Byte>

Parameters​
TypeName
System.IO.Streaminput

ReadFullyAsMemory(Stream, Int32)​

Reads the given stream up to the end, returning the MemoryStream Buffer as ReadOnlyMemory<byte>.

View Source​
Declaration
public static ReadOnlyMemory<byte> ReadFullyAsMemory(this Stream input, int bufferSize)
Returns​

ReadOnlyMemory<System.Byte>

Parameters​
TypeName
System.IO.Streaminput
System.Int32bufferSize

ReadFullyAsMemory(Stream, Byte[])​

View Source​
Declaration
public static ReadOnlyMemory<byte> ReadFullyAsMemory(this Stream input, byte[] buffer)
Returns​

ReadOnlyMemory<System.Byte>

Parameters​
TypeName
System.IO.Streaminput
System.Byte[]buffer

ReadFullyAsMemoryAsync(Stream, CancellationToken)​

Reads the given stream up to the end, returning the MemoryStream Buffer as ReadOnlyMemory<byte>.

View Source​
Declaration
public static Task<ReadOnlyMemory<byte>> ReadFullyAsMemoryAsync(this Stream input, CancellationToken token = default(CancellationToken))
Returns​

System.Threading.Tasks.Task<ReadOnlyMemory<System.Byte>>

Parameters​
TypeName
System.IO.Streaminput
System.Threading.CancellationTokentoken

ReadFullyAsMemoryAsync(Stream, Int32, CancellationToken)​

Reads the given stream up to the end, returning the MemoryStream Buffer as ReadOnlyMemory<byte>.

View Source​
Declaration
public static async Task<ReadOnlyMemory<byte>> ReadFullyAsMemoryAsync(this Stream input, int bufferSize, CancellationToken token = default(CancellationToken))
Returns​

System.Threading.Tasks.Task<ReadOnlyMemory<System.Byte>>

Parameters​
TypeName
System.IO.Streaminput
System.Int32bufferSize
System.Threading.CancellationTokentoken

ReadFullyAsMemoryAsync(Stream, Byte[], CancellationToken)​

View Source​
Declaration
public static async Task<ReadOnlyMemory<byte>> ReadFullyAsMemoryAsync(this Stream input, byte[] buffer, CancellationToken token = default(CancellationToken))
Returns​

System.Threading.Tasks.Task<ReadOnlyMemory<System.Byte>>

Parameters​
TypeName
System.IO.Streaminput
System.Byte[]buffer
System.Threading.CancellationTokentoken

CopyTo(Stream, Stream)​

Copies all the data from one stream into another.

View Source​
Declaration
public static long CopyTo(this Stream input, Stream output)
Returns​

System.Int64

Parameters​
TypeName
System.IO.Streaminput
System.IO.Streamoutput

CopyTo(Stream, Stream, Int32)​

Copies all the data from one stream into another, using a buffer of the given size.

View Source​
Declaration
public static long CopyTo(this Stream input, Stream output, int bufferSize)
Returns​

System.Int64

Parameters​
TypeName
System.IO.Streaminput
System.IO.Streamoutput
System.Int32bufferSize

CopyTo(Stream, Stream, Byte[])​

Copies all the data from one stream into another, using the given buffer for transferring data. Note that the current contents of the buffer is ignored, so the buffer needn't be cleared beforehand.

View Source​
Declaration
public static long CopyTo(this Stream input, Stream output, byte[] buffer)
Returns​

System.Int64

Parameters​
TypeName
System.IO.Streaminput
System.IO.Streamoutput
System.Byte[]buffer

CopyToAsync(Stream, Stream, Byte[], CancellationToken)​

Copies all the data from one stream into another, using the given buffer for transferring data. Note that the current contents of the buffer is ignored, so the buffer needn't be cleared beforehand.

View Source​
Declaration
public static async Task<long> CopyToAsync(this Stream input, Stream output, byte[] buffer, CancellationToken token = default(CancellationToken))
Returns​

System.Threading.Tasks.Task<System.Int64>

Parameters​
TypeName
System.IO.Streaminput
System.IO.Streamoutput
System.Byte[]buffer
System.Threading.CancellationTokentoken

ReadExactly(Stream, Int32)​

Reads exactly the given number of bytes from the specified stream. If the end of the stream is reached before the specified amount of data is read, an exception is thrown.

View Source​
Declaration
public static byte[] ReadExactly(this Stream input, int bytesToRead)
Returns​

System.Byte[]

Parameters​
TypeName
System.IO.Streaminput
System.Int32bytesToRead

ReadExactly(Stream, Byte[])​

Reads into a buffer, filling it completely.

View Source​
Declaration
public static byte[] ReadExactly(this Stream input, byte[] buffer)
Returns​

System.Byte[]

Parameters​
TypeName
System.IO.Streaminput
System.Byte[]buffer

ReadExactly(Stream, Byte[], Int32)​

Reads exactly the given number of bytes from the specified stream, into the given buffer, starting at position 0 of the array.

View Source​
Declaration
public static byte[] ReadExactly(this Stream input, byte[] buffer, int bytesToRead)
Returns​

System.Byte[]

Parameters​
TypeName
System.IO.Streaminput
System.Byte[]buffer
System.Int32bytesToRead

ReadExactly(Stream, Byte[], Int32, Int32)​

Reads exactly the given number of bytes from the specified stream, into the given buffer, starting at position 0 of the array.

View Source​
Declaration
public static byte[] ReadExactly(this Stream input, byte[] buffer, int startIndex, int bytesToRead)
Returns​

System.Byte[]

Parameters​
TypeName
System.IO.Streaminput
System.Byte[]buffer
System.Int32startIndex
System.Int32bytesToRead

CollapseWhitespace(String)​

View Source​
Declaration
public static string CollapseWhitespace(this string str)
Returns​

System.String

Parameters​
TypeName
System.Stringstr

Combine(Byte[], Byte[][])​

View Source​
Declaration
public static byte[] Combine(this byte[] bytes, params byte[][] withBytes)
Returns​

System.Byte[]

Parameters​
TypeName
System.Byte[]bytes
System.Byte[][]withBytes

WriteAsync(Stream, ReadOnlyMemory<Byte>, CancellationToken)​

View Source​
Declaration
public static Task WriteAsync(this Stream stream, ReadOnlyMemory<byte> value, CancellationToken token = default(CancellationToken))
Returns​

System.Threading.Tasks.Task

Parameters​
TypeName
System.IO.Streamstream
ReadOnlyMemory<System.Byte>value
System.Threading.CancellationTokentoken

WriteAsync(Stream, Byte[], CancellationToken)​

View Source​
Declaration
public static Task WriteAsync(this Stream stream, byte[] bytes, CancellationToken token = default(CancellationToken))
Returns​

System.Threading.Tasks.Task

Parameters​
TypeName
System.IO.Streamstream
System.Byte[]bytes
System.Threading.CancellationTokentoken

CopyToAsync(Stream, Stream, CancellationToken)​

View Source​
Declaration
public static Task CopyToAsync(this Stream input, Stream output, CancellationToken token = default(CancellationToken))
Returns​

System.Threading.Tasks.Task

Parameters​
TypeName
System.IO.Streaminput
System.IO.Streamoutput
System.Threading.CancellationTokentoken

WriteAsync(Stream, String, CancellationToken)​

View Source​
Declaration
public static Task WriteAsync(this Stream stream, string text, CancellationToken token = default(CancellationToken))
Returns​

System.Threading.Tasks.Task

Parameters​
TypeName
System.IO.Streamstream
System.Stringtext
System.Threading.CancellationTokentoken

ToMd5Bytes(Stream)​

View Source​
Declaration
public static byte[] ToMd5Bytes(this Stream stream)
Returns​

System.Byte[]

Parameters​
TypeName
System.IO.Streamstream

ToMd5Hash(Stream)​

View Source​
Declaration
public static string ToMd5Hash(this Stream stream)
Returns​

System.String

Parameters​
TypeName
System.IO.Streamstream

ToMd5Hash(Byte[])​

View Source​
Declaration
public static string ToMd5Hash(this byte[] bytes)
Returns​

System.String

Parameters​
TypeName
System.Byte[]bytes

InMemoryStream(Byte[])​

Returns bytes in publiclyVisible MemoryStream

View Source​
Declaration
public static MemoryStream InMemoryStream(this byte[] bytes)
Returns​

System.IO.MemoryStream

Parameters​
TypeName
System.Byte[]bytes

ReadToEnd(MemoryStream)​

View Source​
Declaration
public static string ReadToEnd(this MemoryStream ms)
Returns​

System.String

Parameters​
TypeName
System.IO.MemoryStreamms

ReadToEnd(MemoryStream, Encoding)​

View Source​
Declaration
public static string ReadToEnd(this MemoryStream ms, Encoding encoding)
Returns​

System.String

Parameters​
TypeName
System.IO.MemoryStreamms
System.Text.Encodingencoding

GetBufferAsMemory(MemoryStream)​

View Source​
Declaration
public static ReadOnlyMemory<byte> GetBufferAsMemory(this MemoryStream ms)
Returns​

ReadOnlyMemory<System.Byte>

Parameters​
TypeName
System.IO.MemoryStreamms

GetBufferAsSpan(MemoryStream)​

View Source​
Declaration
public static ReadOnlySpan<byte> GetBufferAsSpan(this MemoryStream ms)
Returns​

ReadOnlySpan<System.Byte>

Parameters​
TypeName
System.IO.MemoryStreamms

GetBufferAsBytes(MemoryStream)​

View Source​
Declaration
public static byte[] GetBufferAsBytes(this MemoryStream ms)
Returns​

System.Byte[]

Parameters​
TypeName
System.IO.MemoryStreamms

ReadToEndAsync(MemoryStream)​

View Source​
Declaration
public static Task<string> ReadToEndAsync(this MemoryStream ms)
Returns​

System.Threading.Tasks.Task<System.String>

Parameters​
TypeName
System.IO.MemoryStreamms

ReadToEndAsync(MemoryStream, Encoding)​

View Source​
Declaration
public static Task<string> ReadToEndAsync(this MemoryStream ms, Encoding encoding)
Returns​

System.Threading.Tasks.Task<System.String>

Parameters​
TypeName
System.IO.MemoryStreamms
System.Text.Encodingencoding

ReadToEnd(Stream)​

View Source​
Declaration
public static string ReadToEnd(this Stream stream)
Returns​

System.String

Parameters​
TypeName
System.IO.Streamstream

ReadToEnd(Stream, Encoding)​

View Source​
Declaration
public static string ReadToEnd(this Stream stream, Encoding encoding)
Returns​

System.String

Parameters​
TypeName
System.IO.Streamstream
System.Text.Encodingencoding

ReadToEndAsync(Stream)​

View Source​
Declaration
public static Task<string> ReadToEndAsync(this Stream stream)
Returns​

System.Threading.Tasks.Task<System.String>

Parameters​
TypeName
System.IO.Streamstream

ReadToEndAsync(Stream, Encoding)​

View Source​
Declaration
public static Task<string> ReadToEndAsync(this Stream stream, Encoding encoding)
Returns​

System.Threading.Tasks.Task<System.String>

Parameters​
TypeName
System.IO.Streamstream
System.Text.Encodingencoding

WriteToAsync(MemoryStream, Stream, CancellationToken)​

View Source​
Declaration
public static Task WriteToAsync(this MemoryStream stream, Stream output, CancellationToken token = default(CancellationToken))
Returns​

System.Threading.Tasks.Task

Parameters​
TypeName
System.IO.MemoryStreamstream
System.IO.Streamoutput
System.Threading.CancellationTokentoken

WriteToAsync(MemoryStream, Stream, Encoding, CancellationToken)​

View Source​
Declaration
public static async Task WriteToAsync(this MemoryStream stream, Stream output, Encoding encoding, CancellationToken token)
Returns​

System.Threading.Tasks.Task

Parameters​
TypeName
System.IO.MemoryStreamstream
System.IO.Streamoutput
System.Text.Encodingencoding
System.Threading.CancellationTokentoken

WriteToAsync(Stream, Stream, CancellationToken)​

View Source​
Declaration
public static Task WriteToAsync(this Stream stream, Stream output, CancellationToken token = default(CancellationToken))
Returns​

System.Threading.Tasks.Task

Parameters​
TypeName
System.IO.Streamstream
System.IO.Streamoutput
System.Threading.CancellationTokentoken

WriteToAsync(Stream, Stream, Encoding, CancellationToken)​

View Source​
Declaration
public static Task WriteToAsync(this Stream stream, Stream output, Encoding encoding, CancellationToken token)
Returns​

System.Threading.Tasks.Task

Parameters​
TypeName
System.IO.Streamstream
System.IO.Streamoutput
System.Text.Encodingencoding
System.Threading.CancellationTokentoken

CopyToNewMemoryStream(Stream)​

View Source​
Declaration
public static MemoryStream CopyToNewMemoryStream(this Stream stream)
Returns​

System.IO.MemoryStream

Parameters​
TypeName
System.IO.Streamstream

CopyToNewMemoryStreamAsync(Stream)​

View Source​
Declaration
public static async Task<MemoryStream> CopyToNewMemoryStreamAsync(this Stream stream)
Returns​

System.Threading.Tasks.Task<System.IO.MemoryStream>

Parameters​
TypeName
System.IO.Streamstream

WritePartialTo(Stream, Stream, Int64, Int64)​

Writes partial range as specified by start-end, from fromStream to toStream.

View Source​
Declaration
[Obsolete("Use WritePartialToAsync")]
public static void WritePartialTo(this Stream fromStream, Stream toStream, long start, long end)
Parameters​
TypeName
System.IO.StreamfromStream
System.IO.StreamtoStream
System.Int64start
System.Int64end

WritePartialToAsync(Stream, Stream, Int64, Int64, CancellationToken)​

Writes partial range as specified by start-end, from fromStream to toStream.

View Source​
Declaration
public static async Task WritePartialToAsync(this Stream fromStream, Stream toStream, long start, long end, CancellationToken token = default(CancellationToken))
Returns​

System.Threading.Tasks.Task

Parameters​
TypeName
System.IO.StreamfromStream
System.IO.StreamtoStream
System.Int64start
System.Int64end
System.Threading.CancellationTokentoken