コード例 #1
0
        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();
            }
        }
コード例 #2
0
        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);
            }
        }
コード例 #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));
        }
コード例 #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));
        }
コード例 #5
0
        public static Task FlushAsync(this Stream stream, CancellationToken cancellationToken)
        {
            var flushAsync = new StreamLocalAsyncFactory(stream).CreateFlushAsyncMethod();

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

            return(flushAsync());
        }