45 lines
1.2 KiB
C#
45 lines
1.2 KiB
C#
using System;
|
|
using System.IO;
|
|
using NixWslWrapper.Core.Interfaces;
|
|
|
|
namespace NixWslWrapper.Infrastructure.IO
|
|
{
|
|
public class ThreadedStreamCopier : IStreamCopier
|
|
{
|
|
private readonly int _bufferSize;
|
|
|
|
public ThreadedStreamCopier(int bufferSize = 8192)
|
|
{
|
|
if (bufferSize <= 0)
|
|
throw new ArgumentOutOfRangeException(nameof(bufferSize), "Buffer size must be positive.");
|
|
_bufferSize = bufferSize;
|
|
}
|
|
|
|
public void Copy(Stream input, Stream output)
|
|
{
|
|
byte[] buffer = new byte[_bufferSize];
|
|
int read;
|
|
try
|
|
{
|
|
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
|
|
{
|
|
output.Write(buffer, 0, read);
|
|
output.Flush();
|
|
}
|
|
}
|
|
catch (IOException)
|
|
{
|
|
// Clean exit when stream is closed or process terminates
|
|
}
|
|
catch (ObjectDisposedException)
|
|
{
|
|
// Clean exit when streams are disposed
|
|
}
|
|
catch (Exception)
|
|
{
|
|
// General catch to ensure thread doesn't crash the host process
|
|
}
|
|
}
|
|
}
|
|
}
|