46 lines
885 B
C#
46 lines
885 B
C#
using System.IO;
|
|
using AMWD.Common.Packing.Tar.Interfaces;
|
|
|
|
namespace AMWD.Common.Packing.Tar.Utils
|
|
{
|
|
internal class DataWriter : IArchiveDataWriter
|
|
{
|
|
private readonly long _size;
|
|
private long _remainingBytes;
|
|
private readonly Stream _stream;
|
|
|
|
public DataWriter(Stream data, long dataSizeInBytes)
|
|
{
|
|
_size = dataSizeInBytes;
|
|
_remainingBytes = _size;
|
|
_stream = data;
|
|
}
|
|
|
|
public bool CanWrite { get; private set; } = true;
|
|
|
|
public int Write(byte[] buffer, int count)
|
|
{
|
|
if (_remainingBytes == 0)
|
|
{
|
|
CanWrite = false;
|
|
return -1;
|
|
}
|
|
|
|
int bytesToWrite;
|
|
if (_remainingBytes - count < 0)
|
|
{
|
|
bytesToWrite = (int)_remainingBytes;
|
|
}
|
|
else
|
|
{
|
|
bytesToWrite = count;
|
|
}
|
|
|
|
_stream.Write(buffer, 0, bytesToWrite);
|
|
_remainingBytes -= bytesToWrite;
|
|
|
|
return bytesToWrite;
|
|
}
|
|
}
|
|
}
|