Ejemplo n.º 1
0
        /// <summary>
        /// Read all the bytes from a stream
        /// </summary>
        public static async Task <byte[]> ReadBytes(this IReadAsyncStream readStream)
        {
            using (var ms = new MemoryStream())
            {
                await readStream.CopyToStream(ms, CancellationToken.None).ConfigureAwait(false);

                return(ms.ToArray());
            }
        }
Ejemplo n.º 2
0
            public ReadWatcherStream(IReadAsyncStream stream)
            {
                _stream  = stream;
                _buffer  = new byte[BufferSize * 2]; // We make larger buffer sizes to try to avoid extra data pull
                _buffer2 = new byte[BufferSize * 2];

                _bufferNeedsData  = true;
                _bufferNeedsData2 = true;
                _isFirstBuffer    = true;
            }
Ejemplo n.º 3
0
        /// <summary>
        /// Straight copy from the reader to the writer
        /// </summary>
        public static async Task CopyToAsync(this IReadAsyncStream readStream, IWriteAsyncStream writeStream, CancellationToken ct)
        {
            // We have the double exception catching to avoid the using() from hiding the inner exception
            Exception inner = null;

            try
            {
                using (readStream)
                {
                    try
                    {
                        var buffer = new byte[BufferSize];
                        int read;
                        do
                        {
                            ct.ThrowIfCancellationRequested();
                            read = await readStream.ReadAsync(buffer, 0, buffer.Length, ct).ConfigureAwait(false);

                            if (read > 0)
                            {
                                ct.ThrowIfCancellationRequested();
                                await writeStream.WriteAsync(buffer, 0, read, ct).ConfigureAwait(false);
                            }
                        } while (read > 0);
                    }
                    catch (Exception e)
                    {
                        inner = e;
                        throw;
                    }
                }
            }
            catch (Exception)
            {
                if (inner != null)
                {
                    throw inner;
                }
                else
                {
                    throw;
                }
            }
        }
Ejemplo n.º 4
0
            public ReadWatcherStream(IReadAsyncStream stream)
            {
                _stream = stream;
                _buffer = new byte[BufferSize * 2]; // We make larger buffer sizes to try to avoid extra data pull 
                _buffer2 = new byte[BufferSize * 2];

                _bufferNeedsData = true;
                _bufferNeedsData2 = true;
                _isFirstBuffer = true;
            }
Ejemplo n.º 5
0
 public ReadWriteAsyncStream(IReadAsyncStream stream, Func<Stream, Stream> readStack)
 {
     _stream = stream;
     _readStream = new ReadWatcherStream(stream);
     _stack = readStack(_readStream);
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="stream">A stream of data</param>
 /// <param name="metadata">Metadata to include</param>
 public DataWithMetadata(IReadAsyncStream stream, Metadata metadata = null)
 {
     _metadata = metadata ?? new Metadata();
     _stream = stream;
 }
Ejemplo n.º 7
0
 public ReadWriteAsyncStream(IReadAsyncStream stream, Func <Stream, Stream> readStack)
 {
     _stream     = stream;
     _readStream = new ReadWatcherStream(stream);
     _stack      = readStack(_readStream);
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Create a read stream that will transform any data (using streams) before returning your actual read data
 /// </summary>
 /// <param name="stream">Underlying read stream to pull data from before transforming</param>
 /// <param name="readStack">Using the passed read stream, wrap to create your data transformations</param>
 /// <returns>A read stream</returns>
 public static IReadAsyncStream AddTransformer(this IReadAsyncStream stream, Func <Stream, Stream> readStack)
 {
     return(new ReadWriteAsyncStream(stream, readStack));
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="stream">A stream of data</param>
 /// <param name="metadata">Metadata to include</param>
 public DataWithMetadata(IReadAsyncStream stream, Metadata metadata = null)
 {
     _metadata = metadata ?? new Metadata();
     _stream   = stream;
 }