InBuffer.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* This file is part of SevenZipSharp.
  2. SevenZipSharp is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU Lesser General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. SevenZipSharp is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU Lesser General Public License for more details.
  10. You should have received a copy of the GNU Lesser General Public License
  11. along with SevenZipSharp. If not, see <http://www.gnu.org/licenses/>.
  12. */
  13. using System.IO;
  14. namespace SevenZip.Sdk.Buffer
  15. {
  16. /// <summary>
  17. /// Implements the input buffer work
  18. /// </summary>
  19. internal class InBuffer
  20. {
  21. private readonly byte[] m_Buffer;
  22. private readonly uint m_BufferSize;
  23. private uint m_Limit;
  24. private uint m_Pos;
  25. private ulong m_ProcessedSize;
  26. private Stream m_Stream;
  27. private bool m_StreamWasExhausted;
  28. /// <summary>
  29. /// Initializes the input buffer
  30. /// </summary>
  31. /// <param name="bufferSize"></param>
  32. private InBuffer(uint bufferSize)
  33. {
  34. m_Buffer = new byte[bufferSize];
  35. m_BufferSize = bufferSize;
  36. }
  37. /// <summary>
  38. /// Initializes the class
  39. /// </summary>
  40. /// <param name="stream"></param>
  41. private void Init(Stream stream)
  42. {
  43. m_Stream = stream;
  44. m_ProcessedSize = 0;
  45. m_Limit = 0;
  46. m_Pos = 0;
  47. m_StreamWasExhausted = false;
  48. }
  49. /// <summary>
  50. /// Reads the whole block
  51. /// </summary>
  52. /// <returns></returns>
  53. private bool ReadBlock()
  54. {
  55. if (m_StreamWasExhausted)
  56. return false;
  57. m_ProcessedSize += m_Pos;
  58. int aNumProcessedBytes = m_Stream.Read(m_Buffer, 0, (int) m_BufferSize);
  59. m_Pos = 0;
  60. m_Limit = (uint) aNumProcessedBytes;
  61. m_StreamWasExhausted = (aNumProcessedBytes == 0);
  62. return (!m_StreamWasExhausted);
  63. }
  64. /// <summary>
  65. /// Releases the stream
  66. /// </summary>
  67. private void ReleaseStream()
  68. {
  69. // m_Stream.Close();
  70. m_Stream = null;
  71. }
  72. /// <summary>
  73. /// Reads the byte to check it
  74. /// </summary>
  75. /// <param name="b"></param>
  76. /// <returns></returns>
  77. private bool ReadByte(out byte b)
  78. {
  79. b = 0;
  80. if (m_Pos >= m_Limit)
  81. if (!ReadBlock())
  82. return false;
  83. b = m_Buffer[m_Pos++];
  84. return true;
  85. }
  86. /// <summary>
  87. /// Reads the next byte
  88. /// </summary>
  89. /// <returns></returns>
  90. private byte ReadByte()
  91. {
  92. // return (byte)m_Stream.ReadByte();
  93. if (m_Pos >= m_Limit)
  94. if (!ReadBlock())
  95. return 0xFF;
  96. return m_Buffer[m_Pos++];
  97. }
  98. /// <summary>
  99. /// Gets processed size
  100. /// </summary>
  101. /// <returns></returns>
  102. private ulong GetProcessedSize()
  103. {
  104. return m_ProcessedSize + m_Pos;
  105. }
  106. }
  107. }