| | | 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.Buffers; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.Threading; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | |
| | | 9 | | namespace System.IO |
| | | 10 | | { |
| | | 11 | | // Stream that wraps a super stream and exposes a window [startPosition, startPosition + maxLength). |
| | | 12 | | // Supports both seekable and unseekable super streams. When the super stream is seekable, this stream |
| | | 13 | | // is seekable too and will reposition the super stream before each read if needed. When the super |
| | | 14 | | // stream is unseekable, reads must be sequential and Seek/Position-set throw. |
| | | 15 | | // Does not support writing. |
| | | 16 | | internal sealed class SubReadStream : Stream |
| | | 17 | | { |
| | | 18 | | private const int MaxAdvanceBufferLength = 4096; |
| | | 19 | | |
| | | 20 | | private bool _hasReachedEnd; |
| | | 21 | | private readonly long _startInSuperStream; |
| | | 22 | | private long _positionInSuperStream; |
| | | 23 | | private readonly long _endInSuperStream; |
| | | 24 | | private readonly Stream _superStream; |
| | | 25 | | private bool _isDisposed; |
| | | 26 | | |
| | 0 | 27 | | public SubReadStream(Stream superStream, long startPosition, long maxLength) |
| | 0 | 28 | | { |
| | 0 | 29 | | ArgumentNullException.ThrowIfNull(superStream); |
| | 0 | 30 | | if (!superStream.CanRead) |
| | 0 | 31 | | { |
| | 0 | 32 | | throw new ArgumentException(SR.IO_NotSupported_UnreadableStream, nameof(superStream)); |
| | | 33 | | } |
| | 0 | 34 | | ArgumentOutOfRangeException.ThrowIfNegative(startPosition); |
| | 0 | 35 | | ArgumentOutOfRangeException.ThrowIfNegative(maxLength); |
| | 0 | 36 | | ArgumentOutOfRangeException.ThrowIfGreaterThan(startPosition, long.MaxValue - maxLength); |
| | | 37 | | |
| | 0 | 38 | | _startInSuperStream = startPosition; |
| | 0 | 39 | | _positionInSuperStream = startPosition; |
| | 0 | 40 | | _endInSuperStream = startPosition + maxLength; |
| | 0 | 41 | | _superStream = superStream; |
| | 0 | 42 | | } |
| | | 43 | | |
| | | 44 | | public override long Length |
| | | 45 | | { |
| | | 46 | | get |
| | 0 | 47 | | { |
| | 0 | 48 | | ThrowIfDisposed(); |
| | 0 | 49 | | return _endInSuperStream - _startInSuperStream; |
| | 0 | 50 | | } |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | public override long Position |
| | | 54 | | { |
| | | 55 | | get |
| | 0 | 56 | | { |
| | 0 | 57 | | ThrowIfDisposed(); |
| | 0 | 58 | | return _positionInSuperStream - _startInSuperStream; |
| | 0 | 59 | | } |
| | | 60 | | set |
| | 0 | 61 | | { |
| | 0 | 62 | | ThrowIfDisposed(); |
| | 0 | 63 | | if (!CanSeek) |
| | 0 | 64 | | { |
| | 0 | 65 | | throw new NotSupportedException(SR.IO_NotSupported_UnseekableStream); |
| | | 66 | | } |
| | 0 | 67 | | ArgumentOutOfRangeException.ThrowIfNegative(value); |
| | | 68 | | |
| | 0 | 69 | | long newPositionInSuperStream = _startInSuperStream + value; |
| | 0 | 70 | | _superStream.Position = newPositionInSuperStream; |
| | 0 | 71 | | _positionInSuperStream = newPositionInSuperStream; |
| | 0 | 72 | | } |
| | | 73 | | } |
| | | 74 | | |
| | 0 | 75 | | public override bool CanRead => !_isDisposed && _superStream.CanRead; |
| | | 76 | | |
| | 0 | 77 | | public override bool CanSeek => !_isDisposed && _superStream.CanSeek; |
| | | 78 | | |
| | 0 | 79 | | public override bool CanWrite => false; |
| | | 80 | | |
| | 0 | 81 | | private long Remaining => _endInSuperStream - _positionInSuperStream; |
| | | 82 | | |
| | 0 | 83 | | private int LimitByRemaining(int bufferSize) => (int)Math.Max(0, Math.Min(Remaining, bufferSize)); |
| | | 84 | | |
| | | 85 | | // Positions the super stream past the end of this stream's window. After calling this method, |
| | | 86 | | // subsequent reads on this stream throw <see cref="EndOfStreamException"/>. |
| | | 87 | | internal void AdvanceToEnd() |
| | | 88 | | { |
| | | 89 | | _hasReachedEnd = true; |
| | | 90 | | |
| | | 91 | | long remaining = Remaining; |
| | | 92 | | _positionInSuperStream = _endInSuperStream; |
| | | 93 | | AdvanceSuperStream(remaining); |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | internal ValueTask AdvanceToEndAsync(CancellationToken cancellationToken) |
| | | 97 | | { |
| | | 98 | | _hasReachedEnd = true; |
| | | 99 | | |
| | | 100 | | long remaining = Remaining; |
| | | 101 | | _positionInSuperStream = _endInSuperStream; |
| | | 102 | | return AdvanceSuperStreamAsync(remaining, cancellationToken); |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | private void AdvanceSuperStream(long bytesToDiscard) |
| | | 106 | | { |
| | | 107 | | if (_superStream.CanSeek) |
| | | 108 | | { |
| | | 109 | | _superStream.Position += bytesToDiscard; |
| | | 110 | | } |
| | | 111 | | else if (bytesToDiscard > 0) |
| | | 112 | | { |
| | | 113 | | byte[] buffer = ArrayPool<byte>.Shared.Rent((int)Math.Min(MaxAdvanceBufferLength, bytesToDiscard)); |
| | | 114 | | try |
| | | 115 | | { |
| | | 116 | | while (bytesToDiscard > 0) |
| | | 117 | | { |
| | | 118 | | int currentLengthToRead = (int)Math.Min(MaxAdvanceBufferLength, bytesToDiscard); |
| | | 119 | | _superStream.ReadExactly(buffer.AsSpan(0, currentLengthToRead)); |
| | | 120 | | bytesToDiscard -= currentLengthToRead; |
| | | 121 | | } |
| | | 122 | | } |
| | | 123 | | finally |
| | | 124 | | { |
| | | 125 | | ArrayPool<byte>.Shared.Return(buffer); |
| | | 126 | | } |
| | | 127 | | } |
| | | 128 | | } |
| | | 129 | | |
| | | 130 | | private async ValueTask AdvanceSuperStreamAsync(long bytesToDiscard, CancellationToken cancellationToken) |
| | | 131 | | { |
| | | 132 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 133 | | |
| | | 134 | | if (_superStream.CanSeek) |
| | | 135 | | { |
| | | 136 | | _superStream.Position += bytesToDiscard; |
| | | 137 | | } |
| | | 138 | | else if (bytesToDiscard > 0) |
| | | 139 | | { |
| | | 140 | | byte[] buffer = ArrayPool<byte>.Shared.Rent((int)Math.Min(MaxAdvanceBufferLength, bytesToDiscard)); |
| | | 141 | | try |
| | | 142 | | { |
| | | 143 | | while (bytesToDiscard > 0) |
| | | 144 | | { |
| | | 145 | | int currentLengthToRead = (int)Math.Min(MaxAdvanceBufferLength, bytesToDiscard); |
| | | 146 | | await _superStream.ReadExactlyAsync(buffer, 0, currentLengthToRead, cancellationToken).Configure |
| | | 147 | | bytesToDiscard -= currentLengthToRead; |
| | | 148 | | } |
| | | 149 | | } |
| | | 150 | | finally |
| | | 151 | | { |
| | | 152 | | ArrayPool<byte>.Shared.Return(buffer); |
| | | 153 | | } |
| | | 154 | | } |
| | | 155 | | } |
| | | 156 | | |
| | | 157 | | private void ThrowIfDisposed() |
| | 0 | 158 | | { |
| | 0 | 159 | | ObjectDisposedException.ThrowIf(_isDisposed, this); |
| | 0 | 160 | | } |
| | | 161 | | |
| | | 162 | | private void ThrowIfCantRead() |
| | 0 | 163 | | { |
| | 0 | 164 | | if (!_superStream.CanRead) |
| | 0 | 165 | | { |
| | 0 | 166 | | throw new NotSupportedException(SR.IO_NotSupported_UnreadableStream); |
| | | 167 | | } |
| | 0 | 168 | | } |
| | | 169 | | |
| | | 170 | | private void ThrowIfBeyondEndOfStream() |
| | 0 | 171 | | { |
| | 0 | 172 | | if (_hasReachedEnd) |
| | 0 | 173 | | { |
| | 0 | 174 | | throw new EndOfStreamException(); |
| | | 175 | | } |
| | 0 | 176 | | } |
| | | 177 | | |
| | | 178 | | public override int Read(byte[] buffer, int offset, int count) |
| | 0 | 179 | | { |
| | 0 | 180 | | ValidateBufferArguments(buffer, offset, count); |
| | 0 | 181 | | return Read(buffer.AsSpan(offset, count)); |
| | 0 | 182 | | } |
| | | 183 | | |
| | | 184 | | public override int Read(Span<byte> destination) |
| | 0 | 185 | | { |
| | 0 | 186 | | ThrowIfDisposed(); |
| | 0 | 187 | | ThrowIfCantRead(); |
| | 0 | 188 | | ThrowIfBeyondEndOfStream(); |
| | | 189 | | |
| | 0 | 190 | | if (_superStream.CanSeek && _superStream.Position != _positionInSuperStream) |
| | 0 | 191 | | { |
| | 0 | 192 | | _superStream.Seek(_positionInSuperStream, SeekOrigin.Begin); |
| | 0 | 193 | | } |
| | | 194 | | |
| | 0 | 195 | | destination = destination[..LimitByRemaining(destination.Length)]; |
| | | 196 | | |
| | 0 | 197 | | int ret = _superStream.Read(destination); |
| | | 198 | | |
| | 0 | 199 | | _positionInSuperStream += ret; |
| | 0 | 200 | | return ret; |
| | 0 | 201 | | } |
| | | 202 | | |
| | | 203 | | public override int ReadByte() |
| | 0 | 204 | | { |
| | 0 | 205 | | byte b = default; |
| | 0 | 206 | | return Read(new Span<byte>(ref b)) == 1 ? b : -1; |
| | 0 | 207 | | } |
| | | 208 | | |
| | | 209 | | public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) |
| | 0 | 210 | | { |
| | 0 | 211 | | ValidateBufferArguments(buffer, offset, count); |
| | 0 | 212 | | return ReadAsync(new Memory<byte>(buffer, offset, count), cancellationToken).AsTask(); |
| | 0 | 213 | | } |
| | | 214 | | |
| | | 215 | | public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default) |
| | 0 | 216 | | { |
| | 0 | 217 | | ThrowIfDisposed(); |
| | 0 | 218 | | ThrowIfCantRead(); |
| | 0 | 219 | | ThrowIfBeyondEndOfStream(); |
| | 0 | 220 | | return ReadAsyncCore(buffer, cancellationToken); |
| | 0 | 221 | | } |
| | | 222 | | |
| | | 223 | | private async ValueTask<int> ReadAsyncCore(Memory<byte> buffer, CancellationToken cancellationToken) |
| | 0 | 224 | | { |
| | 0 | 225 | | Debug.Assert(!_hasReachedEnd); |
| | | 226 | | |
| | 0 | 227 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 228 | | |
| | 0 | 229 | | if (_superStream.CanSeek && _superStream.Position != _positionInSuperStream) |
| | 0 | 230 | | { |
| | 0 | 231 | | _superStream.Seek(_positionInSuperStream, SeekOrigin.Begin); |
| | 0 | 232 | | } |
| | | 233 | | |
| | 0 | 234 | | buffer = buffer[..LimitByRemaining(buffer.Length)]; |
| | | 235 | | |
| | 0 | 236 | | int ret = await _superStream.ReadAsync(buffer, cancellationToken).ConfigureAwait(false); |
| | | 237 | | |
| | 0 | 238 | | _positionInSuperStream += ret; |
| | 0 | 239 | | return ret; |
| | 0 | 240 | | } |
| | | 241 | | |
| | | 242 | | public override long Seek(long offset, SeekOrigin origin) |
| | 0 | 243 | | { |
| | 0 | 244 | | ThrowIfDisposed(); |
| | | 245 | | |
| | 0 | 246 | | if (!CanSeek) |
| | 0 | 247 | | { |
| | 0 | 248 | | throw new NotSupportedException(SR.IO_NotSupported_UnseekableStream); |
| | | 249 | | } |
| | | 250 | | |
| | 0 | 251 | | long newPositionInSuperStream = origin switch |
| | 0 | 252 | | { |
| | 0 | 253 | | SeekOrigin.Begin => _startInSuperStream + offset, |
| | 0 | 254 | | SeekOrigin.Current => _positionInSuperStream + offset, |
| | 0 | 255 | | SeekOrigin.End => _endInSuperStream + offset, |
| | 0 | 256 | | _ => throw new ArgumentOutOfRangeException(nameof(origin)), |
| | 0 | 257 | | }; |
| | | 258 | | |
| | 0 | 259 | | if (newPositionInSuperStream < _startInSuperStream) |
| | 0 | 260 | | { |
| | 0 | 261 | | throw new IOException(SR.IO_SeekBeforeBegin); |
| | | 262 | | } |
| | | 263 | | |
| | 0 | 264 | | long actualPositionInSuperStream = _superStream.Seek(newPositionInSuperStream, SeekOrigin.Begin); |
| | 0 | 265 | | _positionInSuperStream = actualPositionInSuperStream; |
| | | 266 | | |
| | 0 | 267 | | return _positionInSuperStream - _startInSuperStream; |
| | 0 | 268 | | } |
| | | 269 | | |
| | 0 | 270 | | public override void SetLength(long value) => throw new NotSupportedException(SR.IO_NotSupported_UnwritableStrea |
| | | 271 | | |
| | 0 | 272 | | public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException(SR.IO_NotSup |
| | | 273 | | |
| | 0 | 274 | | public override void Flush() { } |
| | | 275 | | |
| | | 276 | | public override Task FlushAsync(CancellationToken cancellationToken) => |
| | 0 | 277 | | cancellationToken.IsCancellationRequested ? Task.FromCanceled(cancellationToken) : |
| | 0 | 278 | | Task.CompletedTask; |
| | | 279 | | |
| | | 280 | | // Close the stream for reading. Note that this does NOT close the super stream (since |
| | | 281 | | // this stream is just a 'chunk' of the super stream). |
| | | 282 | | protected override void Dispose(bool disposing) |
| | 0 | 283 | | { |
| | 0 | 284 | | _isDisposed = true; |
| | 0 | 285 | | base.Dispose(disposing); |
| | 0 | 286 | | } |
| | | 287 | | } |
| | | 288 | | } |