Skip to content

Commit 5d99620

Browse files
authored
Merge pull request #73 from rameel/code-polish
Clean up and formatting
2 parents cee0602 + 9d3f773 commit 5d99620

2 files changed

Lines changed: 20 additions & 14 deletions

File tree

src/Ramstack.FileSystem.Abstractions/Utilities/StreamReaderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace System.IO;
77
/// <summary>
88
/// Provides extension methods for the <see cref="StreamReader"/> to offer API compatibility with newer .NET versions.
99
/// </summary>
10-
public static class StreamReaderExtensions
10+
internal static class StreamReaderExtensions
1111
{
1212
/// <summary>
1313
/// Reads a line of characters asynchronously from the current stream and returns the data as a string.

src/Ramstack.FileSystem.Abstractions/VirtualFileExtensions.cs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ public static async ValueTask<byte[]> ReadAllBytesAsync(this VirtualFile file, C
133133
if (length > Array.MaxLength)
134134
throw new IOException("The file is too large.");
135135

136+
// https://github.com/dotnet/runtime/blob/5535e31a712343a63f5d7d796cd874e563e5ac14/src/libraries/System.Private.CoreLib/src/System/IO/File.cs#L660
137+
// Some file systems (e.g. procfs on Linux) return 0 for length even when there's content.
138+
// Thus we need to assume 0 doesn't mean empty.
136139
var task = length <= 0
137140
? ReadAllBytesUnknownLengthImplAsync(stream, cancellationToken)
138141
: ReadAllBytesImplAsync(stream, cancellationToken);
@@ -142,16 +145,19 @@ public static async ValueTask<byte[]> ReadAllBytesAsync(this VirtualFile file, C
142145
static async ValueTask<byte[]> ReadAllBytesImplAsync(Stream stream, CancellationToken cancellationToken)
143146
{
144147
var bytes = new byte[stream.Length];
145-
var index = 0;
148+
var total = 0;
146149

147150
do
148151
{
149-
var count = await stream.ReadAsync(bytes.AsMemory(index), cancellationToken).ConfigureAwait(false);
152+
var count = await stream
153+
.ReadAsync(bytes.AsMemory(total), cancellationToken)
154+
.ConfigureAwait(false);
155+
150156
if (count == 0)
151157
Error_EndOfStream();
152158

153-
index += count;
154-
} while (index < bytes.Length);
159+
total += count;
160+
} while (total < bytes.Length);
155161

156162
return bytes;
157163
}
@@ -171,22 +177,22 @@ static async ValueTask<byte[]> ReadAllBytesUnknownLengthImplAsync(Stream stream,
171177
.ConfigureAwait(false);
172178

173179
if (count == 0)
174-
{
175-
var result = bytes.AsSpan(0, total).ToArray();
176-
ArrayPool<byte>.Shared.Return(bytes);
177-
return result;
178-
}
180+
break;
179181

180182
total += count;
181183
}
182184

185+
var result = bytes.AsSpan(0, total).ToArray();
186+
ArrayPool<byte>.Shared.Return(bytes);
187+
return result;
188+
183189
static byte[] ResizeBuffer(byte[] oldArray)
184190
{
185-
var length = (uint)oldArray.Length * 2;
186-
if (length > (uint)Array.MaxLength)
187-
length = (uint)Math.Max(Array.MaxLength, oldArray.Length + 1);
191+
var length = oldArray.Length * 2;
192+
if ((uint)length > (uint)Array.MaxLength)
193+
length = Math.Max(Array.MaxLength, oldArray.Length + 1);
188194

189-
var newArray = ArrayPool<byte>.Shared.Rent((int)length);
195+
var newArray = ArrayPool<byte>.Shared.Rent(length);
190196
oldArray.AsSpan().TryCopyTo(newArray);
191197

192198
ArrayPool<byte>.Shared.Return(oldArray);

0 commit comments

Comments
 (0)