Files
duplicati/Duplicati/Library/Backend/Telegram/StreamReadHelper.cs
T

48 lines
1.6 KiB
C#
Raw Normal View History

2020-10-05 00:36:47 +04:00
using System.IO;
using Duplicati.Library.Utility;
namespace Duplicati.Library.Backend
{
/// <summary>
2020-10-05 00:36:47 +04:00
/// Private helper class to fix a bug with the StreamReader
/// </summary>
2020-10-05 00:36:47 +04:00
internal class StreamReadHelper : OverrideableStream
{
/// <summary>
2020-10-05 00:36:47 +04:00
/// Once the stream has returned 0 as the read count it is disposed,
/// and subsequent read requests will throw an ObjectDisposedException
/// </summary>
2020-09-27 16:58:57 +04:00
private bool m_empty;
/// <summary>
2020-10-05 00:36:47 +04:00
/// Basic initialization, just pass the stream to the super class
/// </summary>
/// <param name="stream"></param>
2020-10-05 00:36:47 +04:00
public StreamReadHelper(Stream stream) : base(stream)
{ }
/// <summary>
2020-10-05 00:36:47 +04:00
/// Override the read function to make sure that we only return less than the requested amount of data if the stream is exhausted
/// </summary>
/// <param name="buffer">The buffer to place data in</param>
/// <param name="offset">The offset into the buffer to start at</param>
/// <param name="count">The number of bytes to read</param>
/// <returns>The number of bytes read</returns>
public override int Read(byte[] buffer, int offset, int count)
{
2020-10-05 00:36:47 +04:00
var readCount = 0;
int a;
while (!m_empty && count > 0)
{
a = base.Read(buffer, offset, count);
readCount += a;
count -= a;
offset += a;
m_empty = a == 0;
}
return readCount;
}
}
}