C# Extension methods for the Stream class.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Extensions
{
    public static class StreamExtensions
    {
        public static byte[] ReadAllBytes(this Stream stream)
        {
            var memoryStream1 = stream as MemoryStream;
            if (memoryStream1 != null)
            {
                return memoryStream1.ToArray();
            }

            MemoryStream memoryStream2;
            using (memoryStream2 = new MemoryStream())
            {
                stream.CopyTo(memoryStream2);
                return memoryStream2.ToArray();
            }
        }

        public static Func<Stream> ToStreamFactory(this Stream stream)
        {
            byte[] buffer;
            using (var memoryStream = new MemoryStream())
            {
                try
                {
                    stream.CopyTo(memoryStream);
                    buffer = memoryStream.ToArray();
                }
                finally
                {
                    stream.Close();
                }
            }
            return () => (Stream)new MemoryStream(buffer);
        }

        public static string ReadToEnd(this Stream stream)
        {
            using (var streamReader = new StreamReader(stream))
            {
                return streamReader.ReadToEnd();
            }
        }

        public static Stream AsStream(this string value)
        {
            return value.AsStream(Encoding.UTF8);
        }

        public static Stream AsStream(this string value, Encoding encoding)
        {
            return new MemoryStream(encoding.GetBytes(value));
        }

        public static bool ContentEquals(this Stream stream, Stream otherStream)
        {
            bool flag = IsBinary(otherStream);
            otherStream.Seek(0L, SeekOrigin.Begin);

            if (!flag)
            {
                return CompareText(stream, otherStream);
            }

            return CompareBinary(stream, otherStream);
        }

        public static bool IsBinary(Stream stream)
        {
            byte[] numArray = new byte[30];
            int count = stream.Read(numArray, 0, 30);

            return Array.FindIndex(
                numArray, 0, count, d => (int)d == 0) >= 0;
        }

        private static bool CompareText(Stream stream, Stream otherStream)
        {
            return ReadStreamLines(stream).SequenceEqual(ReadStreamLines(otherStream),
                StringComparer.Ordinal);
        }

        private static IEnumerable<string> ReadStreamLines(Stream stream)
        {
            using (var streamReader = new StreamReader(stream))
            {
                while (streamReader.Peek() != -1)
                {
                    string line = streamReader.ReadLine();
                    yield return line;
                }
            }
        }

        private static bool CompareBinary(Stream stream, Stream otherStream)
        {
            if (stream.CanSeek && otherStream.CanSeek
                && stream.Length != otherStream.Length)
            {
                return false;
            }

            byte[] buffer1 = new byte[4096];
            byte[] buffer2 = new byte[4096];

            int count;
            do
            {
                count = stream.Read(buffer1, 0, buffer1.Length);
                if (count > 0)
                {
                    int num = otherStream.Read(buffer2, 0, count);
                    if (count != num)
                    {
                        return false;
                    }
                    for (int index = 0; index < count; ++index)
                    {
                        if (buffer1[index] != buffer2[index])
                        {
                            return false;
                        }
                    }
                }
            }

            while (count > 0);

            return true;
        }
    }
}