public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken) { // Validate arguments as would the base CopyToAsync StreamHelpers.ValidateCopyToArgs(this, destination, bufferSize); // And bail early if cancellation has already been requested if (cancellationToken.IsCancellationRequested) { return(Task.FromCanceled(cancellationToken)); } // Then do additional checks as ReadAsync would. if (_cleanedUp) { throw new ObjectDisposedException(this.GetType().FullName); } Socket streamSocket = _streamSocket; if (streamSocket == null) { throw new IOException(SR.Format(SR.net_io_readfailure, SR.net_io_connectionclosed)); } // Do the copy. We get a copy buffer from the shared pool, and we pass both it and the // socket into the copy as part of the event args so as to avoid additional fields in // the async method's state machine. return(CopyToAsyncCore( destination, new AwaitableSocketAsyncEventArgs(streamSocket, ArrayPool <byte> .Shared.Rent(bufferSize)), cancellationToken)); }
public override void CopyTo(Stream destination, int bufferSize) { StreamHelpers.ValidateCopyToArgs(this, destination, bufferSize); EnsureDecompressionMode(); EnsureNotDisposed(); new CopyToStream(this, destination, bufferSize).CopyFromSourceToDestination(); }
public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken) { ArraySegment<byte> buffer; if (TryGetBuffer(out buffer)) { StreamHelpers.ValidateCopyToArgs(this, destination, bufferSize); long pos = Position; long length = Length; Position = length; long bytesToWrite = length - pos; return destination.WriteAsync(buffer.Array, (int)(buffer.Offset + pos), (int)bytesToWrite, cancellationToken); } return base.CopyToAsync(destination, bufferSize, cancellationToken); }
public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken) { // Validate arguments as would the base CopyToAsync StreamHelpers.ValidateCopyToArgs(this, destination, bufferSize); // And bail early if cancellation has already been requested if (cancellationToken.IsCancellationRequested) { return(Task.FromCanceled(cancellationToken)); } // Do the copy. We get a copy buffer from the shared pool, and we pass both it and the // socket into the copy as part of the event args so as to avoid additional fields in // the async method's state machine. return(CopyToAsyncCore( destination, new AwaitableSocketAsyncEventArgs(_streamSocket, ArrayPool <byte> .Shared.Rent(bufferSize)), cancellationToken)); }
public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken) { // Validation as base CopyToAsync would do StreamHelpers.ValidateCopyToArgs(this, destination, bufferSize); // Validation as ReadAsync would do EnsureDecompressionMode(); EnsureNoActiveAsyncOperation(); EnsureNotDisposed(); // Early check for cancellation if (cancellationToken.IsCancellationRequested) { return(Task.FromCanceled <int>(cancellationToken)); } // Do the copy return(new CopyToAsyncStream(this, destination, bufferSize, cancellationToken).CopyFromSourceToDestination()); }
public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken) { // Validate arguments as would base CopyToAsync StreamHelpers.ValidateCopyToArgs(this, destination, bufferSize); // Check that there are no other pending read operations if (_state.AsyncReadInProgress) { throw new InvalidOperationException(SR.net_http_no_concurrent_io_allowed); } // Early check for cancellation if (cancellationToken.IsCancellationRequested) { return(Task.FromCanceled(cancellationToken)); } // Check out a buffer and start the copy return(CopyToAsyncCore(destination, ArrayPool <byte> .Shared.Rent(bufferSize), cancellationToken)); }
public override Task CopyToAsync( Stream destination, int bufferSize, CancellationToken cancellationToken) { ArraySegment <byte> buffer; if (!this.TryGetBuffer(out buffer)) { return(base.CopyToAsync(destination, bufferSize, cancellationToken)); } StreamHelpers.ValidateCopyToArgs((Stream)this, destination, bufferSize); long position = this.Position; long length = this.Length; this.Position = length; long num = length - position; return(destination.WriteAsync(buffer.Array, (int)((long)buffer.Offset + position), (int)num, cancellationToken)); }