| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Threading; |
| | | 5 | | using System.Threading.Tasks; |
| | | 6 | | |
| | | 7 | | namespace System.IO.Compression |
| | | 8 | | { |
| | | 9 | | internal sealed class PositionPreservingWriteOnlyStreamWrapper : Stream |
| | | 10 | | { |
| | | 11 | | private readonly Stream _stream; |
| | | 12 | | private long _position; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Creates a wrapper for write-only (non-readable, non-seekable) streams that keeps track of <see cref="Positio |
| | | 16 | | /// </summary> |
| | | 17 | | /// <param name="stream">The underlying stream, which handles all actual writes.</param> |
| | 0 | 18 | | public PositionPreservingWriteOnlyStreamWrapper(Stream stream) |
| | 0 | 19 | | { |
| | 0 | 20 | | _stream = stream; |
| | 0 | 21 | | } |
| | | 22 | | |
| | 0 | 23 | | public override bool CanRead => false; |
| | 0 | 24 | | public override bool CanSeek => false; |
| | 0 | 25 | | public override bool CanWrite => true; |
| | | 26 | | |
| | | 27 | | public override long Position |
| | | 28 | | { |
| | 0 | 29 | | get { return _position; } |
| | 0 | 30 | | set { throw new NotSupportedException(SR.NotSupported); } |
| | | 31 | | } |
| | | 32 | | public override void Write(byte[] buffer, int offset, int count) |
| | 0 | 33 | | { |
| | 0 | 34 | | _position += count; |
| | 0 | 35 | | _stream.Write(buffer, offset, count); |
| | 0 | 36 | | } |
| | | 37 | | |
| | | 38 | | public override void Write(ReadOnlySpan<byte> buffer) |
| | 0 | 39 | | { |
| | 0 | 40 | | _position += buffer.Length; |
| | 0 | 41 | | _stream.Write(buffer); |
| | 0 | 42 | | } |
| | | 43 | | |
| | | 44 | | public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? s |
| | 0 | 45 | | { |
| | 0 | 46 | | _position += count; |
| | 0 | 47 | | return _stream.BeginWrite(buffer, offset, count, callback, state); |
| | 0 | 48 | | } |
| | | 49 | | |
| | 0 | 50 | | public override void EndWrite(IAsyncResult asyncResult) => _stream.EndWrite(asyncResult); |
| | | 51 | | |
| | | 52 | | public override void WriteByte(byte value) |
| | 0 | 53 | | { |
| | 0 | 54 | | _position += 1; |
| | 0 | 55 | | _stream.WriteByte(value); |
| | 0 | 56 | | } |
| | | 57 | | |
| | | 58 | | public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) |
| | 0 | 59 | | { |
| | 0 | 60 | | _position += count; |
| | 0 | 61 | | return _stream.WriteAsync(buffer, offset, count, cancellationToken); |
| | 0 | 62 | | } |
| | | 63 | | |
| | | 64 | | public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default( |
| | 0 | 65 | | { |
| | 0 | 66 | | _position += buffer.Length; |
| | 0 | 67 | | return _stream.WriteAsync(buffer, cancellationToken); |
| | 0 | 68 | | } |
| | | 69 | | |
| | 0 | 70 | | public override bool CanTimeout => _stream.CanTimeout; |
| | | 71 | | public override int ReadTimeout |
| | | 72 | | { |
| | 0 | 73 | | get { return _stream.ReadTimeout; } |
| | 0 | 74 | | set { _stream.ReadTimeout = value; } |
| | | 75 | | } |
| | | 76 | | public override int WriteTimeout |
| | | 77 | | { |
| | 0 | 78 | | get { return _stream.WriteTimeout; } |
| | 0 | 79 | | set { _stream.WriteTimeout = value; } |
| | | 80 | | } |
| | | 81 | | |
| | 0 | 82 | | public override void Flush() => _stream.Flush(); |
| | 0 | 83 | | public override Task FlushAsync(CancellationToken cancellationToken) => _stream.FlushAsync(cancellationToken); |
| | | 84 | | |
| | | 85 | | public override void Close() |
| | 0 | 86 | | { |
| | 0 | 87 | | _stream.Close(); |
| | 0 | 88 | | } |
| | | 89 | | |
| | | 90 | | protected override void Dispose(bool disposing) |
| | 0 | 91 | | { |
| | 0 | 92 | | if (disposing) |
| | 0 | 93 | | _stream.Dispose(); |
| | 0 | 94 | | } |
| | | 95 | | |
| | 0 | 96 | | public override ValueTask DisposeAsync() => _stream.DisposeAsync(); |
| | | 97 | | |
| | | 98 | | public override long Length |
| | | 99 | | { |
| | 0 | 100 | | get { throw new NotSupportedException(SR.NotSupported); } |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | public override long Seek(long offset, SeekOrigin origin) |
| | 0 | 104 | | { |
| | 0 | 105 | | throw new NotSupportedException(SR.NotSupported); |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | public override void SetLength(long value) |
| | 0 | 109 | | { |
| | 0 | 110 | | throw new NotSupportedException(SR.NotSupported); |
| | | 111 | | } |
| | | 112 | | |
| | 0 | 113 | | public override int Read(byte[] buffer, int offset, int count) => throw new NotSupportedException(SR.NotSupporte |
| | | 114 | | |
| | 0 | 115 | | public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) = |
| | | 116 | | |
| | 0 | 117 | | public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default) => |
| | | 118 | | } |
| | | 119 | | } |