StreamList.cs 4.5 KB

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