System.IO.Stream.AppendTo(System.IO.Stream)

Here are the examples of the csharp api System.IO.Stream.AppendTo(System.IO.Stream) taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

1 Examples 7

19 Source : ExtensionStream.cs
with MIT License
from getbucket

public static string ToText(this Stream source, Encoding encoding = null, bool closed = true)
        {
            Guard.Requires<ArgumentNullException>(source != null);
            try
            {
                if (!source.CanRead)
                {
                    throw new ConsoleException($"Can not read stream, {nameof(source.CanRead)} == false");
                }

                encoding = encoding ?? Str.Encoding;
                if (source is MemoryStream memoryStream)
                {
                    byte[] innerBuffer;
                    try
                    {
                        innerBuffer = memoryStream.GetBuffer();
                    }
                    catch (UnauthorizedAccessException)
                    {
                        innerBuffer = memoryStream.ToArray();
                    }

                    return encoding.GetString(innerBuffer, 0, (int)memoryStream.Length);
                }

                var length = 0;
                try
                {
                    length = (int)source.Length;
                }
                catch (NotSupportedException)
                {
                    // ignore
                }

                MemoryStream targetStream;
                if (length > 0 && length <= Buffer.Length)
                {
                    targetStream = new MemoryStream(Buffer, 0, Buffer.Length, true, true);
                }
                else
                {
                    targetStream = new MemoryStream(length);
                }

                using (targetStream)
                {
                    var read = source.AppendTo(targetStream);
                    return encoding.GetString(targetStream.GetBuffer(), 0, (int)read);
                }
            }
            finally
            {
                if (closed)
                {
                    source.Dispose();
                }
            }
        }