(LimitStream): added 'Fill' option.

fills stream with zeroes if underlying stream has less length than
requested.
This commit is contained in:
morkt 2018-10-30 23:48:44 +04:00
parent d35f790994
commit 64e1ae3946

View File

@ -249,6 +249,12 @@ namespace GameRes.Formats
}
}
public enum StreamOption
{
None,
Fill,
}
/// <summary>
/// Limits underlying stream to the first N bytes.
/// </summary>
@ -257,6 +263,7 @@ namespace GameRes.Formats
bool m_can_seek;
long m_position;
long m_last;
bool m_fill;
public LimitStream (Stream input, long last, bool leave_open = false) : base (input, leave_open)
{
@ -265,6 +272,20 @@ namespace GameRes.Formats
m_last = last;
}
public LimitStream (Stream input, long last, StreamOption option, bool leave_open = false)
: this (input, last, leave_open)
{
if (StreamOption.Fill == option)
{
if (m_can_seek && input.Length < m_last)
{
input.Position = m_position;
m_can_seek = false;
}
m_fill = true;
}
}
public override bool CanSeek { get { return m_can_seek; } }
public override long Length { get { return m_last; } }
@ -276,6 +297,13 @@ namespace GameRes.Formats
return 0;
count = (int)Math.Min (count, m_last - m_position);
int read = BaseStream.Read (buffer, offset, count);
if (m_fill)
{
while (read < count)
{
buffer[read++] = 0;
}
}
m_position += read;
return read;
}