AppendingStream.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. namespace SharpCompress.IO
  5. {
  6. public class ReadOnlyAppendingStream : Stream
  7. {
  8. private readonly Queue<Stream> streams;
  9. private Stream current;
  10. public ReadOnlyAppendingStream(IEnumerable<Stream> streams)
  11. {
  12. this.streams = new Queue<Stream>(streams);
  13. }
  14. public override bool CanRead => true;
  15. public override bool CanSeek => false;
  16. public override bool CanWrite => false;
  17. public override void Flush()
  18. {
  19. throw new NotImplementedException();
  20. }
  21. public override long Length => throw new NotImplementedException();
  22. public override long Position { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
  23. public override int Read(byte[] buffer, int offset, int count)
  24. {
  25. if (current == null && streams.Count == 0)
  26. {
  27. return -1;
  28. }
  29. if (current == null)
  30. {
  31. current = streams.Dequeue();
  32. }
  33. int totalRead = 0;
  34. while (totalRead < count)
  35. {
  36. int read = current.Read(buffer, offset + totalRead, count - totalRead);
  37. if (read <= 0)
  38. {
  39. if (streams.Count == 0)
  40. {
  41. return totalRead;
  42. }
  43. current = streams.Dequeue();
  44. }
  45. totalRead += read;
  46. }
  47. return totalRead;
  48. }
  49. public override long Seek(long offset, SeekOrigin origin)
  50. {
  51. throw new NotImplementedException();
  52. }
  53. public override void SetLength(long value)
  54. {
  55. throw new NotImplementedException();
  56. }
  57. public override void Write(byte[] buffer, int offset, int count)
  58. {
  59. throw new NotImplementedException();
  60. }
  61. }
  62. }