Classes derived from Stream override ReadByte.
The Stream.ReadByte method is implemented by allocating a 1 byte buffer and calling into Read(byte[], int, int). Override ReadByte in derived classes to avoid needing to allocate this small temp buffer. Also, fix some bugs in the stream implementations. Remove Write capability from MergedStream that didn't make sense. Add guards into SegmentStream to ensure reads and writes belonged to the segment - otherwise a reader or writer could access regions of the base stream that were outside the intended segment.
This commit is contained in:
@@ -48,9 +48,25 @@ namespace OpenRA.Primitives
|
||||
|
||||
public sealed override long Seek(long offset, SeekOrigin origin) { throw new NotSupportedException(); }
|
||||
public sealed override void SetLength(long value) { throw new NotSupportedException(); }
|
||||
public sealed override void WriteByte(byte value) { throw new NotSupportedException(); }
|
||||
public sealed override void Write(byte[] buffer, int offset, int count) { throw new NotSupportedException(); }
|
||||
public sealed override void Flush() { throw new NotSupportedException(); }
|
||||
|
||||
public sealed override int ReadByte()
|
||||
{
|
||||
if (data.Count > 0)
|
||||
return data.Dequeue();
|
||||
|
||||
while (!baseStreamEmpty)
|
||||
{
|
||||
baseStreamEmpty = BufferData(baseStream, data);
|
||||
if (data.Count > 0)
|
||||
return data.Dequeue();
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
public sealed override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
var copied = 0;
|
||||
@@ -66,7 +82,8 @@ namespace OpenRA.Primitives
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads data into a buffer, which will be used to satisfy <see cref="Read(byte[], int, int)"/> calls.
|
||||
/// Reads data into a buffer, which will be used to satisfy <see cref="ReadByte()"/> and
|
||||
/// <see cref="Read(byte[], int, int)"/> calls.
|
||||
/// </summary>
|
||||
/// <param name="baseStream">The source stream from which bytes should be read.</param>
|
||||
/// <param name="data">The queue where bytes should be enqueued. Do not dequeue from this buffer.</param>
|
||||
|
||||
Reference in New Issue
Block a user