diff --git a/ArcFormats/CommonStreams.cs b/ArcFormats/CommonStreams.cs index dd824349..4e62215b 100644 --- a/ArcFormats/CommonStreams.cs +++ b/ArcFormats/CommonStreams.cs @@ -91,45 +91,35 @@ namespace GameRes.Formats } } - public class PrefixStream : Stream + public class PrefixStream : ProxyStream { byte[] m_header; - Stream m_stream; long m_position = 0; - bool m_should_dispose; public PrefixStream (byte[] header, Stream main, bool leave_open = false) + : base (main, leave_open) { m_header = header; - m_stream = main; - m_should_dispose = !leave_open; } - public override bool CanRead { get { return m_stream.CanRead; } } - public override bool CanSeek { get { return m_stream.CanSeek; } } public override bool CanWrite { get { return false; } } - public override long Length { get { return m_stream.Length + m_header.Length; } } + public override long Length { get { return BaseStream.Length + m_header.Length; } } public override long Position { get { return m_position; } set { - if (!m_stream.CanSeek) + if (!BaseStream.CanSeek) throw new NotSupportedException ("Underlying stream does not support Stream.Position property"); m_position = Math.Max (value, 0); if (m_position > m_header.Length) { - long stream_pos = m_stream.Seek (m_position - m_header.Length, SeekOrigin.Begin); + long stream_pos = BaseStream.Seek (m_position - m_header.Length, SeekOrigin.Begin); m_position = m_header.Length + stream_pos; } } } - public override void Flush() - { - m_stream.Flush(); - } - public override long Seek (long offset, SeekOrigin origin) { if (SeekOrigin.Begin == origin) @@ -156,9 +146,9 @@ namespace GameRes.Formats } if (count > 0) { - if (m_header.Length == m_position && m_stream.CanSeek) - m_stream.Position = 0; - int stream_read = m_stream.Read (buffer, offset, count); + if (m_header.Length == m_position && BaseStream.CanSeek) + BaseStream.Position = 0; + int stream_read = BaseStream.Read (buffer, offset, count); m_position += stream_read; read += stream_read; } @@ -169,9 +159,9 @@ namespace GameRes.Formats { if (m_position < m_header.Length) return m_header[m_position++]; - if (m_position == m_header.Length && m_stream.CanSeek) - m_stream.Position = 0; - int b = m_stream.ReadByte(); + if (m_position == m_header.Length && BaseStream.CanSeek) + BaseStream.Position = 0; + int b = BaseStream.ReadByte(); if (-1 != b) m_position++; return b; @@ -191,38 +181,23 @@ namespace GameRes.Formats { throw new NotSupportedException ("PrefixStream.WriteByte method is not supported"); } - - bool disposed = false; - protected override void Dispose (bool disposing) - { - if (!disposed) - { - if (m_should_dispose && disposing) - m_stream.Dispose(); - disposed = true; - base.Dispose (disposing); - } - } } /// /// Represents a region within existing stream. /// Underlying stream should allow seeking (CanSeek == true). /// - public class StreamRegion : Stream + public class StreamRegion : ProxyStream { - private Stream m_stream; private long m_begin; private long m_end; - private bool m_should_dispose; public StreamRegion (Stream main, long offset, long length, bool leave_open = false) + : base (main, leave_open) { - m_stream = main; m_begin = offset; - m_end = Math.Min (offset + length, m_stream.Length); - m_stream.Position = m_begin; - m_should_dispose = !leave_open; + m_end = Math.Min (offset + length, BaseStream.Length); + BaseStream.Position = m_begin; } public StreamRegion (Stream main, long offset, bool leave_open = false) @@ -230,19 +205,13 @@ namespace GameRes.Formats { } - public override bool CanRead { get { return m_stream.CanRead; } } public override bool CanSeek { get { return true; } } public override bool CanWrite { get { return false; } } public override long Length { get { return m_end - m_begin; } } public override long Position { - get { return m_stream.Position - m_begin; } - set { m_stream.Position = Math.Max (m_begin + value, m_begin); } - } - - public override void Flush() - { - m_stream.Flush(); + get { return BaseStream.Position - m_begin; } + set { BaseStream.Position = Math.Max (m_begin + value, m_begin); } } public override long Seek (long offset, SeekOrigin origin) @@ -250,29 +219,29 @@ namespace GameRes.Formats if (SeekOrigin.Begin == origin) offset += m_begin; else if (SeekOrigin.Current == origin) - offset += m_stream.Position; + offset += BaseStream.Position; else offset += m_end; offset = Math.Max (offset, m_begin); - m_stream.Position = offset; + BaseStream.Position = offset; return offset - m_begin; } public override int Read (byte[] buffer, int offset, int count) { int read = 0; - long available = m_end - m_stream.Position; + long available = m_end - BaseStream.Position; if (available > 0) { - read = m_stream.Read (buffer, offset, (int)Math.Min (count, available)); + read = BaseStream.Read (buffer, offset, (int)Math.Min (count, available)); } return read; } public override int ReadByte () { - if (m_stream.Position < m_end) - return m_stream.ReadByte(); + if (BaseStream.Position < m_end) + return BaseStream.ReadByte(); else return -1; } @@ -291,18 +260,6 @@ namespace GameRes.Formats { throw new NotSupportedException ("StreamRegion.WriteByte method is not supported"); } - - bool m_disposed = false; - protected override void Dispose (bool disposing) - { - if (!m_disposed) - { - if (m_should_dispose && disposing) - m_stream.Dispose(); - m_disposed = true; - base.Dispose (disposing); - } - } } ///