-
Notifications
You must be signed in to change notification settings - Fork 874
Expand file tree
/
Copy pathNpgsqlReadBuffer.Stream.cs
More file actions
228 lines (189 loc) · 7.57 KB
/
NpgsqlReadBuffer.Stream.cs
File metadata and controls
228 lines (189 loc) · 7.57 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace Npgsql.Internal;
sealed partial class NpgsqlReadBuffer
{
internal sealed class ColumnStream : Stream
{
readonly NpgsqlConnector _connector;
readonly NpgsqlReadBuffer _buf;
long _startPos;
int _start;
int _read;
bool _canSeek;
bool _commandScoped;
bool _consumeOnDispose;
/// Does not throw ODE.
internal int CurrentLength { get; private set; }
internal bool IsDisposed { get; private set; }
internal ColumnStream(NpgsqlConnector connector)
{
_connector = connector;
_buf = connector.ReadBuffer;
IsDisposed = true;
}
internal void Init(int len, bool canSeek, bool commandScoped, bool consumeOnDispose = true)
{
Debug.Assert(!canSeek || _buf.ReadBytesLeft >= len,
"Seekable stream constructed but not all data is in buffer (sequential)");
_startPos = _buf.CumulativeReadPosition;
_canSeek = canSeek;
_start = canSeek ? _buf.ReadPosition : 0;
CurrentLength = len;
_read = 0;
_commandScoped = commandScoped;
_consumeOnDispose = consumeOnDispose;
IsDisposed = false;
}
public override bool CanRead => true;
public override bool CanWrite => false;
public override bool CanSeek => _canSeek;
public override long Length
{
get
{
CheckDisposed();
return CurrentLength;
}
}
public override void SetLength(long value)
=> throw new NotSupportedException();
public override long Position
{
get
{
CheckDisposed();
return _read;
}
set
{
ArgumentOutOfRangeException.ThrowIfNegative(value);
Seek(value, SeekOrigin.Begin);
}
}
public override long Seek(long offset, SeekOrigin origin)
{
CheckDisposed();
if (!_canSeek)
throw new NotSupportedException();
ArgumentOutOfRangeException.ThrowIfGreaterThan(offset, int.MaxValue);
const string seekBeforeBegin = "An attempt was made to move the position before the beginning of the stream.";
switch (origin)
{
case SeekOrigin.Begin:
{
var tempPosition = unchecked(_start + (int)offset);
if (offset < 0 || tempPosition < _start)
throw new IOException(seekBeforeBegin);
_buf.ReadPosition = tempPosition;
_read = (int)offset;
return _read;
}
case SeekOrigin.Current:
{
var tempPosition = unchecked(_buf.ReadPosition + (int)offset);
if (unchecked(_buf.ReadPosition + offset) < _start || tempPosition < _start)
throw new IOException(seekBeforeBegin);
_buf.ReadPosition = tempPosition;
_read += (int)offset;
return _read;
}
case SeekOrigin.End:
{
var tempPosition = unchecked(_start + CurrentLength + (int)offset);
if (unchecked(_start + CurrentLength + offset) < _start || tempPosition < _start)
throw new IOException(seekBeforeBegin);
_buf.ReadPosition = tempPosition;
_read = CurrentLength + (int)offset;
return _read;
}
default:
throw new ArgumentOutOfRangeException(nameof(origin), "Invalid seek origin.");
}
}
public override void Flush()
=> CheckDisposed();
public override Task FlushAsync(CancellationToken cancellationToken)
{
CheckDisposed();
return cancellationToken.IsCancellationRequested
? Task.FromCanceled(cancellationToken) : Task.CompletedTask;
}
public override int ReadByte()
{
Span<byte> byteSpan = stackalloc byte[1];
var read = Read(byteSpan);
return read > 0 ? byteSpan[0] : -1;
}
public override int Read(byte[] buffer, int offset, int count)
{
ValidateArguments(buffer, offset, count);
return Read(new Span<byte>(buffer, offset, count));
}
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
ValidateArguments(buffer, offset, count);
return ReadAsync(new Memory<byte>(buffer, offset, count), cancellationToken).AsTask();
}
public override int Read(Span<byte> span)
{
CheckDisposed();
var count = Math.Min(span.Length, CurrentLength - _read);
if (count == 0)
return 0;
var read = _buf.Read(_commandScoped, span.Slice(0, count));
_read += read;
return read;
}
public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default)
{
CheckDisposed();
var count = Math.Min(buffer.Length, CurrentLength - _read);
return count == 0 ? new ValueTask<int>(0) : ReadLong(this, buffer.Slice(0, count), cancellationToken);
static async ValueTask<int> ReadLong(ColumnStream stream, Memory<byte> buffer, CancellationToken cancellationToken = default)
{
using var registration = cancellationToken.CanBeCanceled
? stream._connector.StartNestedCancellableOperation(cancellationToken, attemptPgCancellation: false)
: default;
var read = await stream._buf.ReadAsync(stream._commandScoped, buffer, cancellationToken).ConfigureAwait(false);
stream._read += read;
return read;
}
}
public override void Write(byte[] buffer, int offset, int count)
=> throw new NotSupportedException();
void CheckDisposed()
=> ObjectDisposedException.ThrowIf(IsDisposed, this);
protected override void Dispose(bool disposing)
{
if (disposing)
DisposeCore(async: false).GetAwaiter().GetResult();
}
public override ValueTask DisposeAsync()
=> DisposeCore(async: true);
async ValueTask DisposeCore(bool async)
{
if (IsDisposed)
return;
if (_consumeOnDispose && !_connector.IsBroken)
{
var pos = _buf.CumulativeReadPosition - _startPos;
var remaining = checked((int)(CurrentLength - pos));
if (remaining > 0)
await _buf.Skip(async, remaining).ConfigureAwait(false);
}
IsDisposed = true;
}
}
static void ValidateArguments(byte[] buffer, int offset, int count)
{
ArgumentNullException.ThrowIfNull(buffer);
ArgumentOutOfRangeException.ThrowIfNegative(offset);
ArgumentOutOfRangeException.ThrowIfNegative(count);
if (buffer.Length - offset < count)
ThrowHelper.ThrowArgumentException("Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.");
}
}