(SeekableStream): use supplied buffer as intermediate.

This commit is contained in:
morkt 2016-03-12 12:45:13 +04:00
parent 236d33f4e7
commit 0600805f06

View File

@ -116,7 +116,7 @@ namespace GameRes.Formats.Cri
public override int Read (byte[] buffer, int offset, int count) public override int Read (byte[] buffer, int offset, int count)
{ {
int total_read = 0; int read, total_read = 0;
if (m_source_depleted) if (m_source_depleted)
{ {
m_buffer.Position = m_read_pos; m_buffer.Position = m_read_pos;
@ -135,21 +135,22 @@ namespace GameRes.Formats.Cri
return total_read; return total_read;
offset += total_read; offset += total_read;
} }
else else if (count > 0)
{ {
m_buffer.Seek (0, SeekOrigin.End); m_buffer.Seek (0, SeekOrigin.End);
while (m_read_pos > m_buffer.Length) while (m_read_pos > m_buffer.Length)
{ {
int b = m_source.ReadByte(); int available = (int)Math.Min (m_read_pos - m_buffer.Length, count);
if (-1 == b) read = m_source.Read (buffer, offset, available);
if (0 == read)
{ {
m_source_depleted = true; m_source_depleted = true;
return 0; return 0;
} }
m_buffer.WriteByte ((byte)b); m_buffer.Write (buffer, offset, read);
} }
} }
int read = m_source.Read (buffer, offset, count); read = m_source.Read (buffer, offset, count);
m_read_pos += read; m_read_pos += read;
m_buffer.Write (buffer, offset, read); m_buffer.Write (buffer, offset, read);
return total_read + read; return total_read + read;