- tweak: loading popup thinks
* - tweak: change loading handle logic * - tweak: beautify loading thinks * - fix: speed thinks while downloading
This commit is contained in:
@@ -1,21 +1,37 @@
|
||||
using System.Buffers;
|
||||
using Nebula.Shared.Models;
|
||||
|
||||
namespace Nebula.Shared.Utils;
|
||||
|
||||
public static class StreamHelper
|
||||
{
|
||||
public static async ValueTask<byte[]> ReadExactAsync(this Stream stream, int amount, CancellationToken? cancel)
|
||||
public static void CopyTo(this Stream input, Stream output, ILoadingHandler loadingHandler)
|
||||
{
|
||||
const int bufferSize = 81920;
|
||||
var buffer = new byte[bufferSize];
|
||||
|
||||
int bytesRead;
|
||||
while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
|
||||
{
|
||||
output.Write(buffer, 0, bytesRead);
|
||||
loadingHandler.AppendResolvedJob(bytesRead);
|
||||
}
|
||||
}
|
||||
|
||||
public static async ValueTask<byte[]> ReadExactAsync(this Stream stream, int amount, CancellationToken cancel = default)
|
||||
{
|
||||
var data = new byte[amount];
|
||||
await ReadExactAsync(stream, data, cancel);
|
||||
return data;
|
||||
}
|
||||
|
||||
public static async ValueTask ReadExactAsync(this Stream stream, Memory<byte> into, CancellationToken? cancel)
|
||||
public static async ValueTask ReadExactAsync(this Stream stream, Memory<byte> into, CancellationToken cancel = default, ILoadingHandler? loadingHandler = null)
|
||||
{
|
||||
while (into.Length > 0)
|
||||
{
|
||||
var read = await stream.ReadAsync(into);
|
||||
var read = await stream.ReadAsync(into, cancel);
|
||||
|
||||
loadingHandler?.AppendResolvedJob(read);
|
||||
|
||||
// Check EOF.
|
||||
if (read == 0)
|
||||
@@ -24,31 +40,4 @@ public static class StreamHelper
|
||||
into = into[read..];
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task CopyAmountToAsync(
|
||||
this Stream stream,
|
||||
Stream to,
|
||||
int amount,
|
||||
int bufferSize,
|
||||
CancellationToken cancel)
|
||||
{
|
||||
var buffer = ArrayPool<byte>.Shared.Rent(bufferSize);
|
||||
|
||||
while (amount > 0)
|
||||
{
|
||||
Memory<byte> readInto = buffer;
|
||||
if (amount < readInto.Length)
|
||||
readInto = readInto[..amount];
|
||||
|
||||
var read = await stream.ReadAsync(readInto, cancel);
|
||||
if (read == 0)
|
||||
throw new EndOfStreamException();
|
||||
|
||||
amount -= read;
|
||||
|
||||
readInto = readInto[..read];
|
||||
|
||||
await to.WriteAsync(readInto, cancel);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user