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:
RoosterDragon
2020-10-18 15:28:33 +01:00
committed by abcdefg30
parent 466de89e17
commit 54c4a05062
3 changed files with 71 additions and 10 deletions

View File

@@ -9,6 +9,7 @@
*/
#endregion
using System;
using System.IO;
namespace OpenRA.Primitives
@@ -18,7 +19,7 @@ namespace OpenRA.Primitives
public Stream Stream1 { get; set; }
public Stream Stream2 { get; set; }
long VirtualLength { get; set; }
long VirtualLength { get; }
long position;
public MergedStream(Stream stream1, Stream stream2)
@@ -61,7 +62,21 @@ namespace OpenRA.Primitives
public override void SetLength(long value)
{
VirtualLength = value;
throw new NotSupportedException();
}
public override int ReadByte()
{
int value;
if (position >= Stream1.Length)
value = Stream2.ReadByte();
else
value = Stream1.ReadByte();
position++;
return value;
}
public override int Read(byte[] buffer, int offset, int count)
@@ -83,12 +98,14 @@ namespace OpenRA.Primitives
return bytesRead;
}
public override void WriteByte(byte value)
{
throw new NotSupportedException();
}
public override void Write(byte[] buffer, int offset, int count)
{
if (position >= Stream1.Length)
Stream2.Write(buffer, offset - (int)Stream1.Length, count);
else
Stream1.Write(buffer, offset, count);
throw new NotSupportedException();
}
public override bool CanRead
@@ -103,7 +120,7 @@ namespace OpenRA.Primitives
public override bool CanWrite
{
get { return Stream1.CanWrite && Stream2.CanWrite; }
get { return false; }
}
public override long Length