StreamList.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using BestHTTP.PlatformSupport.Memory;
  2. using System;
  3. namespace BestHTTP.Extensions
  4. {
  5. /// <summary>
  6. /// Wrapper of multiple streams. Writes and reads are both supported. Read goes trough all the streams.
  7. /// </summary>
  8. public sealed class StreamList : System.IO.Stream
  9. {
  10. private System.IO.Stream[] Streams;
  11. private int CurrentIdx;
  12. public StreamList(params System.IO.Stream[] streams)
  13. {
  14. this.Streams = streams;
  15. this.CurrentIdx = 0;
  16. }
  17. public override bool CanRead
  18. {
  19. get
  20. {
  21. if (CurrentIdx >= Streams.Length)
  22. return false;
  23. return Streams[CurrentIdx].CanRead;
  24. }
  25. }
  26. public override bool CanSeek { get { return false; } }
  27. public override bool CanWrite
  28. {
  29. get
  30. {
  31. if (CurrentIdx >= Streams.Length)
  32. return false;
  33. return Streams[CurrentIdx].CanWrite;
  34. }
  35. }
  36. public override void Flush()
  37. {
  38. if (CurrentIdx >= Streams.Length)
  39. return;
  40. // We have to call the flush to all previous streams, as we may advanced the CurrentIdx
  41. for (int i = 0; i <= CurrentIdx; ++i)
  42. Streams[i].Flush();
  43. }
  44. public override long Length
  45. {
  46. get
  47. {
  48. if (CurrentIdx >= Streams.Length)
  49. return 0;
  50. long length = 0;
  51. for (int i = 0; i < Streams.Length; ++i)
  52. length += Streams[i].Length;
  53. return length;
  54. }
  55. }
  56. public override int Read(byte[] buffer, int offset, int count)
  57. {
  58. if (CurrentIdx >= Streams.Length)
  59. return -1;
  60. int readCount = Streams[CurrentIdx].Read(buffer, offset, count);
  61. while (readCount < count && ++CurrentIdx < Streams.Length)
  62. {
  63. // Dispose previous stream
  64. try
  65. {
  66. Streams[CurrentIdx - 1].Dispose();
  67. Streams[CurrentIdx - 1] = null;
  68. }
  69. catch (Exception ex)
  70. {
  71. HTTPManager.Logger.Exception("StreamList", "Dispose", ex);
  72. }
  73. readCount += Streams[CurrentIdx].Read(buffer, offset + readCount, count - readCount);
  74. }
  75. return readCount;
  76. }
  77. public override void Write(byte[] buffer, int offset, int count)
  78. {
  79. if (CurrentIdx >= Streams.Length)
  80. return;
  81. Streams[CurrentIdx].Write(buffer, offset, count);
  82. }
  83. public void Write(string str)
  84. {
  85. byte[] bytes = str.GetASCIIBytes();
  86. this.Write(bytes, 0, bytes.Length);
  87. BufferPool.Release(bytes);
  88. }
  89. protected override void Dispose(bool disposing)
  90. {
  91. if (disposing)
  92. {
  93. for (int i = 0; i < Streams.Length; ++i)
  94. if (Streams[i] != null)
  95. {
  96. try
  97. {
  98. Streams[i].Dispose();
  99. }
  100. catch (Exception ex)
  101. {
  102. HTTPManager.Logger.Exception("StreamList", "Dispose", ex);
  103. }
  104. }
  105. }
  106. }
  107. public override long Position
  108. {
  109. get
  110. {
  111. throw new NotImplementedException("Position get");
  112. }
  113. set
  114. {
  115. throw new NotImplementedException("Position set");
  116. }
  117. }
  118. public override long Seek(long offset, System.IO.SeekOrigin origin)
  119. {
  120. if (CurrentIdx >= Streams.Length)
  121. return 0;
  122. return Streams[CurrentIdx].Seek(offset, origin);
  123. }
  124. public override void SetLength(long value)
  125. {
  126. throw new NotImplementedException("SetLength");
  127. }
  128. }
  129. }