async methods in File

This commit is contained in:
Timerix 2025-03-22 20:53:16 +05:00
parent f2cdfc86b7
commit cfa26ce807

View File

@ -60,25 +60,55 @@ public static class File
return output;
}
public static string ReadAllText(IOPath file) => ReadAllBytes(file).BytesToString(StringConverter.UTF8);
public static async Task<byte[]> ReadAllBytesAsync(IOPath file)
{
using System.IO.FileStream stream = OpenRead(file);
int size = GetSize(file).ToInt();
byte[] output = new byte[size];
if (await stream.ReadAsync(output, 0, size).ConfigureAwait(false) < size)
throw new Exception("can't read all bytes");
return output;
}
public static string ReadAllText(IOPath file) =>
ReadAllBytes(file).BytesToString(StringConverter.UTF8);
public static async Task<string> ReadAllTextAsync(IOPath file) =>
(await ReadAllBytesAsync(file)).BytesToString(StringConverter.UTF8);
public static void WriteAllBytes(IOPath file, byte[] content)
{
using System.IO.FileStream stream = OpenWrite(file);
stream.Write(content, 0, content.Length);
}
public static async Task WriteAllBytesAsync(IOPath file, byte[] content)
{
using System.IO.FileStream stream = OpenWrite(file);
await stream.WriteAsync(content, 0, content.Length);
}
public static void WriteAllText(IOPath file, string content) =>
WriteAllBytes(file, content.ToBytes(StringConverter.UTF8));
public static async Task WriteAllText(IOPath file, string content) =>
await WriteAllBytesAsync(file, content.ToBytes(StringConverter.UTF8));
public static async Task WriteAllTextAsync(IOPath file, string content) =>
await WriteAllBytesAsync(file, content.ToBytes(StringConverter.UTF8));
public static void AppendAllBytes(IOPath file, byte[] content)
{
using System.IO.FileStream stream = OpenAppend(file);
stream.Write(content, 0, content.Length);
}
public static async Task AppendAllBytesAsync(IOPath file, byte[] content)
{
using System.IO.FileStream stream = OpenAppend(file);
await stream.WriteAsync(content, 0, content.Length);
}
public static void AppendAllText(IOPath file, string content) =>
AppendAllBytes(file, content.ToBytes(StringConverter.UTF8));
public static async Task AppendAllTextAsync(IOPath file, string content) =>
await AppendAllBytesAsync(file, content.ToBytes(StringConverter.UTF8));
public static System.IO.FileStream OpenRead(IOPath file)
{