StreamUtils.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. throw new ArgumentNullException("nameof(stream)");
  34. }
  35. if (buffer == null) {
  36. throw new ArgumentNullException("nameof(buffer)");
  37. }
  38. // Offset can equal length when buffer and count are 0.
  39. if ((offset < 0) || (offset > buffer.Length)) {
  40. throw new ArgumentOutOfRangeException("nameof(offset)");
  41. }
  42. if ((count < 0) || (offset + count > buffer.Length)) {
  43. throw new ArgumentOutOfRangeException("nameof(count)");
  44. }
  45. while (count > 0) {
  46. int readCount = stream.Read(buffer, offset, count);
  47. if (readCount <= 0) {
  48. throw new EndOfStreamException();
  49. }
  50. offset += readCount;
  51. count -= readCount;
  52. }
  53. }
  54. /// <summary>
  55. /// Copy the contents of one <see cref="Stream"/> to another.
  56. /// </summary>
  57. /// <param name="source">The stream to source data from.</param>
  58. /// <param name="destination">The stream to write data to.</param>
  59. /// <param name="buffer">The buffer to use during copying.</param>
  60. static public void Copy(Stream source, Stream destination, byte[] buffer)
  61. {
  62. if (source == null) {
  63. throw new ArgumentNullException("nameof(source)");
  64. }
  65. if (destination == null) {
  66. throw new ArgumentNullException("nameof(destination)");
  67. }
  68. if (buffer == null) {
  69. throw new ArgumentNullException("nameof(buffer)");
  70. }
  71. // Ensure a reasonable size of buffer is used without being prohibitive.
  72. if (buffer.Length < 128) {
  73. throw new ArgumentException("Buffer is too small", "nameof(buffer)");
  74. }
  75. bool copying = true;
  76. while (copying) {
  77. int bytesRead = source.Read(buffer, 0, buffer.Length);
  78. if (bytesRead > 0) {
  79. destination.Write(buffer, 0, bytesRead);
  80. } else {
  81. destination.Flush();
  82. copying = false;
  83. }
  84. }
  85. }
  86. /// <summary>
  87. /// Copy the contents of one <see cref="Stream"/> to another.
  88. /// </summary>
  89. /// <param name="source">The stream to source data from.</param>
  90. /// <param name="destination">The stream to write data to.</param>
  91. /// <param name="buffer">The buffer to use during copying.</param>
  92. /// <param name="progressHandler">The <see cref="ProgressHandler">progress handler delegate</see> to use.</param>
  93. /// <param name="updateInterval">The minimum <see cref="TimeSpan"/> between progress updates.</param>
  94. /// <param name="sender">The source for this event.</param>
  95. /// <param name="name">The name to use with the event.</param>
  96. /// <remarks>This form is specialised for use within #Zip to support events during archive operations.</remarks>
  97. static public void Copy(Stream source, Stream destination,
  98. byte[] buffer, ProgressHandler progressHandler, TimeSpan updateInterval, object sender, string name)
  99. {
  100. Copy(source, destination, buffer, progressHandler, updateInterval, sender, name, -1);
  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. /// <param name="progressHandler">The <see cref="ProgressHandler">progress handler delegate</see> to use.</param>
  109. /// <param name="updateInterval">The minimum <see cref="TimeSpan"/> between progress updates.</param>
  110. /// <param name="sender">The source for this event.</param>
  111. /// <param name="name">The name to use with the event.</param>
  112. /// <param name="fixedTarget">A predetermined fixed target value to use with progress updates.
  113. /// If the value is negative the target is calculated by looking at the stream.</param>
  114. /// <remarks>This form is specialised for use within #Zip to support events during archive operations.</remarks>
  115. static public void Copy(Stream source, Stream destination,
  116. byte[] buffer,
  117. ProgressHandler progressHandler, TimeSpan updateInterval,
  118. object sender, string name, long fixedTarget)
  119. {
  120. if (source == null) {
  121. throw new ArgumentNullException("nameof(source)");
  122. }
  123. if (destination == null) {
  124. throw new ArgumentNullException("nameof(destination)");
  125. }
  126. if (buffer == null) {
  127. throw new ArgumentNullException("nameof(buffer)");
  128. }
  129. // Ensure a reasonable size of buffer is used without being prohibitive.
  130. if (buffer.Length < 128) {
  131. throw new ArgumentException("Buffer is too small", "nameof(buffer)");
  132. }
  133. if (progressHandler == null) {
  134. throw new ArgumentNullException("nameof(progressHandler)");
  135. }
  136. bool copying = true;
  137. DateTime marker = DateTime.Now;
  138. long processed = 0;
  139. long target = 0;
  140. if (fixedTarget >= 0) {
  141. target = fixedTarget;
  142. } else if (source.CanSeek) {
  143. target = source.Length - source.Position;
  144. }
  145. // Always fire 0% progress..
  146. var args = new ProgressEventArgs(name, processed, target);
  147. progressHandler(sender, args);
  148. bool progressFired = true;
  149. while (copying) {
  150. int bytesRead = source.Read(buffer, 0, buffer.Length);
  151. if (bytesRead > 0) {
  152. processed += bytesRead;
  153. progressFired = false;
  154. destination.Write(buffer, 0, bytesRead);
  155. } else {
  156. destination.Flush();
  157. copying = false;
  158. }
  159. if (DateTime.Now - marker > updateInterval) {
  160. progressFired = true;
  161. marker = DateTime.Now;
  162. args = new ProgressEventArgs(name, processed, target);
  163. progressHandler(sender, args);
  164. copying = args.ContinueRunning;
  165. }
  166. }
  167. if (!progressFired) {
  168. args = new ProgressEventArgs(name, processed, target);
  169. progressHandler(sender, args);
  170. }
  171. }
  172. /// <summary>
  173. /// Initialise an instance of <see cref="StreamUtils"></see>
  174. /// </summary>
  175. private StreamUtils()
  176. {
  177. // Do nothing.
  178. }
  179. }
  180. }