mirror of
https://github.com/crskycode/GARbro.git
synced 2024-12-25 12:24:12 +08:00
(PrefixStream, StreamRegion): derive from ProxyStream.
This commit is contained in:
parent
0c426d723f
commit
8732a8a837
@ -91,45 +91,35 @@ namespace GameRes.Formats
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class PrefixStream : Stream
|
public class PrefixStream : ProxyStream
|
||||||
{
|
{
|
||||||
byte[] m_header;
|
byte[] m_header;
|
||||||
Stream m_stream;
|
|
||||||
long m_position = 0;
|
long m_position = 0;
|
||||||
bool m_should_dispose;
|
|
||||||
|
|
||||||
public PrefixStream (byte[] header, Stream main, bool leave_open = false)
|
public PrefixStream (byte[] header, Stream main, bool leave_open = false)
|
||||||
|
: base (main, leave_open)
|
||||||
{
|
{
|
||||||
m_header = header;
|
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 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
|
public override long Position
|
||||||
{
|
{
|
||||||
get { return m_position; }
|
get { return m_position; }
|
||||||
set
|
set
|
||||||
{
|
{
|
||||||
if (!m_stream.CanSeek)
|
if (!BaseStream.CanSeek)
|
||||||
throw new NotSupportedException ("Underlying stream does not support Stream.Position property");
|
throw new NotSupportedException ("Underlying stream does not support Stream.Position property");
|
||||||
m_position = Math.Max (value, 0);
|
m_position = Math.Max (value, 0);
|
||||||
if (m_position > m_header.Length)
|
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;
|
m_position = m_header.Length + stream_pos;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Flush()
|
|
||||||
{
|
|
||||||
m_stream.Flush();
|
|
||||||
}
|
|
||||||
|
|
||||||
public override long Seek (long offset, SeekOrigin origin)
|
public override long Seek (long offset, SeekOrigin origin)
|
||||||
{
|
{
|
||||||
if (SeekOrigin.Begin == origin)
|
if (SeekOrigin.Begin == origin)
|
||||||
@ -156,9 +146,9 @@ namespace GameRes.Formats
|
|||||||
}
|
}
|
||||||
if (count > 0)
|
if (count > 0)
|
||||||
{
|
{
|
||||||
if (m_header.Length == m_position && m_stream.CanSeek)
|
if (m_header.Length == m_position && BaseStream.CanSeek)
|
||||||
m_stream.Position = 0;
|
BaseStream.Position = 0;
|
||||||
int stream_read = m_stream.Read (buffer, offset, count);
|
int stream_read = BaseStream.Read (buffer, offset, count);
|
||||||
m_position += stream_read;
|
m_position += stream_read;
|
||||||
read += stream_read;
|
read += stream_read;
|
||||||
}
|
}
|
||||||
@ -169,9 +159,9 @@ namespace GameRes.Formats
|
|||||||
{
|
{
|
||||||
if (m_position < m_header.Length)
|
if (m_position < m_header.Length)
|
||||||
return m_header[m_position++];
|
return m_header[m_position++];
|
||||||
if (m_position == m_header.Length && m_stream.CanSeek)
|
if (m_position == m_header.Length && BaseStream.CanSeek)
|
||||||
m_stream.Position = 0;
|
BaseStream.Position = 0;
|
||||||
int b = m_stream.ReadByte();
|
int b = BaseStream.ReadByte();
|
||||||
if (-1 != b)
|
if (-1 != b)
|
||||||
m_position++;
|
m_position++;
|
||||||
return b;
|
return b;
|
||||||
@ -191,38 +181,23 @@ namespace GameRes.Formats
|
|||||||
{
|
{
|
||||||
throw new NotSupportedException ("PrefixStream.WriteByte method is not supported");
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Represents a region within existing stream.
|
/// Represents a region within existing stream.
|
||||||
/// Underlying stream should allow seeking (CanSeek == true).
|
/// Underlying stream should allow seeking (CanSeek == true).
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class StreamRegion : Stream
|
public class StreamRegion : ProxyStream
|
||||||
{
|
{
|
||||||
private Stream m_stream;
|
|
||||||
private long m_begin;
|
private long m_begin;
|
||||||
private long m_end;
|
private long m_end;
|
||||||
private bool m_should_dispose;
|
|
||||||
|
|
||||||
public StreamRegion (Stream main, long offset, long length, bool leave_open = false)
|
public StreamRegion (Stream main, long offset, long length, bool leave_open = false)
|
||||||
|
: base (main, leave_open)
|
||||||
{
|
{
|
||||||
m_stream = main;
|
|
||||||
m_begin = offset;
|
m_begin = offset;
|
||||||
m_end = Math.Min (offset + length, m_stream.Length);
|
m_end = Math.Min (offset + length, BaseStream.Length);
|
||||||
m_stream.Position = m_begin;
|
BaseStream.Position = m_begin;
|
||||||
m_should_dispose = !leave_open;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public StreamRegion (Stream main, long offset, bool leave_open = false)
|
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 CanSeek { get { return true; } }
|
||||||
public override bool CanWrite { get { return false; } }
|
public override bool CanWrite { get { return false; } }
|
||||||
public override long Length { get { return m_end - m_begin; } }
|
public override long Length { get { return m_end - m_begin; } }
|
||||||
public override long Position
|
public override long Position
|
||||||
{
|
{
|
||||||
get { return m_stream.Position - m_begin; }
|
get { return BaseStream.Position - m_begin; }
|
||||||
set { m_stream.Position = Math.Max (m_begin + value, m_begin); }
|
set { BaseStream.Position = Math.Max (m_begin + value, m_begin); }
|
||||||
}
|
|
||||||
|
|
||||||
public override void Flush()
|
|
||||||
{
|
|
||||||
m_stream.Flush();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override long Seek (long offset, SeekOrigin origin)
|
public override long Seek (long offset, SeekOrigin origin)
|
||||||
@ -250,29 +219,29 @@ namespace GameRes.Formats
|
|||||||
if (SeekOrigin.Begin == origin)
|
if (SeekOrigin.Begin == origin)
|
||||||
offset += m_begin;
|
offset += m_begin;
|
||||||
else if (SeekOrigin.Current == origin)
|
else if (SeekOrigin.Current == origin)
|
||||||
offset += m_stream.Position;
|
offset += BaseStream.Position;
|
||||||
else
|
else
|
||||||
offset += m_end;
|
offset += m_end;
|
||||||
offset = Math.Max (offset, m_begin);
|
offset = Math.Max (offset, m_begin);
|
||||||
m_stream.Position = offset;
|
BaseStream.Position = offset;
|
||||||
return offset - m_begin;
|
return offset - m_begin;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override int Read (byte[] buffer, int offset, int count)
|
public override int Read (byte[] buffer, int offset, int count)
|
||||||
{
|
{
|
||||||
int read = 0;
|
int read = 0;
|
||||||
long available = m_end - m_stream.Position;
|
long available = m_end - BaseStream.Position;
|
||||||
if (available > 0)
|
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;
|
return read;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override int ReadByte ()
|
public override int ReadByte ()
|
||||||
{
|
{
|
||||||
if (m_stream.Position < m_end)
|
if (BaseStream.Position < m_end)
|
||||||
return m_stream.ReadByte();
|
return BaseStream.ReadByte();
|
||||||
else
|
else
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -291,18 +260,6 @@ namespace GameRes.Formats
|
|||||||
{
|
{
|
||||||
throw new NotSupportedException ("StreamRegion.WriteByte method is not supported");
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user