(LimitStream): moved to CommonStreams.

This commit is contained in:
morkt 2016-10-08 05:15:45 +04:00
parent 6867da8a61
commit a533235c1b
2 changed files with 44 additions and 41 deletions

View File

@ -249,6 +249,50 @@ namespace GameRes.Formats
}
}
/// <summary>
/// Limits underlying stream to the first N bytes.
/// </summary>
public class LimitStream : InputProxyStream
{
bool m_can_seek;
long m_position;
long m_last;
public LimitStream (Stream input, long last, bool leave_open = false) : base (input, leave_open)
{
m_can_seek = input.CanSeek;
m_position = 0;
m_last = last;
}
public override bool CanSeek { get { return m_can_seek; } }
public override long Length { get { return m_last; } }
public override int Read (byte[] buffer, int offset, int count)
{
if (m_can_seek)
m_position = Position;
if (m_position >= m_last)
return 0;
count = (int)Math.Min (count, m_last - m_position);
int read = BaseStream.Read (buffer, offset, count);
m_position += read;
return read;
}
public override int ReadByte ()
{
if (m_can_seek)
m_position = Position;
if (m_position >= m_last)
return -1;
int b = BaseStream.ReadByte();
if (-1 != b)
++m_position;
return b;
}
}
/// <summary>
/// Lazily evaluated wrapper around non-seekable stream.
/// </summary>

View File

@ -354,45 +354,4 @@ namespace GameRes.Formats.Primel
}
}
}
internal class LimitStream : InputProxyStream
{
bool m_can_seek;
long m_position;
long m_last;
public LimitStream (Stream input, long last, bool leave_open = false) : base (input, leave_open)
{
m_can_seek = input.CanSeek;
m_position = 0;
m_last = last;
}
public override bool CanSeek { get { return m_can_seek; } }
public override long Length { get { return m_last; } }
public override int Read (byte[] buffer, int offset, int count)
{
if (m_can_seek)
m_position = Position;
if (m_position >= m_last)
return 0;
count = (int)Math.Min (count, m_last - m_position);
int read = BaseStream.Read (buffer, offset, count);
m_position += read;
return read;
}
public override int ReadByte ()
{
if (m_can_seek)
m_position = Position;
if (m_position >= m_last)
return -1;
int b = BaseStream.ReadByte();
if (-1 != b)
++m_position;
return b;
}
}
}