Esempio n. 1
0
        /// <summary>Reads the contents of the stream asynchronously.</summary>
        /// <param name="stream">The stream.</param>
        /// <returns>A Task representing the contents of the file in bytes.</returns>
        public static Task <byte[]> ReadAllBytesAsync(this Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            int          capacity = stream.CanSeek ? ((int)stream.Length) : 0;
            MemoryStream readData = new MemoryStream(capacity);

            return(stream.CopyStreamToStreamAsync(readData).ContinueWith <byte[]>(delegate(Task t) {
                t.PropagateExceptions();
                return readData.ToArray();
            }));
        }
Esempio n. 2
0
        /// <summary>
        /// Reads the contents of the stream asynchronously.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <returns>A Task representing the contents of the file in bytes.</returns>
        public static Task <byte[]> ReadAllBytesAsync(this Stream stream)
        {
            Contract.Requires(stream != null);

            // Create a MemoryStream to store the data read from the input stream
            int initialCapacity = stream.CanSeek ? (int)stream.Length : 0;
            var readData        = new MemoryStream(initialCapacity);

            // Copy from the source stream to the memory stream and return the copied data
            return(stream.CopyStreamToStreamAsync(readData).ContinueWith(t =>
            {
                t.PropagateExceptions();
                return readData.ToArray();
            }));
        }
Esempio n. 3
0
        /// <summary>Reads the contents of the stream asynchronously.</summary>
        /// <param name="stream">The stream.</param>
        /// <returns>A Task representing the contents of the file in bytes.</returns>
        public static Task <byte[]> ReadAllBytesAsync(this System.IO.Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            // Create a MemoryStream to store the data read from the input stream
            int initialCapacity = stream.CanSeek ? (int)stream.Length : 0;
            var readData        = new MemoryStream(initialCapacity);

            // Copy from the source stream to the memory stream and return the copied data
            return(stream.CopyStreamToStreamAsync(readData).ContinueWith(t =>
            {
                t.PropagateExceptions();
                return readData.ToArray();
            }));
        }
Esempio n. 4
0
        /// <summary>Copies the contents of a stream to a file, asynchronously.</summary>
        /// <param name="source">The source stream.</param>
        /// <param name="destinationPath">The path to the destination file.</param>
        /// <returns>A Task that represents the asynchronous operation.</returns>
        public static Task CopyStreamToFileAsync(this Stream source, string destinationPath)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            if (destinationPath == null)
            {
                throw new ArgumentNullException("destinationPath");
            }
            FileStream destinationStream = FileAsync.OpenWrite(destinationPath);

            return(source.CopyStreamToStreamAsync(destinationStream).ContinueWith(delegate(Task t) {
                AggregateException exception = t.Exception;
                destinationStream.Close();
                if (exception != null)
                {
                    throw exception;
                }
            }, TaskContinuationOptions.ExecuteSynchronously));
        }