feanz
1/9/2015 - 4:03 PM

Stream extension methods

Stream extension methods

public static Stream ToStream(this string str)
{
    byte[] byteArray = Encoding.UTF8.GetBytes(str);
    //byte[] byteArray = Encoding.ASCII.GetBytes(str);
    return new MemoryStream(byteArray);
}
public static string ToString(this Stream stream)
{
    var reader = new StreamReader(stream);
    return reader.ReadToEnd();
}
/// <summary>
/// Copy from one stream to another.
/// Example:
/// using(var stream = response.GetResponseStream())
/// using(var ms = new MemoryStream())
/// {
///     stream.CopyTo(ms);
///      // Do something with copied data
/// }
/// </summary>
/// <param name="fromStream">From stream.</param>
/// <param name="toStream">To stream.</param>
public static void CopyTo(this Stream fromStream, Stream toStream)
{
    if (fromStream == null)
        throw new ArgumentNullException("fromStream");
    if (toStream == null)
        throw new ArgumentNullException("toStream");
    var bytes = new byte[8092];
    int dataRead;
    while ((dataRead = fromStream.Read(bytes, 0, bytes.Length)) > 0)
        toStream.Write(bytes, 0, dataRead);
}