/* This file is part of SevenZipSharp. SevenZipSharp is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. SevenZipSharp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with SevenZipSharp. If not, see . */ using System.IO; namespace SevenZip.Sdk.Buffer { /// /// Implements the input buffer work /// internal class InBuffer { private readonly byte[] m_Buffer; private readonly uint m_BufferSize; private uint m_Limit; private uint m_Pos; private ulong m_ProcessedSize; private Stream m_Stream; private bool m_StreamWasExhausted; /// /// Initializes the input buffer /// /// private InBuffer(uint bufferSize) { m_Buffer = new byte[bufferSize]; m_BufferSize = bufferSize; } /// /// Initializes the class /// /// private void Init(Stream stream) { m_Stream = stream; m_ProcessedSize = 0; m_Limit = 0; m_Pos = 0; m_StreamWasExhausted = false; } /// /// Reads the whole block /// /// private bool ReadBlock() { if (m_StreamWasExhausted) return false; m_ProcessedSize += m_Pos; int aNumProcessedBytes = m_Stream.Read(m_Buffer, 0, (int) m_BufferSize); m_Pos = 0; m_Limit = (uint) aNumProcessedBytes; m_StreamWasExhausted = (aNumProcessedBytes == 0); return (!m_StreamWasExhausted); } /// /// Releases the stream /// private void ReleaseStream() { // m_Stream.Close(); m_Stream = null; } /// /// Reads the byte to check it /// /// /// private bool ReadByte(out byte b) { b = 0; if (m_Pos >= m_Limit) if (!ReadBlock()) return false; b = m_Buffer[m_Pos++]; return true; } /// /// Reads the next byte /// /// private byte ReadByte() { // return (byte)m_Stream.ReadByte(); if (m_Pos >= m_Limit) if (!ReadBlock()) return 0xFF; return m_Buffer[m_Pos++]; } /// /// Gets processed size /// /// private ulong GetProcessedSize() { return m_ProcessedSize + m_Pos; } } }