-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileInfoExtensions.cs
More file actions
332 lines (283 loc) · 12.1 KB
/
FileInfoExtensions.cs
File metadata and controls
332 lines (283 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
using System.Buffers;
using System.Text;
namespace Ramstack.FileProviders;
/// <summary>
/// Provides extension methods for the <see cref="IFileInfo"/>.
/// </summary>
public static class FileInfoExtensions
{
/// <summary>
/// Returns file contents as a read-only stream.
/// </summary>
/// <param name="file">The <see cref="IFileInfo"/>.</param>
/// <returns>
/// The file contents as a read-only stream.
/// </returns>
public static Stream OpenRead(this IFileInfo file) =>
file.CreateReadStream();
/// <summary>
/// Returns a <see cref="StreamReader"/> with the specified character encoding that reads from the text file.
/// </summary>
/// <param name="file">The <see cref="IFileInfo"/>.</param>
/// <param name="encoding">The character encoding to use.</param>
/// <returns>
/// A new <see cref="StreamReader"/> with the specified character encoding.
/// </returns>
public static StreamReader OpenText(this IFileInfo file, Encoding? encoding) =>
new StreamReader(file.CreateReadStream(), encoding, detectEncodingFromByteOrderMarks: true, bufferSize: -1, leaveOpen: false);
/// <summary>
/// Reads all the text in the file with the specified encoding.
/// </summary>
/// <param name="file">The file from which to read the entire text content.</param>
/// <param name="encoding">The encoding applied to the contents.</param>
/// <returns>
/// A string containing all text in the file.
/// </returns>
public static string ReadAllText(this IFileInfo file, Encoding? encoding = null)
{
using var reader = file.OpenText(encoding);
return reader.ReadToEnd();
}
/// <summary>
/// Reads all lines of the file with the specified encoding.
/// </summary>
/// <param name="file">The file to read from.</param>
/// <param name="encoding">The encoding applied to the contents.</param>
/// <returns>
/// A string array containing all lines of the file.
/// </returns>
public static string[] ReadAllLines(this IFileInfo file, Encoding? encoding = null)
{
var stream = file.OpenRead();
using var reader = new StreamReader(stream, encoding!);
var list = new List<string>();
while (reader.ReadLine() is { } line)
list.Add(line);
return list.ToArray();
}
/// <summary>
/// Reads the entire contents of the current file into a byte array.
/// </summary>
/// <param name="file">The file to read from.</param>
/// <returns>
/// A byte array containing the contents of the file.
/// </returns>
public static byte[] ReadAllBytes(this IFileInfo file)
{
// ReSharper disable once UseAwaitUsing
using var stream = file.OpenRead();
var length = GetStreamLength(stream);
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.
return length <= 0
? ReadAllBytesUnknownLengthImpl(stream)
: ReadAllBytesImpl(stream);
static byte[] ReadAllBytesImpl(Stream stream)
{
var bytes = new byte[stream.Length];
var index = 0;
do
{
var count = stream.Read(bytes.AsSpan(index));
if (count == 0)
Error_EndOfStream();
index += count;
}
while (index < bytes.Length);
return bytes;
}
static byte[] ReadAllBytesUnknownLengthImpl(Stream stream)
{
var bytes = ArrayPool<byte>.Shared.Rent(512);
var index = 0;
try
{
while (true)
{
if (index == bytes.Length)
bytes = ResizeBuffer(bytes);
var count = stream.Read(bytes.AsSpan(index));
if (count == 0)
return bytes.AsSpan(0, index).ToArray();
index += count;
}
}
finally
{
ArrayPool<byte>.Shared.Return(bytes);
}
}
}
/// <summary>
/// Asynchronously reads all the text in the current file.
/// </summary>
/// <param name="file">The file from which to read the entire text content.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask{TResult}"/> representing the asynchronous operation,
/// containing the full text from the current file.
/// </returns>
public static ValueTask<string> ReadAllTextAsync(this IFileInfo file, CancellationToken cancellationToken = default) =>
ReadAllTextAsync(file, null, cancellationToken);
/// <summary>
/// Asynchronously reads all the text in the current file with the specified encoding.
/// </summary>
/// <param name="file">The file from which to read the entire text content.</param>
/// <param name="encoding">The encoding applied to the contents.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask{TResult}"/> representing the asynchronous operation,
/// containing the full text from the current file.
/// </returns>
public static async ValueTask<string> ReadAllTextAsync(this IFileInfo file, Encoding? encoding, CancellationToken cancellationToken = default)
{
const int BufferSize = 4096;
var stream = file.OpenRead();
var reader = new StreamReader(stream, encoding ??= Encoding.UTF8);
var buffer = (char[]?)null;
try
{
cancellationToken.ThrowIfCancellationRequested();
buffer = ArrayPool<char>.Shared.Rent(encoding.GetMaxCharCount(BufferSize));
var sb = new StringBuilder();
while (true)
{
var count = await reader
.ReadAsync(new Memory<char>(buffer), cancellationToken)
.ConfigureAwait(false);
if (count == 0)
return sb.ToString();
sb.Append(buffer.AsSpan(0, count));
}
}
finally
{
reader.Dispose();
if (buffer is not null)
ArrayPool<char>.Shared.Return(buffer);
}
}
/// <summary>
/// Asynchronously reads all lines of the current file.
/// </summary>
/// <param name="file">The file to read from.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask{TResult}"/> representing the asynchronous operation,
/// 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);
/// <summary>
/// Asynchronously reads all lines of the current file with the specified encoding.
/// </summary>
/// <param name="file">The file to read from.</param>
/// <param name="encoding">The encoding applied to the contents.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask{TResult}"/> representing the asynchronous operation,
/// containing an array of all lines in the current file.
/// </returns>
public static async ValueTask<string[]> ReadAllLinesAsync(this IFileInfo file, Encoding? encoding, CancellationToken cancellationToken = default)
{
var stream = file.OpenRead();
using var reader = new StreamReader(stream, encoding!);
var list = new List<string>();
while (await reader.ReadLineAsync().ConfigureAwait(false) is { } line)
{
cancellationToken.ThrowIfCancellationRequested();
list.Add(line);
}
return list.ToArray();
}
/// <summary>
/// Asynchronously reads the entire contents of the current file into a byte array.
/// </summary>
/// <param name="file">The file to read from.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask{TResult}"/> representing the asynchronous operation,
/// containing an array of the file's bytes.
/// </returns>
public static async ValueTask<byte[]> ReadAllBytesAsync(this IFileInfo file, CancellationToken cancellationToken = default)
{
// ReSharper disable once UseAwaitUsing
using var stream = file.OpenRead();
var length = GetStreamLength(stream);
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);
return await task.ConfigureAwait(false);
static async ValueTask<byte[]> ReadAllBytesImplAsync(Stream stream, CancellationToken cancellationToken)
{
var bytes = new byte[stream.Length];
var index = 0;
do
{
var count = await stream.ReadAsync(bytes.AsMemory(index), cancellationToken).ConfigureAwait(false);
if (count == 0)
Error_EndOfStream();
index += count;
}
while (index < bytes.Length);
return bytes;
}
static async ValueTask<byte[]> ReadAllBytesUnknownLengthImplAsync(Stream stream, CancellationToken cancellationToken)
{
var bytes = ArrayPool<byte>.Shared.Rent(512);
var total = 0;
try
{
while (true)
{
if (total == bytes.Length)
bytes = ResizeBuffer(bytes);
var count = await stream
.ReadAsync(bytes.AsMemory(total), cancellationToken)
.ConfigureAwait(false);
if (count == 0)
return bytes.AsSpan(0, total).ToArray();
total += count;
}
}
finally
{
ArrayPool<byte>.Shared.Return(bytes);
}
}
}
private static byte[] ResizeBuffer(byte[] oldArray)
{
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(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();
}