Innovenergy_trunk/csharp/Lib/Channels/V2/Pipes/ChannelPipeSource.cs

44 lines
1.1 KiB
C#

using CliWrap;
namespace InnovEnergy.Lib.Channels.V2.Pipes;
public class ChannelPipeSource : PipeSource
{
private readonly IStreamChannel _StreamChannel;
private Byte[]? _Buffer;
private ChannelPipeSource(IStreamChannel streamChannel)
{
_StreamChannel = streamChannel;
}
public static ChannelPipeSource FromStreamChannel(IStreamChannel streamChannel)
{
return new ChannelPipeSource(streamChannel);
}
public override async Task CopyToAsync(Stream destination, CancellationToken cancellationToken = new CancellationToken())
{
var rx = await _StreamChannel.Read();
if (rx is Byte[] ba)
{
await destination.WriteAsync(ba, 0, ba.Length, cancellationToken);
return;
}
if (_Buffer is null || _Buffer.Length < rx.Count )
{
_Buffer = rx.ToArray();
}
else
{
var i = 0;
foreach (var b in _Buffer)
_Buffer[i++] = b;
}
await destination.WriteAsync(_Buffer, 0, rx.Count, cancellationToken);
}
}