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:
@@ -59,8 +59,35 @@ namespace OpenRA.Primitives
|
||||
set { BaseStream.Position = BaseOffset + value; }
|
||||
}
|
||||
|
||||
public override int Read(byte[] buffer, int offset, int count) { return BaseStream.Read(buffer, offset, count); }
|
||||
public override void Write(byte[] buffer, int offset, int count) { BaseStream.Write(buffer, offset, count); }
|
||||
public override int ReadByte()
|
||||
{
|
||||
if (Position < Length)
|
||||
return BaseStream.ReadByte();
|
||||
return -1;
|
||||
}
|
||||
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
var remaining = Length - Position;
|
||||
if (remaining <= 0)
|
||||
return 0;
|
||||
return BaseStream.Read(buffer, offset, (int)Math.Min(remaining, count));
|
||||
}
|
||||
|
||||
public override void WriteByte(byte value)
|
||||
{
|
||||
if (Position < Length)
|
||||
BaseStream.WriteByte(value);
|
||||
throw new IOException("Attempted to write past the end of the stream.");
|
||||
}
|
||||
|
||||
public override void Write(byte[] buffer, int offset, int count)
|
||||
{
|
||||
if (Position + count >= Length)
|
||||
throw new IOException("Attempted to write past the end of the stream.");
|
||||
BaseStream.Write(buffer, offset, count);
|
||||
}
|
||||
|
||||
public override void Flush() { BaseStream.Flush(); }
|
||||
public override long Seek(long offset, SeekOrigin origin)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user