-
Notifications
You must be signed in to change notification settings - Fork 874
Expand file tree
/
Copy pathSubReadStream.cs
More file actions
201 lines (162 loc) · 5.62 KB
/
SubReadStream.cs
File metadata and controls
201 lines (162 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace Npgsql.Util;
// Adapted from https://github.com/dotnet/runtime/blob/83adfae6a6273d8fb4c69554aa3b1cc7cbf01c71/src/libraries/System.IO.Compression/src/System/IO/Compression/ZipCustomStreams.cs#L221
sealed class SubReadStream : Stream
{
readonly long _startInSuperStream;
long _positionInSuperStream;
readonly long _endInSuperStream;
readonly Stream _superStream;
readonly bool _canSeek;
bool _isDisposed;
public SubReadStream(Stream superStream, long maxLength)
{
_startInSuperStream = -1;
_positionInSuperStream = 0;
_endInSuperStream = maxLength;
_superStream = superStream;
_canSeek = false;
_isDisposed = false;
}
public SubReadStream(Stream superStream, long startPosition, long maxLength)
{
_startInSuperStream = startPosition;
_positionInSuperStream = startPosition;
_endInSuperStream = startPosition + maxLength;
_superStream = superStream;
_canSeek = superStream.CanSeek;
_isDisposed = false;
}
public override long Length
{
get
{
ThrowIfDisposed();
if (!_canSeek)
throw new NotSupportedException();
return _endInSuperStream - _startInSuperStream;
}
}
public override long Position
{
get
{
ThrowIfDisposed();
if (!_canSeek)
throw new NotSupportedException();
return _positionInSuperStream - _startInSuperStream;
}
set
{
ThrowIfDisposed();
throw new NotSupportedException();
}
}
public override bool CanRead => _superStream.CanRead && !_isDisposed;
public override bool CanSeek => false;
public override bool CanWrite => false;
void ThrowIfDisposed()
=> ObjectDisposedException.ThrowIf(_isDisposed, this);
void ThrowIfCantRead()
{
if (!CanRead)
throw new NotSupportedException();
}
public override int Read(byte[] buffer, int offset, int count)
{
// parameter validation sent to _superStream.Read
var origCount = count;
ThrowIfDisposed();
ThrowIfCantRead();
if (_canSeek && _superStream.Position != _positionInSuperStream)
_superStream.Seek(_positionInSuperStream, SeekOrigin.Begin);
if (_positionInSuperStream > _endInSuperStream - count)
count = (int)(_endInSuperStream - _positionInSuperStream);
Debug.Assert(count >= 0);
Debug.Assert(count <= origCount);
var ret = _superStream.Read(buffer, offset, count);
_positionInSuperStream += ret;
return ret;
}
public override int Read(Span<byte> destination)
{
// parameter validation sent to _superStream.Read
var origCount = destination.Length;
var count = destination.Length;
ThrowIfDisposed();
ThrowIfCantRead();
if (_canSeek && _superStream.Position != _positionInSuperStream)
_superStream.Seek(_positionInSuperStream, SeekOrigin.Begin);
if (_positionInSuperStream + count > _endInSuperStream)
count = (int)(_endInSuperStream - _positionInSuperStream);
Debug.Assert(count >= 0);
Debug.Assert(count <= origCount);
var ret = _superStream.Read(destination.Slice(0, count));
_positionInSuperStream += ret;
return ret;
}
public override int ReadByte()
{
Span<byte> b = stackalloc byte[1];
return Read(b) == 1 ? b[0] : -1;
}
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
ValidateBufferArguments(buffer, offset, count);
return ReadAsync(new Memory<byte>(buffer, offset, count), cancellationToken).AsTask();
}
public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default)
{
ThrowIfDisposed();
ThrowIfCantRead();
if (_canSeek && _superStream.Position != _positionInSuperStream)
{
_superStream.Seek(_positionInSuperStream, SeekOrigin.Begin);
}
if (_positionInSuperStream > _endInSuperStream - buffer.Length)
{
buffer = buffer.Slice(0, (int)(_endInSuperStream - _positionInSuperStream));
}
return Core(buffer, cancellationToken);
async ValueTask<int> Core(Memory<byte> buffer, CancellationToken cancellationToken)
{
var ret = await _superStream.ReadAsync(buffer, cancellationToken).ConfigureAwait(false);
_positionInSuperStream += ret;
return ret;
}
}
public override long Seek(long offset, SeekOrigin origin)
{
ThrowIfDisposed();
throw new NotSupportedException();
}
public override void SetLength(long value)
{
ThrowIfDisposed();
throw new NotSupportedException();
}
public override void Write(byte[] buffer, int offset, int count)
{
ThrowIfDisposed();
throw new NotSupportedException();
}
public override void Flush()
{
ThrowIfDisposed();
throw new NotSupportedException();
}
// Close the stream for reading. Note that this does NOT close the superStream (since
// the substream is just 'a chunk' of the super-stream
protected override void Dispose(bool disposing)
{
if (disposing && !_isDisposed)
{
_isDisposed = true;
}
base.Dispose(disposing);
}
}