OutBuffer.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. internal class OutBuffer
  17. {
  18. private readonly byte[] m_Buffer;
  19. private readonly uint m_BufferSize;
  20. private uint m_Pos;
  21. private ulong m_ProcessedSize;
  22. private Stream m_Stream;
  23. /// <summary>
  24. /// Initializes a new instance of the OutBuffer class
  25. /// </summary>
  26. /// <param name="bufferSize"></param>
  27. public OutBuffer(uint bufferSize)
  28. {
  29. m_Buffer = new byte[bufferSize];
  30. m_BufferSize = bufferSize;
  31. }
  32. public void SetStream(Stream stream)
  33. {
  34. m_Stream = stream;
  35. }
  36. public void FlushStream()
  37. {
  38. m_Stream.Flush();
  39. }
  40. public void CloseStream()
  41. {
  42. m_Stream.Close();
  43. }
  44. public void ReleaseStream()
  45. {
  46. m_Stream = null;
  47. }
  48. public void Init()
  49. {
  50. m_ProcessedSize = 0;
  51. m_Pos = 0;
  52. }
  53. public void WriteByte(byte b)
  54. {
  55. m_Buffer[m_Pos++] = b;
  56. if (m_Pos >= m_BufferSize)
  57. FlushData();
  58. }
  59. public void FlushData()
  60. {
  61. if (m_Pos == 0)
  62. return;
  63. m_Stream.Write(m_Buffer, 0, (int) m_Pos);
  64. m_Pos = 0;
  65. }
  66. public ulong GetProcessedSize()
  67. {
  68. return m_ProcessedSize + m_Pos;
  69. }
  70. }
  71. }