diff --git a/Directory.Packages.props b/Directory.Packages.props
index 602e0f0..6f2bf25 100644
--- a/Directory.Packages.props
+++ b/Directory.Packages.props
@@ -4,9 +4,9 @@
true
-
-
-
+
+
+
diff --git a/src/Ramstack.FileProviders.Extensions/FileInfoExtensions.cs b/src/Ramstack.FileProviders.Extensions/FileInfoExtensions.cs
index c033aee..646ef04 100644
--- a/src/Ramstack.FileProviders.Extensions/FileInfoExtensions.cs
+++ b/src/Ramstack.FileProviders.Extensions/FileInfoExtensions.cs
@@ -75,7 +75,7 @@ public static byte[] ReadAllBytes(this IFileInfo file)
// ReSharper disable once UseAwaitUsing
using var stream = file.OpenRead();
- var length = GetStreamLength(stream);
+ var length = stream.CanSeek ? stream.Length : 0;
if (length > Array.MaxLength)
throw new IOException("The file is too large.");
@@ -89,16 +89,15 @@ public static byte[] ReadAllBytes(this IFileInfo file)
static byte[] ReadAllBytesImpl(Stream stream)
{
var bytes = new byte[stream.Length];
- var index = 0;
+ var total = 0;
do
{
- var count = stream.Read(bytes.AsSpan(index));
+ var count = stream.Read(bytes.AsSpan(total));
if (count == 0)
Error_EndOfStream();
- index += count;
- }
- while (index < bytes.Length);
+ total += count;
+ } while (total < bytes.Length);
return bytes;
}
@@ -106,26 +105,23 @@ static byte[] ReadAllBytesImpl(Stream stream)
static byte[] ReadAllBytesUnknownLengthImpl(Stream stream)
{
var bytes = ArrayPool.Shared.Rent(512);
- var index = 0;
+ var total = 0;
- try
+ while (true)
{
- while (true)
- {
- if (index == bytes.Length)
- bytes = ResizeBuffer(bytes);
+ if (total == bytes.Length)
+ bytes = ResizeBuffer(bytes);
- var count = stream.Read(bytes.AsSpan(index));
- if (count == 0)
- return bytes.AsSpan(0, index).ToArray();
+ var count = stream.Read(bytes.AsSpan(total));
+ if (count == 0)
+ break;
- index += count;
- }
- }
- finally
- {
- ArrayPool.Shared.Return(bytes);
+ total += count;
}
+
+ var result = bytes.AsSpan(0, total).ToArray();
+ ArrayPool.Shared.Return(bytes);
+ return result;
}
}
@@ -139,7 +135,7 @@ static byte[] ReadAllBytesUnknownLengthImpl(Stream stream)
/// containing the full text from the current file.
///
public static ValueTask ReadAllTextAsync(this IFileInfo file, CancellationToken cancellationToken = default) =>
- ReadAllTextAsync(file, null, cancellationToken);
+ ReadAllTextAsync(file, encoding: null, cancellationToken);
///
/// Asynchronously reads all the text in the current file with the specified encoding.
@@ -170,7 +166,7 @@ public static async ValueTask ReadAllTextAsync(this IFileInfo file, Enco
/// containing an array of all lines in the current file.
///
public static ValueTask ReadAllLinesAsync(this IFileInfo file, CancellationToken cancellationToken = default) =>
- ReadAllLinesAsync(file, Encoding.UTF8, cancellationToken);
+ ReadAllLinesAsync(file, encoding: null, cancellationToken);
///
/// Asynchronously reads all lines of the current file with the specified encoding.
@@ -209,7 +205,7 @@ public static async ValueTask ReadAllBytesAsync(this IFileInfo file, Can
// ReSharper disable once UseAwaitUsing
using var stream = file.OpenRead();
- var length = GetStreamLength(stream);
+ var length = stream.CanSeek ? stream.Length : 0;
if (length > Array.MaxLength)
throw new IOException("The file is too large.");
@@ -225,16 +221,16 @@ public static async ValueTask ReadAllBytesAsync(this IFileInfo file, Can
static async ValueTask ReadAllBytesImplAsync(Stream stream, CancellationToken cancellationToken)
{
var bytes = new byte[stream.Length];
- var index = 0;
+ var total = 0;
+
do
{
- var count = await stream.ReadAsync(bytes.AsMemory(index), cancellationToken).ConfigureAwait(false);
+ var count = await stream.ReadAsync(bytes.AsMemory(total), cancellationToken).ConfigureAwait(false);
if (count == 0)
Error_EndOfStream();
- index += count;
- }
- while (index < bytes.Length);
+ total += count;
+ } while (total < bytes.Length);
return bytes;
}
@@ -244,27 +240,24 @@ static async ValueTask ReadAllBytesUnknownLengthImplAsync(Stream stream,
var bytes = ArrayPool.Shared.Rent(512);
var total = 0;
- try
+ while (true)
{
- while (true)
- {
- if (total == bytes.Length)
- bytes = ResizeBuffer(bytes);
+ if (total == bytes.Length)
+ bytes = ResizeBuffer(bytes);
- var count = await stream
- .ReadAsync(bytes.AsMemory(total), cancellationToken)
- .ConfigureAwait(false);
+ var count = await stream
+ .ReadAsync(bytes.AsMemory(total), cancellationToken)
+ .ConfigureAwait(false);
- if (count == 0)
- return bytes.AsSpan(0, total).ToArray();
+ if (count == 0)
+ break;
- total += count;
- }
- }
- finally
- {
- ArrayPool.Shared.Return(bytes);
+ total += count;
}
+
+ var result = bytes.AsSpan(0, total).ToArray();
+ ArrayPool.Shared.Return(bytes);
+ return result;
}
}
@@ -275,29 +268,12 @@ private static byte[] ResizeBuffer(byte[] oldArray)
length = Math.Max(Array.MaxLength, oldArray.Length + 1);
var newArray = ArrayPool.Shared.Rent(length);
-
- Debug.Assert(oldArray.Length <= newArray.Length);
oldArray.AsSpan().TryCopyTo(newArray);
ArrayPool.Shared.Return(oldArray);
return newArray;
}
- private static long GetStreamLength(Stream stream)
- {
- try
- {
- if (stream.CanSeek)
- return stream.Length;
- }
- catch
- {
- // skip
- }
-
- return 0;
- }
-
private static void Error_EndOfStream() =>
throw new EndOfStreamException();
}