Skip to content

Commit 7200a8b

Browse files
committed
Lucene.Net.Support.IO.StreamExtensions::Read(Span<byte>, long): Moved position checks into "seek" block, since checking stream.Length will throw on a non-seekable stream and RandomAccess.Read() repeats the < 0 check. Also added a missing null guard clause and updated the docs for Read(Span<byte>) and Write(ReadOnlySpan<byte>).
1 parent 0792630 commit 7200a8b

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

src/Lucene.Net/Support/IO/StreamExtensions.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using J2N.IO;
21
using Lucene.Net.Support.Threading;
32
using System;
43
using System.Buffers;
@@ -81,18 +80,18 @@ public static int Read(this FileStream stream, Span<byte> destination, long posi
8180
{
8281
if (stream is null)
8382
throw new ArgumentNullException(nameof(stream));
84-
if (position < 0)
85-
throw new ArgumentOutOfRangeException(nameof(position));
86-
if (position > stream.Length)
87-
return 0;
8883

8984
#if FEATURE_RANDOMACCESS_READ
9085
return RandomAccess.Read(stream.SafeFileHandle, destination, position);
9186
#else
87+
if (position < 0)
88+
throw new ArgumentOutOfRangeException(nameof(position));
9289
if (!stream.CanSeek)
9390
throw new NotSupportedException("Stream does not support seeking.");
9491
if (!stream.CanRead)
9592
throw new NotSupportedException("Stream does not support reading.");
93+
if (position > stream.Length)
94+
return 0;
9695

9796
int read;
9897
object readLock = lockCache.GetOrCreateValue(stream);
@@ -130,9 +129,13 @@ public static int Read(this FileStream stream, Span<byte> destination, long posi
130129
/// available, or zero (0) if the buffer's length is zero or the end of
131130
/// the stream has been reached.</returns>
132131
/// <exception cref="IOException">An I/O error occurs.</exception>
132+
/// <exception cref="ArgumentNullException"><paramref name="stream"/> is <c>null</c>.</exception>
133133
/// <remarks>This is to patch .NET Standard and .NET Framework.</remarks>
134134
public static int Read(this Stream stream, Span<byte> buffer)
135135
{
136+
if (stream is null)
137+
throw new ArgumentNullException(nameof(stream));
138+
136139
byte[] sharedBuffer = ArrayPool<byte>.Shared.Rent(buffer.Length);
137140
try
138141
{
@@ -157,7 +160,7 @@ public static int Read(this Stream stream, Span<byte> buffer)
157160
/// </summary>
158161
/// <param name="stream">The stream to write to.</param>
159162
/// <param name="buffer">A region of memory. This method copies the contents of this region to the current stream.</param>
160-
/// <exception cref="ArgumentNullException"><paramref name="buffer"/> is <c>null</c>.</exception>
163+
/// <exception cref="ArgumentNullException"><paramref name="stream"/> is <c>null</c>.</exception>
161164
/// <remarks>This is to patch .NET Standard and .NET Framework.</remarks>
162165
public static void Write(this Stream stream, ReadOnlySpan<byte> buffer)
163166
{

0 commit comments

Comments
 (0)