EntryStream.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using System.IO;
  3. using SharpCompress.Readers;
  4. namespace SharpCompress.Common
  5. {
  6. public class EntryStream : Stream
  7. {
  8. private readonly IReader _reader;
  9. private readonly Stream _stream;
  10. private bool _completed;
  11. private bool _isDisposed;
  12. internal EntryStream(IReader reader, Stream stream)
  13. {
  14. _reader = reader;
  15. _stream = stream;
  16. }
  17. /// <summary>
  18. /// When reading a stream from OpenEntryStream, the stream must be completed so use this to finish reading the entire entry.
  19. /// </summary>
  20. public void SkipEntry()
  21. {
  22. this.Skip();
  23. _completed = true;
  24. }
  25. protected override void Dispose(bool disposing)
  26. {
  27. if (!(_completed || _reader.Cancelled))
  28. {
  29. SkipEntry();
  30. }
  31. if (_isDisposed)
  32. {
  33. return;
  34. }
  35. _isDisposed = true;
  36. base.Dispose(disposing);
  37. _stream.Dispose();
  38. }
  39. public override bool CanRead => true;
  40. public override bool CanSeek => false;
  41. public override bool CanWrite => false;
  42. public override void Flush()
  43. {
  44. throw new NotSupportedException();
  45. }
  46. public override long Length => _stream.Length;
  47. public override long Position { get => throw new NotSupportedException(); set => throw new NotSupportedException(); }
  48. public override int Read(byte[] buffer, int offset, int count)
  49. {
  50. int read = _stream.Read(buffer, offset, count);
  51. if (read <= 0)
  52. {
  53. _completed = true;
  54. }
  55. return read;
  56. }
  57. public override int ReadByte()
  58. {
  59. int value = _stream.ReadByte();
  60. if (value == -1)
  61. {
  62. _completed = true;
  63. }
  64. return value;
  65. }
  66. public override long Seek(long offset, SeekOrigin origin)
  67. {
  68. throw new NotSupportedException();
  69. }
  70. public override void SetLength(long value)
  71. {
  72. throw new NotSupportedException();
  73. }
  74. public override void Write(byte[] buffer, int offset, int count)
  75. {
  76. throw new NotSupportedException();
  77. }
  78. }
  79. }