StreamUtils.cs 8.5 KB

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