System.IO.Compression.DeflateStream.ReadAsync C# (CSharp) Method

ReadAsync() public method

public ReadAsync ( byte array, int offset, int count, CancellationToken cancellationToken ) : Task
array byte
offset int
count int
cancellationToken System.Threading.CancellationToken
return Task
        public override Task<int> ReadAsync(byte[] array, int offset, int count, CancellationToken cancellationToken)
        {
            // We use this checking order for compat to earlier versions:
            EnsureDecompressionMode();
            EnsureNoActiveAsyncOperation();
            ValidateParameters(array, offset, count);
            EnsureNotDisposed();

            if (cancellationToken.IsCancellationRequested)
            {
                return Task.FromCanceled<int>(cancellationToken);
            }

            EnsureBufferInitialized();
            Task<int> readTask = null;

            AsyncOperationStarting();
            try
            {
                // Try to read decompressed data in output buffer
                int bytesRead = _inflater.Inflate(array, offset, count);
                if (bytesRead != 0)
                {
                    // If decompression output buffer is not empty, return immediately.
                    return Task.FromResult(bytesRead);
                }

                if (_inflater.Finished())
                {
                    // end of compression stream
                    return Task.FromResult(0);
                }

                // If there is no data on the output buffer and we are not at 
                // the end of the stream, we need to get more data from the base stream
                readTask = _stream.ReadAsync(_buffer, 0, _buffer.Length, cancellationToken);
                if (readTask == null)
                {
                    throw new InvalidOperationException(SR.NotSupported_UnreadableStream);
                }

                return ReadAsyncCore(readTask, array, offset, count, cancellationToken);
            }
            finally
            {
                // if we haven't started any async work, decrement the counter to end the transaction
                if (readTask == null)
                {
                    AsyncOperationCompleting();
                }
            }
        }

Same methods

DeflateStream::ReadAsync ( byte array, int offset, int count, System cancellationToken ) : System.Threading.Tasks.Task

Usage Example

Example #1
0
 public override Task <int> ReadAsync(Byte[] array, int offset, int count, CancellationToken cancellationToken)
 {
     CheckDeflateStream();
     return(_deflateStream.ReadAsync(array, offset, count, cancellationToken));
 }
All Usage Examples Of System.IO.Compression.DeflateStream::ReadAsync