From a533235c1bec27c626f35a7171f209fb9bdd462b Mon Sep 17 00:00:00 2001 From: morkt Date: Sat, 8 Oct 2016 05:15:45 +0400 Subject: [PATCH] (LimitStream): moved to CommonStreams. --- ArcFormats/CommonStreams.cs | 44 ++++++++++++++++++++++++++++++++ ArcFormats/Primel/Compression.cs | 41 ----------------------------- 2 files changed, 44 insertions(+), 41 deletions(-) diff --git a/ArcFormats/CommonStreams.cs b/ArcFormats/CommonStreams.cs index 57f094ba..fa0547bf 100644 --- a/ArcFormats/CommonStreams.cs +++ b/ArcFormats/CommonStreams.cs @@ -249,6 +249,50 @@ namespace GameRes.Formats } } + /// + /// Limits underlying stream to the first N bytes. + /// + public class LimitStream : InputProxyStream + { + bool m_can_seek; + long m_position; + long m_last; + + public LimitStream (Stream input, long last, bool leave_open = false) : base (input, leave_open) + { + m_can_seek = input.CanSeek; + m_position = 0; + m_last = last; + } + + public override bool CanSeek { get { return m_can_seek; } } + public override long Length { get { return m_last; } } + + public override int Read (byte[] buffer, int offset, int count) + { + if (m_can_seek) + m_position = Position; + if (m_position >= m_last) + return 0; + count = (int)Math.Min (count, m_last - m_position); + int read = BaseStream.Read (buffer, offset, count); + m_position += read; + return read; + } + + public override int ReadByte () + { + if (m_can_seek) + m_position = Position; + if (m_position >= m_last) + return -1; + int b = BaseStream.ReadByte(); + if (-1 != b) + ++m_position; + return b; + } + } + /// /// Lazily evaluated wrapper around non-seekable stream. /// diff --git a/ArcFormats/Primel/Compression.cs b/ArcFormats/Primel/Compression.cs index 48f48b6e..033dc7a9 100644 --- a/ArcFormats/Primel/Compression.cs +++ b/ArcFormats/Primel/Compression.cs @@ -354,45 +354,4 @@ namespace GameRes.Formats.Primel } } } - - internal class LimitStream : InputProxyStream - { - bool m_can_seek; - long m_position; - long m_last; - - public LimitStream (Stream input, long last, bool leave_open = false) : base (input, leave_open) - { - m_can_seek = input.CanSeek; - m_position = 0; - m_last = last; - } - - public override bool CanSeek { get { return m_can_seek; } } - public override long Length { get { return m_last; } } - - public override int Read (byte[] buffer, int offset, int count) - { - if (m_can_seek) - m_position = Position; - if (m_position >= m_last) - return 0; - count = (int)Math.Min (count, m_last - m_position); - int read = BaseStream.Read (buffer, offset, count); - m_position += read; - return read; - } - - public override int ReadByte () - { - if (m_can_seek) - m_position = Position; - if (m_position >= m_last) - return -1; - int b = BaseStream.ReadByte(); - if (-1 != b) - ++m_position; - return b; - } - } }