public async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken ct) { if (_isComplete) { throw new InvalidOperationException("Cannot write anymore to a completed stream"); } ct.ThrowIfCancellationRequested(); _stack.Write(buffer, offset, count); if (_ms.Length > 0) { var data = _ms.GetBuffer(); // This does not copy the array! more efficient! ct.ThrowIfCancellationRequested(); await _stream.WriteAsync(data, 0, (int)_ms.Length, ct).ConfigureAwait(false); _ms.SetLength(0); } }
protected override void FlushBuffer(byte[] b, int offset, int len) { if (_hasDisposed) { throw new ObjectDisposedException(nameof(SecureStoreIndexOutput)); } _length += len; SafeTask.SafeWait(() => _writeStream.WriteAsync(b, offset, len, CancellationToken.None)); }
/// <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; } } }
/// <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; } }