public StreamCopyToFactory(Stream source, bool buildAsync = true)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            this.source = source;

            if (buildAsync)
            {
                var factory = new StreamLocalAsyncFactory(source);
                readAsync = factory.CreateReadAsyncMethod();
            }
        }
        private async Task CopyToAsyncInternal(Stream destination, Int32 bufferSize, CancellationToken cancellationToken)
        {
            Debug.Assert(destination != null);
            Debug.Assert(bufferSize > 0);
            Debug.Assert(source.CanRead);
            Debug.Assert(destination.CanWrite);

            var destinationFactory = new StreamLocalAsyncFactory(destination);
            var writeAsync         = destinationFactory.CreateWriteAsyncMethod();

            destinationFactory = null;

            byte[] buffer = new byte[bufferSize];
            while (true)
            {
                int bytesRead = await readAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false);

                if (bytesRead == 0)
                {
                    break;
                }
                await writeAsync(buffer, 0, bytesRead, cancellationToken).ConfigureAwait(false);
            }
        }
Exemple #3
0
        public static Task WriteAsync(this Stream stream, byte[] buffer, int offset, int count, CancellationToken cancellationToken)
        {
            var writeAsync = new StreamLocalAsyncFactory(stream).CreateWriteAsyncMethod();

            return(writeAsync(buffer, offset, count, cancellationToken));
        }
Exemple #4
0
        public static Task <int> ReadAsync(this Stream stream, byte[] buffer, int offset, int count)
        {
            var readAsync = new StreamLocalAsyncFactory(stream).CreateReadAsyncMethod();

            return(readAsync(buffer, offset, count));
        }
Exemple #5
0
        public static Task FlushAsync(this Stream stream, CancellationToken cancellationToken)
        {
            var flushAsync = new StreamLocalAsyncFactory(stream).CreateFlushAsyncMethod();

            return(flushAsync(cancellationToken));
        }
Exemple #6
0
        public static Task FlushAsync(this Stream stream)
        {
            var flushAsync = new StreamLocalAsyncFactory(stream).CreateFlushAsyncMethod();

            return(flushAsync());
        }