InBuffer.cs 1.3 KB

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