StreamUtils.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. using System;
  2. using System.IO;
  3. namespace System.Net.ApiService.Compression.Core {
  4. /// <summary>
  5. /// Provides simple <see cref="Stream"/>" utilities.
  6. /// </summary>
  7. public sealed class StreamUtils
  8. {
  9. /// <summary>
  10. /// Read from a <see cref="Stream"/> ensuring all the required data is read.
  11. /// </summary>
  12. /// <param name="stream">The stream to read.</param>
  13. /// <param name="buffer">The buffer to fill.</param>
  14. /// <seealso cref="ReadFully(Stream,byte[],int,int)"/>
  15. static public void ReadFully(Stream stream, byte[] buffer)
  16. {
  17. ReadFully(stream, buffer, 0, buffer.Length);
  18. }
  19. /// <summary>
  20. /// Read from a <see cref="Stream"/>" ensuring all the required data is read.
  21. /// </summary>
  22. /// <param name="stream">The stream to read data from.</param>
  23. /// <param name="buffer">The buffer to store data in.</param>
  24. /// <param name="offset">The offset at which to begin storing data.</param>
  25. /// <param name="count">The number of bytes of data to store.</param>
  26. /// <exception cref="ArgumentNullException">Required parameter is null</exception>
  27. /// <exception cref="ArgumentOutOfRangeException"><paramref name="offset"/> and or <paramref name="count"/> are invalid.</exception>
  28. /// <exception cref="EndOfStreamException">End of stream is encountered before all the data has been read.</exception>
  29. static public void ReadFully(Stream stream, byte[] buffer, int offset, int count)
  30. {
  31. if (stream == null)
  32. {
  33. throw new ArgumentNullException(nameof(stream));
  34. }
  35. if (buffer == null)
  36. {
  37. throw new ArgumentNullException(nameof(buffer));
  38. }
  39. // Offset can equal length when buffer and count are 0.
  40. if ((offset < 0) || (offset > buffer.Length))
  41. {
  42. throw new ArgumentOutOfRangeException(nameof(offset));
  43. }
  44. if ((count < 0) || (offset + count > buffer.Length))
  45. {
  46. throw new ArgumentOutOfRangeException(nameof(count));
  47. }
  48. while (count > 0)
  49. {
  50. int readCount = stream.Read(buffer, offset, count);
  51. if (readCount <= 0)
  52. {
  53. throw new EndOfStreamException();
  54. }
  55. offset += readCount;
  56. count -= readCount;
  57. }
  58. }
  59. /// <summary>
  60. /// Read as much data as possible from a <see cref="Stream"/>", up to the requested number of bytes
  61. /// </summary>
  62. /// <param name="stream">The stream to read data from.</param>
  63. /// <param name="buffer">The buffer to store data in.</param>
  64. /// <param name="offset">The offset at which to begin storing data.</param>
  65. /// <param name="count">The number of bytes of data to store.</param>
  66. /// <exception cref="ArgumentNullException">Required parameter is null</exception>
  67. /// <exception cref="ArgumentOutOfRangeException"><paramref name="offset"/> and or <paramref name="count"/> are invalid.</exception>
  68. static public int ReadRequestedBytes(Stream stream, byte[] buffer, int offset, int count)
  69. {
  70. if (stream == null)
  71. {
  72. throw new ArgumentNullException(nameof(stream));
  73. }
  74. if (buffer == null)
  75. {
  76. throw new ArgumentNullException(nameof(buffer));
  77. }
  78. // Offset can equal length when buffer and count are 0.
  79. if ((offset < 0) || (offset > buffer.Length))
  80. {
  81. throw new ArgumentOutOfRangeException(nameof(offset));
  82. }
  83. if ((count < 0) || (offset + count > buffer.Length))
  84. {
  85. throw new ArgumentOutOfRangeException(nameof(count));
  86. }
  87. int totalReadCount = 0;
  88. while (count > 0)
  89. {
  90. int readCount = stream.Read(buffer, offset, count);
  91. if (readCount <= 0)
  92. {
  93. break;
  94. }
  95. offset += readCount;
  96. count -= readCount;
  97. totalReadCount += readCount;
  98. }
  99. return totalReadCount;
  100. }
  101. /// <summary>
  102. /// Copy the contents of one <see cref="Stream"/> to another.
  103. /// </summary>
  104. /// <param name="source">The stream to source data from.</param>
  105. /// <param name="destination">The stream to write data to.</param>
  106. /// <param name="buffer">The buffer to use during copying.</param>
  107. static public void Copy(Stream source, Stream destination, byte[] buffer)
  108. {
  109. if (source == null)
  110. {
  111. throw new ArgumentNullException(nameof(source));
  112. }
  113. if (destination == null)
  114. {
  115. throw new ArgumentNullException(nameof(destination));
  116. }
  117. if (buffer == null)
  118. {
  119. throw new ArgumentNullException(nameof(buffer));
  120. }
  121. // Ensure a reasonable size of buffer is used without being prohibitive.
  122. if (buffer.Length < 128)
  123. {
  124. throw new ArgumentException("Buffer is too small", nameof(buffer));
  125. }
  126. bool copying = true;
  127. while (copying)
  128. {
  129. int bytesRead = source.Read(buffer, 0, buffer.Length);
  130. if (bytesRead > 0)
  131. {
  132. destination.Write(buffer, 0, bytesRead);
  133. }
  134. else
  135. {
  136. destination.Flush();
  137. copying = false;
  138. }
  139. }
  140. }
  141. /// <summary>
  142. /// Copy the contents of one <see cref="Stream"/> to another.
  143. /// </summary>
  144. /// <param name="source">The stream to source data from.</param>
  145. /// <param name="destination">The stream to write data to.</param>
  146. /// <param name="buffer">The buffer to use during copying.</param>
  147. /// <param name="progressHandler">The <see cref="ProgressHandler">progress handler delegate</see> to use.</param>
  148. /// <param name="updateInterval">The minimum <see cref="TimeSpan"/> between progress updates.</param>
  149. /// <param name="sender">The source for this event.</param>
  150. /// <param name="name">The name to use with the event.</param>
  151. /// <remarks>This form is specialised for use within #Zip to support events during archive operations.</remarks>
  152. static public void Copy(Stream source, Stream destination,
  153. byte[] buffer, ProgressHandler progressHandler, TimeSpan updateInterval, object sender, string name)
  154. {
  155. Copy(source, destination, buffer, progressHandler, updateInterval, sender, name, -1);
  156. }
  157. /// <summary>
  158. /// Copy the contents of one <see cref="Stream"/> to another.
  159. /// </summary>
  160. /// <param name="source">The stream to source data from.</param>
  161. /// <param name="destination">The stream to write data to.</param>
  162. /// <param name="buffer">The buffer to use during copying.</param>
  163. /// <param name="progressHandler">The <see cref="ProgressHandler">progress handler delegate</see> to use.</param>
  164. /// <param name="updateInterval">The minimum <see cref="TimeSpan"/> between progress updates.</param>
  165. /// <param name="sender">The source for this event.</param>
  166. /// <param name="name">The name to use with the event.</param>
  167. /// <param name="fixedTarget">A predetermined fixed target value to use with progress updates.
  168. /// If the value is negative the target is calculated by looking at the stream.</param>
  169. /// <remarks>This form is specialised for use within #Zip to support events during archive operations.</remarks>
  170. static public void Copy(Stream source, Stream destination,
  171. byte[] buffer,
  172. ProgressHandler progressHandler, TimeSpan updateInterval,
  173. object sender, string name, long fixedTarget)
  174. {
  175. if (source == null)
  176. {
  177. throw new ArgumentNullException(nameof(source));
  178. }
  179. if (destination == null)
  180. {
  181. throw new ArgumentNullException(nameof(destination));
  182. }
  183. if (buffer == null)
  184. {
  185. throw new ArgumentNullException(nameof(buffer));
  186. }
  187. // Ensure a reasonable size of buffer is used without being prohibitive.
  188. if (buffer.Length < 128)
  189. {
  190. throw new ArgumentException("Buffer is too small", nameof(buffer));
  191. }
  192. if (progressHandler == null)
  193. {
  194. throw new ArgumentNullException(nameof(progressHandler));
  195. }
  196. bool copying = true;
  197. DateTime marker = DateTime.Now;
  198. long processed = 0;
  199. long target = 0;
  200. if (fixedTarget >= 0)
  201. {
  202. target = fixedTarget;
  203. }
  204. else if (source.CanSeek)
  205. {
  206. target = source.Length - source.Position;
  207. }
  208. // Always fire 0% progress..
  209. var args = new ProgressEventArgs(name, processed, target);
  210. progressHandler(sender, args);
  211. bool progressFired = true;
  212. while (copying)
  213. {
  214. int bytesRead = source.Read(buffer, 0, buffer.Length);
  215. if (bytesRead > 0)
  216. {
  217. processed += bytesRead;
  218. progressFired = false;
  219. destination.Write(buffer, 0, bytesRead);
  220. }
  221. else
  222. {
  223. destination.Flush();
  224. copying = false;
  225. }
  226. if (DateTime.Now - marker > updateInterval)
  227. {
  228. progressFired = true;
  229. marker = DateTime.Now;
  230. args = new ProgressEventArgs(name, processed, target);
  231. progressHandler(sender, args);
  232. copying = args.ContinueRunning;
  233. }
  234. }
  235. if (!progressFired)
  236. {
  237. args = new ProgressEventArgs(name, processed, target);
  238. progressHandler(sender, args);
  239. }
  240. }
  241. /// <summary>
  242. /// Initialise an instance of <see cref="StreamUtils"></see>
  243. /// </summary>
  244. private StreamUtils()
  245. {
  246. // Do nothing.
  247. }
  248. }
  249. }