forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNpgsqlReadBuffer.Stream.cs
More file actions
184 lines (156 loc) · 6.89 KB
/
NpgsqlReadBuffer.Stream.cs
File metadata and controls
184 lines (156 loc) · 6.89 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
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace Npgsql
{
public sealed partial class NpgsqlReadBuffer
{
internal sealed class ColumnStream : Stream
{
readonly NpgsqlReadBuffer _buf;
int _start, _len, _read;
bool _canSeek;
internal bool IsDisposed { get; private set; }
internal ColumnStream(NpgsqlReadBuffer buf)
=> _buf = buf;
internal void Init(int len, bool canSeek)
{
Debug.Assert(!canSeek || _buf.ReadBytesLeft >= len,
"Seekable stream constructed but not all data is in buffer (sequential)");
_start = _buf.ReadPosition;
_len = len;
_read = 0;
_canSeek = canSeek;
IsDisposed = false;
}
public override bool CanRead => true;
public override bool CanWrite => false;
public override bool CanSeek => _canSeek;
public override long Length
{
get
{
CheckDisposed();
return _len;
}
}
public override void SetLength(long value)
=> throw new NotSupportedException();
public override long Position
{
get
{
CheckDisposed();
return _read;
}
set
{
if (value < 0)
throw new ArgumentOutOfRangeException("Non - negative number required.");
Seek(_start + value, SeekOrigin.Begin);
}
}
public override long Seek(long offset, SeekOrigin origin)
{
CheckDisposed();
if (!_canSeek)
throw new NotSupportedException();
if (offset > int.MaxValue)
throw new ArgumentOutOfRangeException(nameof(offset), "Stream length must be non-negative and less than 2^31 - 1 - origin.");
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 = _start;
return tempPosition;
}
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;
return tempPosition;
}
case SeekOrigin.End:
{
var tempPosition = unchecked(_len + (int)offset);
if (unchecked(_len + offset) < _start || tempPosition < _start)
throw new IOException(SeekBeforeBegin);
_buf.ReadPosition = tempPosition;
return tempPosition;
}
default:
throw new ArgumentOutOfRangeException(nameof(origin), "Invalid seek origin.");
}
}
public override void Flush()
=> throw new NotSupportedException();
public override Task FlushAsync(CancellationToken cancellationToken)
=> throw new NotSupportedException();
public override int Read(byte[] buffer, int offset, int count)
=> Read(buffer, offset, count, CancellationToken.None, false).GetAwaiter().GetResult();
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
using (NoSynchronizationContextScope.Enter())
return Read(buffer, offset, count, cancellationToken, true).AsTask();
}
ValueTask<int> Read(byte[] buffer, int offset, int count, CancellationToken cancellationToken, bool async)
{
CheckDisposed();
if (buffer == null)
throw new ArgumentNullException(nameof(buffer));
if (offset < 0)
throw new ArgumentNullException(nameof(offset));
if (count < 0)
throw new ArgumentNullException(nameof(count));
if (buffer.Length - offset < count)
throw new ArgumentException("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.");
if (cancellationToken.IsCancellationRequested)
return new ValueTask<int>(Task.FromCanceled<int>(cancellationToken));
count = Math.Min(count, _len - _read);
if (count == 0)
return new ValueTask<int>(0);
var task = _buf.ReadBytes(buffer, offset, count, async);
if (task.IsCompleted) // This may be a bug in the new version of ValueTask
{
_read += task.Result;
return task;
}
return new ValueTask<int>(ReadLong(task, cancellationToken, async));
}
async Task<int> ReadLong(ValueTask<int> task, CancellationToken cancellationToken, bool async)
{
var read = async
? await task
: task.GetAwaiter().GetResult();
_read += read;
return read;
}
public override void Write(byte[] buffer, int offset, int count)
=> throw new NotSupportedException();
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
=> throw new NotSupportedException();
void CheckDisposed()
{
if (IsDisposed)
throw new ObjectDisposedException(null);
}
protected override void Dispose(bool disposing)
{
if (IsDisposed)
return;
var leftToSkip = _len - _read;
if (leftToSkip > 0)
_buf.Skip(leftToSkip, false).GetAwaiter().GetResult();
IsDisposed = true;
}
}
}
}