StreamExtensions
Assembly: ServiceStack.Text.dll
View Source
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
public const int DefaultBufferSize = 8192
AsyncBufferSize
View Source
public static int AsyncBufferSize
Methods
WriteTo(Stream, Stream)
View Source
public static long WriteTo(this Stream inStream, Stream outStream)
Returns
System.Int64
Parameters
Type | Name |
---|---|
System.IO.Stream | inStream |
System.IO.Stream | outStream |
ReadLines(Stream)
View Source
public static IEnumerable<string> ReadLines(this Stream stream)
Returns
System.Collections.Generic.IEnumerable<System.String>
Parameters
Type | Name |
---|---|
System.IO.Stream | stream |
ReadFully(Stream)
Reads the given stream up to the end, returning the data as a byte array.
View Source
public static byte[] ReadFully(this Stream input)
Returns
System.Byte[]
Parameters
Type | Name |
---|---|
System.IO.Stream | input |
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
public static byte[] ReadFully(this Stream input, int bufferSize)
Returns
System.Byte[]
Parameters
Type | Name |
---|---|
System.IO.Stream | input |
System.Int32 | bufferSize |
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
public static byte[] ReadFully(this Stream input, byte[] buffer)
Returns
System.Byte[]
Parameters
Type | Name |
---|---|
System.IO.Stream | input |
System.Byte[] | buffer |
ReadFullyAsync(Stream, CancellationToken)
Reads the given stream up to the end, returning the data as a byte array.
View Source
public static Task<byte[]> ReadFullyAsync(this Stream input, CancellationToken token = default(CancellationToken))
Returns
System.Threading.Tasks.Task<System.Byte[]>
Parameters
Type | Name |
---|---|
System.IO.Stream | input |
System.Threading.CancellationToken | token |
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
public static async Task<byte[]> ReadFullyAsync(this Stream input, int bufferSize, CancellationToken token = default(CancellationToken))
Returns
System.Threading.Tasks.Task<System.Byte[]>
Parameters
Type | Name |
---|---|
System.IO.Stream | input |
System.Int32 | bufferSize |
System.Threading.CancellationToken | token |
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
public static async Task<byte[]> ReadFullyAsync(this Stream input, byte[] buffer, CancellationToken token = default(CancellationToken))
Returns
System.Threading.Tasks.Task<System.Byte[]>
Parameters
Type | Name |
---|---|
System.IO.Stream | input |
System.Byte[] | buffer |
System.Threading.CancellationToken | token |
ReadFullyAsMemory(Stream)
Reads the given stream up to the end, returning the MemoryStream Buffer as ReadOnlyMemory<byte>.
View Source
public static ReadOnlyMemory<byte> ReadFullyAsMemory(this Stream input)
Returns
ReadOnlyMemory<System.Byte>
Parameters
Type | Name |
---|---|
System.IO.Stream | input |
ReadFullyAsMemory(Stream, Int32)
Reads the given stream up to the end, returning the MemoryStream Buffer as ReadOnlyMemory<byte>.
View Source
public static ReadOnlyMemory<byte> ReadFullyAsMemory(this Stream input, int bufferSize)
Returns
ReadOnlyMemory<System.Byte>
Parameters
Type | Name |
---|---|
System.IO.Stream | input |
System.Int32 | bufferSize |
ReadFullyAsMemory(Stream, Byte[])
View Source
public static ReadOnlyMemory<byte> ReadFullyAsMemory(this Stream input, byte[] buffer)
Returns
ReadOnlyMemory<System.Byte>
Parameters
Type | Name |
---|---|
System.IO.Stream | input |
System.Byte[] | buffer |
ReadFullyAsMemoryAsync(Stream, CancellationToken)
Reads the given stream up to the end, returning the MemoryStream Buffer as ReadOnlyMemory<byte>.
View Source
public static Task<ReadOnlyMemory<byte>> ReadFullyAsMemoryAsync(this Stream input, CancellationToken token = default(CancellationToken))
Returns
System.Threading.Tasks.Task<ReadOnlyMemory<System.Byte>>
Parameters
Type | Name |
---|---|
System.IO.Stream | input |
System.Threading.CancellationToken | token |
ReadFullyAsMemoryAsync(Stream, Int32, CancellationToken)
Reads the given stream up to the end, returning the MemoryStream Buffer as ReadOnlyMemory<byte>.
View Source
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
Type | Name |
---|---|
System.IO.Stream | input |
System.Int32 | bufferSize |
System.Threading.CancellationToken | token |
ReadFullyAsMemoryAsync(Stream, Byte[], CancellationToken)
View Source
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
Type | Name |
---|---|
System.IO.Stream | input |
System.Byte[] | buffer |
System.Threading.CancellationToken | token |
CopyTo(Stream, Stream)
Copies all the data from one stream into another.
View Source
public static long CopyTo(this Stream input, Stream output)
Returns
System.Int64
Parameters
Type | Name |
---|---|
System.IO.Stream | input |
System.IO.Stream | output |
CopyTo(Stream, Stream, Int32)
Copies all the data from one stream into another, using a buffer of the given size.
View Source
public static long CopyTo(this Stream input, Stream output, int bufferSize)
Returns
System.Int64
Parameters
Type | Name |
---|---|
System.IO.Stream | input |
System.IO.Stream | output |
System.Int32 | bufferSize |
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
public static long CopyTo(this Stream input, Stream output, byte[] buffer)
Returns
System.Int64
Parameters
Type | Name |
---|---|
System.IO.Stream | input |
System.IO.Stream | output |
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
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
Type | Name |
---|---|
System.IO.Stream | input |
System.IO.Stream | output |
System.Byte[] | buffer |
System.Threading.CancellationToken | token |
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
public static byte[] ReadExactly(this Stream input, int bytesToRead)
Returns
System.Byte[]
Parameters
Type | Name |
---|---|
System.IO.Stream | input |
System.Int32 | bytesToRead |
ReadExactly(Stream, Byte[])
Reads into a buffer, filling it completely.
View Source
public static byte[] ReadExactly(this Stream input, byte[] buffer)
Returns
System.Byte[]
Parameters
Type | Name |
---|---|
System.IO.Stream | input |
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
public static byte[] ReadExactly(this Stream input, byte[] buffer, int bytesToRead)
Returns
System.Byte[]
Parameters
Type | Name |
---|---|
System.IO.Stream | input |
System.Byte[] | buffer |
System.Int32 | bytesToRead |
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
public static byte[] ReadExactly(this Stream input, byte[] buffer, int startIndex, int bytesToRead)
Returns
System.Byte[]
Parameters
Type | Name |
---|---|
System.IO.Stream | input |
System.Byte[] | buffer |
System.Int32 | startIndex |
System.Int32 | bytesToRead |
CollapseWhitespace(String)
View Source
public static string CollapseWhitespace(this string str)
Returns
System.String
Parameters
Type | Name |
---|---|
System.String | str |
Combine(Byte[], Byte[][])
View Source
public static byte[] Combine(this byte[] bytes, params byte[][] withBytes)
Returns
System.Byte[]
Parameters
Type | Name |
---|---|
System.Byte[] | bytes |
System.Byte[][] | withBytes |
WriteAsync(Stream, ReadOnlyMemory<Byte>, CancellationToken)
View Source
public static Task WriteAsync(this Stream stream, ReadOnlyMemory<byte> value, CancellationToken token = default(CancellationToken))
Returns
System.Threading.Tasks.Task
Parameters
Type | Name |
---|---|
System.IO.Stream | stream |
ReadOnlyMemory<System.Byte> | value |
System.Threading.CancellationToken | token |
WriteAsync(Stream, Byte[], CancellationToken)
View Source
public static Task WriteAsync(this Stream stream, byte[] bytes, CancellationToken token = default(CancellationToken))
Returns
System.Threading.Tasks.Task
Parameters
Type | Name |
---|---|
System.IO.Stream | stream |
System.Byte[] | bytes |
System.Threading.CancellationToken | token |
CopyToAsync(Stream, Stream, CancellationToken)
View Source
public static Task CopyToAsync(this Stream input, Stream output, CancellationToken token = default(CancellationToken))
Returns
System.Threading.Tasks.Task
Parameters
Type | Name |
---|---|
System.IO.Stream | input |
System.IO.Stream | output |
System.Threading.CancellationToken | token |
WriteAsync(Stream, String, CancellationToken)
View Source
public static Task WriteAsync(this Stream stream, string text, CancellationToken token = default(CancellationToken))
Returns
System.Threading.Tasks.Task
Parameters
Type | Name |
---|---|
System.IO.Stream | stream |
System.String | text |
System.Threading.CancellationToken | token |
ToMd5Bytes(Stream)
View Source
public static byte[] ToMd5Bytes(this Stream stream)
Returns
System.Byte[]
Parameters
Type | Name |
---|---|
System.IO.Stream | stream |
ToMd5Hash(Stream)
View Source
public static string ToMd5Hash(this Stream stream)
Returns
System.String
Parameters
Type | Name |
---|---|
System.IO.Stream | stream |
ToMd5Hash(Byte[])
View Source
public static string ToMd5Hash(this byte[] bytes)
Returns
System.String
Parameters
Type | Name |
---|---|
System.Byte[] | bytes |
InMemoryStream(Byte[])
Returns bytes in publiclyVisible MemoryStream
View Source
public static MemoryStream InMemoryStream(this byte[] bytes)
Returns
System.IO.MemoryStream
Parameters
Type | Name |
---|---|
System.Byte[] | bytes |
ReadToEnd(MemoryStream)
View Source
public static string ReadToEnd(this MemoryStream ms)
Returns
System.String
Parameters
Type | Name |
---|---|
System.IO.MemoryStream | ms |
ReadToEnd(MemoryStream, Encoding)
View Source
public static string ReadToEnd(this MemoryStream ms, Encoding encoding)
Returns
System.String
Parameters
Type | Name |
---|---|
System.IO.MemoryStream | ms |
System.Text.Encoding | encoding |
GetBufferAsMemory(MemoryStream)
View Source
public static ReadOnlyMemory<byte> GetBufferAsMemory(this MemoryStream ms)
Returns
ReadOnlyMemory<System.Byte>
Parameters
Type | Name |
---|---|
System.IO.MemoryStream | ms |
GetBufferAsSpan(MemoryStream)
View Source
public static ReadOnlySpan<byte> GetBufferAsSpan(this MemoryStream ms)
Returns
ReadOnlySpan<System.Byte>
Parameters
Type | Name |
---|---|
System.IO.MemoryStream | ms |
GetBufferAsBytes(MemoryStream)
View Source
public static byte[] GetBufferAsBytes(this MemoryStream ms)
Returns
System.Byte[]
Parameters
Type | Name |
---|---|
System.IO.MemoryStream | ms |
ReadToEndAsync(MemoryStream)
View Source
public static Task<string> ReadToEndAsync(this MemoryStream ms)
Returns
System.Threading.Tasks.Task<System.String>
Parameters
Type | Name |
---|---|
System.IO.MemoryStream | ms |
ReadToEndAsync(MemoryStream, Encoding)
View Source
public static Task<string> ReadToEndAsync(this MemoryStream ms, Encoding encoding)
Returns
System.Threading.Tasks.Task<System.String>
Parameters
Type | Name |
---|---|
System.IO.MemoryStream | ms |
System.Text.Encoding | encoding |
ReadToEnd(Stream)
View Source
public static string ReadToEnd(this Stream stream)
Returns
System.String
Parameters
Type | Name |
---|---|
System.IO.Stream | stream |
ReadToEnd(Stream, Encoding)
View Source
public static string ReadToEnd(this Stream stream, Encoding encoding)
Returns
System.String
Parameters
Type | Name |
---|---|
System.IO.Stream | stream |
System.Text.Encoding | encoding |
ReadToEndAsync(Stream)
View Source
public static Task<string> ReadToEndAsync(this Stream stream)
Returns
System.Threading.Tasks.Task<System.String>
Parameters
Type | Name |
---|---|
System.IO.Stream | stream |
ReadToEndAsync(Stream, Encoding)
View Source
public static Task<string> ReadToEndAsync(this Stream stream, Encoding encoding)
Returns
System.Threading.Tasks.Task<System.String>
Parameters
Type | Name |
---|---|
System.IO.Stream | stream |
System.Text.Encoding | encoding |
WriteToAsync(MemoryStream, Stream, CancellationToken)
View Source
public static Task WriteToAsync(this MemoryStream stream, Stream output, CancellationToken token = default(CancellationToken))
Returns
System.Threading.Tasks.Task
Parameters
Type | Name |
---|---|
System.IO.MemoryStream | stream |
System.IO.Stream | output |
System.Threading.CancellationToken | token |
WriteToAsync(MemoryStream, Stream, Encoding, CancellationToken)
View Source
public static async Task WriteToAsync(this MemoryStream stream, Stream output, Encoding encoding, CancellationToken token)
Returns
System.Threading.Tasks.Task
Parameters
Type | Name |
---|---|
System.IO.MemoryStream | stream |
System.IO.Stream | output |
System.Text.Encoding | encoding |
System.Threading.CancellationToken | token |
WriteToAsync(Stream, Stream, CancellationToken)
View Source
public static Task WriteToAsync(this Stream stream, Stream output, CancellationToken token = default(CancellationToken))
Returns
System.Threading.Tasks.Task
Parameters
Type | Name |
---|---|
System.IO.Stream | stream |
System.IO.Stream | output |
System.Threading.CancellationToken | token |
WriteToAsync(Stream, Stream, Encoding, CancellationToken)
View Source
public static Task WriteToAsync(this Stream stream, Stream output, Encoding encoding, CancellationToken token)
Returns
System.Threading.Tasks.Task
Parameters
Type | Name |
---|---|
System.IO.Stream | stream |
System.IO.Stream | output |
System.Text.Encoding | encoding |
System.Threading.CancellationToken | token |
CopyToNewMemoryStream(Stream)
View Source
public static MemoryStream CopyToNewMemoryStream(this Stream stream)
Returns
System.IO.MemoryStream
Parameters
Type | Name |
---|---|
System.IO.Stream | stream |
CopyToNewMemoryStreamAsync(Stream)
View Source
public static async Task<MemoryStream> CopyToNewMemoryStreamAsync(this Stream stream)
Returns
System.Threading.Tasks.Task<System.IO.MemoryStream>
Parameters
Type | Name |
---|---|
System.IO.Stream | stream |
WritePartialTo(Stream, Stream, Int64, Int64)
Writes partial range as specified by start-end, from fromStream to toStream.
View Source
[Obsolete("Use WritePartialToAsync")]
public static void WritePartialTo(this Stream fromStream, Stream toStream, long start, long end)
Parameters
Type | Name |
---|---|
System.IO.Stream | fromStream |
System.IO.Stream | toStream |
System.Int64 | start |
System.Int64 | end |
WritePartialToAsync(Stream, Stream, Int64, Int64, CancellationToken)
Writes partial range as specified by start-end, from fromStream to toStream.
View Source
public static async Task WritePartialToAsync(this Stream fromStream, Stream toStream, long start, long end, CancellationToken token = default(CancellationToken))
Returns
System.Threading.Tasks.Task
Parameters
Type | Name |
---|---|
System.IO.Stream | fromStream |
System.IO.Stream | toStream |
System.Int64 | start |
System.Int64 | end |
System.Threading.CancellationToken | token |