1baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// InBuffer.cs
2baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
3baa3858d3f5d128a5c8466b700098109edcad5f2repo syncnamespace SevenZip.Buffer
4baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
5baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	public class InBuffer
6baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	{
7baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		byte[] m_Buffer;
8baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		uint m_Pos;
9baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		uint m_Limit;
10baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		uint m_BufferSize;
11baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		System.IO.Stream m_Stream;
12baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		bool m_StreamWasExhausted;
13baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		ulong m_ProcessedSize;
14baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
15baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public InBuffer(uint bufferSize)
16baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		{
17baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			m_Buffer = new byte[bufferSize];
18baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			m_BufferSize = bufferSize;
19baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		}
20baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
21baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public void Init(System.IO.Stream stream)
22baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		{
23baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			m_Stream = stream;
24baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			m_ProcessedSize = 0;
25baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			m_Limit = 0;
26baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			m_Pos = 0;
27baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			m_StreamWasExhausted = false;
28baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		}
29baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
30baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public bool ReadBlock()
31baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		{
32baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			if (m_StreamWasExhausted)
33baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				return false;
34baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			m_ProcessedSize += m_Pos;
35baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			int aNumProcessedBytes = m_Stream.Read(m_Buffer, 0, (int)m_BufferSize);
36baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			m_Pos = 0;
37baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			m_Limit = (uint)aNumProcessedBytes;
38baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			m_StreamWasExhausted = (aNumProcessedBytes == 0);
39baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			return (!m_StreamWasExhausted);
40baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		}
41baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
42baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
43baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public void ReleaseStream()
44baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		{
45baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			// m_Stream.Close();
46baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			m_Stream = null;
47baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		}
48baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
49baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public bool ReadByte(byte b) // check it
50baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		{
51baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			if (m_Pos >= m_Limit)
52baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				if (!ReadBlock())
53baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					return false;
54baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			b = m_Buffer[m_Pos++];
55baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			return true;
56baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		}
57baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
58baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public byte ReadByte()
59baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		{
60baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			// return (byte)m_Stream.ReadByte();
61baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			if (m_Pos >= m_Limit)
62baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				if (!ReadBlock())
63baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					return 0xFF;
64baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			return m_Buffer[m_Pos++];
65baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		}
66baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
67baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public ulong GetProcessedSize()
68baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		{
69baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			return m_ProcessedSize + m_Pos;
70baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		}
71baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	}
72baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
73