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