Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Microsoft.Extensions.FileProviders.Abstractions" Version="6.0.0" />
<PackageVersion Include="Microsoft.Extensions.FileProviders.Composite" Version="6.0.0" />
<PackageVersion Include="Microsoft.Extensions.FileProviders.Physical" Version="6.0.0" />
<PackageVersion Include="Microsoft.Extensions.FileProviders.Abstractions" Version="6.0.1" />
<PackageVersion Include="Microsoft.Extensions.FileProviders.Composite" Version="6.0.1" />
<PackageVersion Include="Microsoft.Extensions.FileProviders.Physical" Version="6.0.1" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
<PackageVersion Include="MinVer" Version="6.0.0" />
Expand Down
100 changes: 38 additions & 62 deletions src/Ramstack.FileProviders.Extensions/FileInfoExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.");

Expand All @@ -89,43 +89,39 @@ 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;
}

static byte[] ReadAllBytesUnknownLengthImpl(Stream stream)
{
var bytes = ArrayPool<byte>.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<byte>.Shared.Return(bytes);
total += count;
}

var result = bytes.AsSpan(0, total).ToArray();
ArrayPool<byte>.Shared.Return(bytes);
return result;
}
}

Expand All @@ -139,7 +135,7 @@ static byte[] ReadAllBytesUnknownLengthImpl(Stream stream)
/// containing the full text from the current file.
/// </returns>
public static ValueTask<string> ReadAllTextAsync(this IFileInfo file, CancellationToken cancellationToken = default) =>
ReadAllTextAsync(file, null, cancellationToken);
ReadAllTextAsync(file, encoding: null, cancellationToken);

/// <summary>
/// Asynchronously reads all the text in the current file with the specified encoding.
Expand Down Expand Up @@ -170,7 +166,7 @@ public static async ValueTask<string> ReadAllTextAsync(this IFileInfo file, Enco
/// containing an array of all lines in the current file.
/// </returns>
public static ValueTask<string[]> ReadAllLinesAsync(this IFileInfo file, CancellationToken cancellationToken = default) =>
ReadAllLinesAsync(file, Encoding.UTF8, cancellationToken);
ReadAllLinesAsync(file, encoding: null, cancellationToken);

/// <summary>
/// Asynchronously reads all lines of the current file with the specified encoding.
Expand Down Expand Up @@ -209,7 +205,7 @@ public static async ValueTask<byte[]> 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.");

Expand All @@ -225,16 +221,16 @@ public static async ValueTask<byte[]> ReadAllBytesAsync(this IFileInfo file, Can
static async ValueTask<byte[]> 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;
}
Expand All @@ -244,27 +240,24 @@ static async ValueTask<byte[]> ReadAllBytesUnknownLengthImplAsync(Stream stream,
var bytes = ArrayPool<byte>.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<byte>.Shared.Return(bytes);
total += count;
}

var result = bytes.AsSpan(0, total).ToArray();
ArrayPool<byte>.Shared.Return(bytes);
return result;
}
}

Expand All @@ -275,29 +268,12 @@ private static byte[] ResizeBuffer(byte[] oldArray)
length = Math.Max(Array.MaxLength, oldArray.Length + 1);

var newArray = ArrayPool<byte>.Shared.Rent(length);

Debug.Assert(oldArray.Length <= newArray.Length);
oldArray.AsSpan().TryCopyTo(newArray);

ArrayPool<byte>.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();
}
Loading