-
Notifications
You must be signed in to change notification settings - Fork 753
Expand file tree
/
Copy pathUtf8JsonStreamReader.cs
More file actions
397 lines (328 loc) · 12.8 KB
/
Utf8JsonStreamReader.cs
File metadata and controls
397 lines (328 loc) · 12.8 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
namespace NuGet.Shared
{
/// <summary>
/// This struct is used to read over a memeory stream in parts, in order to avoid reading the entire stream into memory.
/// It functions as a wrapper around <see cref="Utf8JsonStreamReader"/>, while maintaining a stream and a buffer to read from.
/// </summary>
internal ref struct Utf8JsonStreamReader
{
private static readonly char[] DelimitedStringDelimiters = [' ', ','];
private static readonly byte[] Utf8Bom = [0xEF, 0xBB, 0xBF];
private static readonly JsonReaderOptions DefaultJsonReaderOptions = new JsonReaderOptions
{
AllowTrailingCommas = true,
CommentHandling = JsonCommentHandling.Skip,
};
private const int BufferSizeDefault = 16 * 1024;
private const int MinBufferSize = 1024;
private Utf8JsonReader _reader;
#pragma warning disable CA2213 // Disposable fields should be disposed
private Stream _stream;
#pragma warning restore CA2213 // Disposable fields should be disposed
// The buffer is used to read from the stream in chunks.
private byte[] _buffer;
private bool _disposed;
private ArrayPool<byte> _bufferPool;
private int _bufferUsed = 0;
internal Utf8JsonStreamReader(Stream stream, int bufferSize = BufferSizeDefault, ArrayPool<byte> arrayPool = null)
{
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
if (bufferSize < MinBufferSize)
{
throw new ArgumentException($"Buffer size must be at least {MinBufferSize} bytes", nameof(bufferSize));
}
_bufferPool = arrayPool ?? ArrayPool<byte>.Shared;
_buffer = _bufferPool.Rent(bufferSize);
_disposed = false;
_stream = stream;
if (_stream.Read(_buffer, offset: 0, count: 1) == 1 &&
_stream.Read(_buffer, offset: ++_bufferUsed, count: 1) == 1 &&
_stream.Read(_buffer, offset: ++_bufferUsed, count: 1) == 1)
{
++_bufferUsed;
bool hasUtf8Bom = Utf8Bom.AsSpan().SequenceEqual(_buffer.AsSpan(start: 0, length: 3));
if (hasUtf8Bom)
{
_bufferUsed = 0;
}
}
var initialJsonReaderState = new JsonReaderState(DefaultJsonReaderOptions);
ReadStreamIntoBuffer(initialJsonReaderState);
_reader.Read();
}
internal bool IsFinalBlock => _reader.IsFinalBlock;
internal JsonTokenType TokenType => _reader.TokenType;
internal bool ValueTextEquals(ReadOnlySpan<byte> utf8Text) => _reader.ValueTextEquals(utf8Text);
internal bool TryGetInt32(out int value) => _reader.TryGetInt32(out value);
internal string GetString() => _reader.GetString();
internal bool GetBoolean() => _reader.GetBoolean();
internal int GetInt32() => _reader.GetInt32();
internal int CurrentDepth => _reader.CurrentDepth;
internal bool Read()
{
ThrowExceptionIfDisposed();
bool wasRead;
while (!(wasRead = _reader.Read()) && !_reader.IsFinalBlock)
{
GetMoreBytesFromStream();
}
return wasRead;
}
internal void Skip()
{
ThrowExceptionIfDisposed();
bool wasSkipped;
while (!(wasSkipped = _reader.TrySkip()) && !_reader.IsFinalBlock)
{
GetMoreBytesFromStream();
}
if (!wasSkipped)
{
_reader.Skip();
}
}
internal IList<T> ReadObjectAsList<T>(IUtf8JsonStreamReaderConverter<T> streamReaderConverter)
{
if (TokenType == JsonTokenType.Null)
{
return Array.Empty<T>();
}
if (TokenType != JsonTokenType.StartObject)
{
throw new JsonException($"Expected start object token but instead found '{TokenType}'");
}
//We use JsonObjects for the arrays so we advance to the first property in the object which is the name/ver of the first library
Read();
if (TokenType == JsonTokenType.EndObject)
{
return Array.Empty<T>();
}
var listObjects = new List<T>();
do
{
listObjects.Add(streamReaderConverter.Read(ref this));
//At this point we're looking at the EndObject token for the object, need to advance.
Read();
}
while (TokenType != JsonTokenType.EndObject);
return listObjects;
}
internal IList<T> ReadListOfObjects<T>(IUtf8JsonStreamReaderConverter<T> streamReaderConverter)
{
if (TokenType != JsonTokenType.StartArray)
{
throw new JsonException($"Expected start array token but instead found '{TokenType}'");
}
IList<T> objectList = null;
if (TokenType == JsonTokenType.StartArray)
{
while (Read() && TokenType != JsonTokenType.EndArray)
{
var convertedObject = streamReaderConverter.Read(ref this);
if (convertedObject != null)
{
objectList ??= new List<T>();
objectList.Add(convertedObject);
}
}
}
return objectList ?? Array.Empty<T>();
}
internal string ReadNextTokenAsString()
{
ThrowExceptionIfDisposed();
if (Read())
{
return _reader.ReadTokenAsString();
}
return null;
}
internal IList<string> ReadStringArrayAsIList(IList<string> strings = null)
{
if (TokenType == JsonTokenType.StartArray)
{
while (Read() && TokenType != JsonTokenType.EndArray)
{
string value = _reader.ReadTokenAsString();
strings ??= new List<string>();
strings.Add(value);
}
}
return strings;
}
internal ImmutableArray<string> ReadStringArrayAsImmutableArray()
{
string[] strings = null;
var index = 0;
if (TokenType == JsonTokenType.StartArray)
{
while (Read() && TokenType != JsonTokenType.EndArray)
{
if (strings == null)
{
strings = ArrayPool<string>.Shared.Rent(16);
}
else if (strings.Length == index)
{
var oldStrings = strings;
strings = ArrayPool<string>.Shared.Rent(strings.Length * 2);
oldStrings.CopyTo(strings, index: 0);
ArrayPool<string>.Shared.Return(oldStrings);
}
strings[index++] = _reader.ReadTokenAsString();
}
}
if (strings == null)
{
return [];
}
var retVal = strings.AsSpan(0, index).ToImmutableArray();
ArrayPool<string>.Shared.Return(strings);
return retVal;
}
internal IReadOnlyList<string> ReadDelimitedString()
{
ThrowExceptionIfDisposed();
if (Read())
{
switch (TokenType)
{
case JsonTokenType.String:
var value = GetString();
return value.Split(DelimitedStringDelimiters, StringSplitOptions.RemoveEmptyEntries);
default:
throw new InvalidCastException();
}
}
return null;
}
internal bool ReadNextTokenAsBoolOrFalse()
{
ThrowExceptionIfDisposed();
if (Read() && (TokenType == JsonTokenType.False || TokenType == JsonTokenType.True))
{
return GetBoolean();
}
return false;
}
internal bool ReadNextTokenAsBoolOrThrowAnException(byte[] propertyName, string invalidAttributeString)
{
ThrowExceptionIfDisposed();
if (Read() && (TokenType == JsonTokenType.False || TokenType == JsonTokenType.True))
{
return GetBoolean();
}
else
{
throw new ArgumentException(
string.Format(CultureInfo.CurrentCulture,
invalidAttributeString,
Encoding.UTF8.GetString(propertyName),
_reader.ReadTokenAsString(),
"false"));
}
}
internal IReadOnlyList<string> ReadNextStringOrArrayOfStringsAsReadOnlyList()
{
ThrowExceptionIfDisposed();
if (Read())
{
switch (_reader.TokenType)
{
case JsonTokenType.String:
return new[] { _reader.GetString() };
case JsonTokenType.StartArray:
return ReadStringArrayAsReadOnlyListFromArrayStart();
case JsonTokenType.StartObject:
return null;
}
}
return null;
}
internal IReadOnlyList<string> ReadStringArrayAsReadOnlyListFromArrayStart()
{
ThrowExceptionIfDisposed();
List<string> strings = null;
while (Read() && _reader.TokenType != JsonTokenType.EndArray)
{
string value = _reader.ReadTokenAsString();
strings ??= new List<string>();
strings.Add(value);
}
return (IReadOnlyList<string>)strings ?? Array.Empty<string>();
}
// This function is called when Read() returns false and we're not already in the final block
private void GetMoreBytesFromStream()
{
if (_reader.BytesConsumed < _bufferUsed)
{
// If the number of bytes consumed by the reader is less than the amount set in the buffer then we have leftover bytes
var oldBuffer = _buffer;
ReadOnlySpan<byte> leftover = oldBuffer.AsSpan((int)_reader.BytesConsumed);
_bufferUsed = leftover.Length;
// If the leftover bytes are the same as the buffer size then we are at capacity and need to double the buffer size
if (leftover.Length == _buffer.Length)
{
_buffer = _bufferPool.Rent(_buffer.Length * 2);
leftover.CopyTo(_buffer);
_bufferPool.Return(oldBuffer, true);
}
else
{
leftover.CopyTo(_buffer);
}
}
else
{
_bufferUsed = 0;
}
ReadStreamIntoBuffer(_reader.CurrentState);
}
/// <summary>
/// Loops through the stream and reads it into the buffer until the buffer is full or the stream is empty, creates the Utf8JsonReader.
/// </summary>
private void ReadStreamIntoBuffer(JsonReaderState jsonReaderState)
{
int bytesRead;
do
{
var spaceLeftInBuffer = _buffer.Length - _bufferUsed;
bytesRead = _stream.Read(_buffer, _bufferUsed, spaceLeftInBuffer);
_bufferUsed += bytesRead;
}
while (bytesRead != 0 && _bufferUsed != _buffer.Length);
_reader = new Utf8JsonReader(_buffer.AsSpan(0, _bufferUsed), isFinalBlock: bytesRead == 0, jsonReaderState);
}
public void Dispose()
{
if (!_disposed)
{
_disposed = true;
byte[] toReturn = _buffer;
_buffer = null!;
_bufferPool.Return(toReturn, true);
}
}
private void ThrowExceptionIfDisposed()
{
if (_disposed)
{
throw new ObjectDisposedException(nameof(Utf8JsonStreamReader));
}
}
}
}