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

View File

@@ -48,9 +48,25 @@ namespace OpenRA.Primitives
public sealed override long Seek(long offset, SeekOrigin origin) { throw new NotSupportedException(); } 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 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 Write(byte[] buffer, int offset, int count) { throw new NotSupportedException(); }
public sealed override void Flush() { 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) public sealed override int Read(byte[] buffer, int offset, int count)
{ {
var copied = 0; var copied = 0;
@@ -66,7 +82,8 @@ namespace OpenRA.Primitives
} }
/// <summary> /// <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> /// </summary>
/// <param name="baseStream">The source stream from which bytes should be read.</param> /// <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> /// <param name="data">The queue where bytes should be enqueued. Do not dequeue from this buffer.</param>

View File

@@ -59,8 +59,35 @@ namespace OpenRA.Primitives
set { BaseStream.Position = BaseOffset + value; } set { BaseStream.Position = BaseOffset + value; }
} }
public override int Read(byte[] buffer, int offset, int count) { return BaseStream.Read(buffer, offset, count); } public override int ReadByte()
public override void Write(byte[] buffer, int offset, int count) { BaseStream.Write(buffer, offset, count); } {
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 void Flush() { BaseStream.Flush(); }
public override long Seek(long offset, SeekOrigin origin) public override long Seek(long offset, SeekOrigin origin)
{ {