/// <summary>
        /// Given the <paramref name="inputStream"/> this method returns a task which will asynchronously
        /// read the entire content of that stream and return a new synchronous stream from which the data can be read.
        /// </summary>
        /// <param name="inputStream">The input stream to asynchronously buffer.</param>
        /// <returns>A task which returns the buffered stream.</returns>
        internal static Task <BufferedReadStream> BufferStreamAsync(Stream inputStream)
        {
            Debug.Assert(inputStream != null, "inputStream != null");

            BufferedReadStream bufferedReadStream = new BufferedReadStream(inputStream);

            // Note that this relies on lazy eval of the enumerator
            return(Task.Factory.Iterate(bufferedReadStream.BufferInputStream())
                   .FollowAlwaysWith((task) => inputStream.Dispose())
                   .FollowOnSuccessWith(
                       (task) =>
            {
                bufferedReadStream.ResetForReading();
                return bufferedReadStream;
            }));
        }