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
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace System.IO;
/// <summary>
/// Provides extension methods for the <see cref="StreamReader"/> to offer API compatibility with newer .NET versions.
/// </summary>
public static class StreamReaderExtensions
internal static class StreamReaderExtensions
{
/// <summary>
/// Reads a line of characters asynchronously from the current stream and returns the data as a string.
Expand Down
32 changes: 19 additions & 13 deletions src/Ramstack.FileSystem.Abstractions/VirtualFileExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ public static async ValueTask<byte[]> ReadAllBytesAsync(this VirtualFile file, C
if (length > Array.MaxLength)
throw new IOException("The file is too large.");

// https://github.com/dotnet/runtime/blob/5535e31a712343a63f5d7d796cd874e563e5ac14/src/libraries/System.Private.CoreLib/src/System/IO/File.cs#L660
// Some file systems (e.g. procfs on Linux) return 0 for length even when there's content.
// Thus we need to assume 0 doesn't mean empty.
var task = length <= 0
? ReadAllBytesUnknownLengthImplAsync(stream, cancellationToken)
: ReadAllBytesImplAsync(stream, cancellationToken);
Expand All @@ -142,16 +145,19 @@ public static async ValueTask<byte[]> ReadAllBytesAsync(this VirtualFile file, C
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 @@ -171,22 +177,22 @@ static async ValueTask<byte[]> ReadAllBytesUnknownLengthImplAsync(Stream stream,
.ConfigureAwait(false);

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

total += count;
}

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

static byte[] ResizeBuffer(byte[] oldArray)
{
var length = (uint)oldArray.Length * 2;
if (length > (uint)Array.MaxLength)
length = (uint)Math.Max(Array.MaxLength, oldArray.Length + 1);
var length = oldArray.Length * 2;
if ((uint)length > (uint)Array.MaxLength)
length = Math.Max(Array.MaxLength, oldArray.Length + 1);

var newArray = ArrayPool<byte>.Shared.Rent((int)length);
var newArray = ArrayPool<byte>.Shared.Rent(length);
oldArray.AsSpan().TryCopyTo(newArray);

ArrayPool<byte>.Shared.Return(oldArray);
Expand Down
Loading