You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -278,33 +296,65 @@ public override byte[] ToArray()
278
296
/// <returns>
279
297
/// An array of bytes that was read from the internal buffer.
280
298
/// </returns>
281
-
/// <exception cref="ArgumentOutOfRangeException"><paramref name="length"/> is greater than the internal buffer size.</exception>
282
-
privatebyte[]ReadBytes(intlength)
299
+
/// <exception cref="EndOfStreamException"><paramref name="length"/> is greater than the internal buffer size.</exception>
300
+
internalbyte[]ReadBytes(intlength)
283
301
{
284
302
vardata=newbyte[length];
285
-
varbytesRead=Read(data,0,length);
286
303
287
-
if(bytesRead<length)
288
-
{
289
-
thrownewArgumentOutOfRangeException(nameof(length),string.Format(CultureInfo.InvariantCulture,"The requested length ({0}) is greater than the actual number of bytes read ({1}).",length,bytesRead));
290
-
}
304
+
ReadExactly(data);
291
305
292
306
returndata;
293
307
}
294
308
295
-
#if NET6_0_OR_GREATER
309
+
#if NET6_0
296
310
/// <summary>
297
-
/// Fills the specified span with bytes from the internal buffer.
311
+
/// Reads bytes from the current stream and advances the position within the stream until the <paramref name="buffer"/> is filled.
298
312
/// </summary>
299
-
/// <param name="destination">The span to fill.</param>
300
-
/// <exception cref="ArgumentException">The Length of <paramref name="destination"/> is greater than the actual number of bytes read.</exception>
301
-
privatevoidReadBytes(Span<byte>destination)
313
+
/// <param name="buffer">A region of memory. When this method returns, the contents of this region are replaced by the bytes read from the current stream.</param>
314
+
/// <exception cref="EndOfStreamException">
315
+
/// The end of the stream is reached before filling the <paramref name="buffer"/>.
316
+
/// </exception>
317
+
/// <remarks>
318
+
/// When <paramref name="buffer"/> is empty, this read operation will be completed without waiting for available data in the stream.
319
+
/// </remarks>
320
+
privatevoidReadExactly(Span<byte>buffer)
302
321
{
303
-
varbytesRead=Read(destination);
322
+
vartotalRead=0;
323
+
while(totalRead<buffer.Length)
324
+
{
325
+
varread=Read(buffer.Slice(totalRead));
326
+
if(read==0)
327
+
{
328
+
thrownewEndOfStreamException();
329
+
}
330
+
331
+
totalRead+=read;
332
+
}
333
+
}
304
334
305
-
if(bytesRead<destination.Length)
335
+
#elif !NET7_0_OR_GREATER
336
+
/// <summary>
337
+
/// Reads bytes from the current stream and advances the position within the stream until the <paramref name="buffer"/> is filled.
338
+
/// </summary>
339
+
/// <param name="buffer">A region of memory. When this method returns, the contents of this region are replaced by the bytes read from the current stream.</param>
340
+
/// <exception cref="EndOfStreamException">
341
+
/// The end of the stream is reached before filling the <paramref name="buffer"/>.
342
+
/// </exception>
343
+
/// <remarks>
344
+
/// When <paramref name="buffer"/> is empty, this read operation will be completed without waiting for available data in the stream.
345
+
/// </remarks>
346
+
privatevoidReadExactly(byte[]buffer)
347
+
{
348
+
vartotalRead=0;
349
+
while(totalRead<buffer.Length)
306
350
{
307
-
thrownewArgumentException(nameof(destination),string.Format(CultureInfo.InvariantCulture,"The requested length ({0}) is greater than the actual number of bytes read ({1}).",destination.Length,bytesRead));
0 commit comments