Guest User

Untitled

a guest
Feb 20th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. namespace Ext.IO
  2. {
  3. #region Imports
  4.  
  5. using System;
  6. using System.IO;
  7. using System.Diagnostics;
  8.  
  9. #endregion
  10.  
  11. public static class StreamExtensions
  12. {
  13. /// <summary>
  14. /// Copies one stream into another using a transfer buffer size of 4K.
  15. /// </summary>
  16.  
  17. public static void Copy(this Stream input, Stream output)
  18. {
  19. ValidateArguments(input, output);
  20. Copy(input, output, 0);
  21. }
  22.  
  23. /// <summary>
  24. /// Copies one stream into another using a caller-specified transfer
  25. /// buffer size.
  26. /// </summary>
  27.  
  28. public static void Copy(this Stream input, Stream output, int bufferSize)
  29. {
  30. ValidateArguments(input, output, bufferSize);
  31. Copy(input, output, bufferSize == 0 ? null : new byte[bufferSize]);
  32. }
  33.  
  34. /// <summary>
  35. /// Copies one stream into another using a caller-specified transfer
  36. /// buffer. If the buffer is null then a default one of 4K is used.
  37. /// </summary>
  38.  
  39. public static void Copy(this Stream input, Stream output, byte[] buffer)
  40. {
  41. ValidateArguments(input, output);
  42.  
  43. buffer = buffer ?? new byte[4096];
  44. int count;
  45.  
  46. do
  47. {
  48. count = input.Read(buffer, 0, buffer.Length);
  49. output.Write(buffer, 0, count);
  50. }
  51. while (count > 0);
  52. }
  53.  
  54. /// <summary>
  55. /// Saves the content of the input stream from its current position
  56. /// to the given file path using a default transfer buffer size of
  57. /// 4K.
  58. /// </summary>
  59.  
  60. public static void SaveToFile(this Stream input, string path)
  61. {
  62. ValidateArguments(input);
  63. SaveToFile(input, path, 0);
  64. }
  65.  
  66. /// <summary>
  67. /// Saves the content of the input stream from its current position
  68. /// to the given file path using a caller-specified transfer
  69. /// buffer size.
  70. /// </summary>
  71.  
  72. public static void SaveToFile(this Stream input, string path, int bufferSize)
  73. {
  74. ValidateArguments(input);
  75.  
  76. using (var output = File.OpenWrite(path))
  77. Copy(input, output, bufferSize);
  78. }
  79.  
  80. /// <summary>
  81. /// Copies the content of the input stream from its current position
  82. /// to a memory-based stream.
  83. /// </summary>
  84.  
  85. public static MemoryStream Memorize(this Stream input)
  86. {
  87. ValidateArguments(input);
  88. var output = new MemoryStream();
  89. Copy(input, output);
  90. return output;
  91. }
  92.  
  93. /// <summary>
  94. /// Returns the remaining contents of the input as an array of
  95. /// unsigned bytes.
  96. /// </summary>
  97.  
  98. public static byte[] ToArray(this Stream input)
  99. {
  100. ValidateArguments(input);
  101. return input.Memorize().ToArray();
  102. }
  103.  
  104. #region Argument Validation
  105.  
  106. [DebuggerStepThrough]
  107. private static void ValidateArguments(Stream input)
  108. {
  109. ValidateInputStream(input);
  110. }
  111.  
  112. [DebuggerStepThrough]
  113. private static void ValidateArguments(Stream input, Stream output)
  114. {
  115. ValidateInputStream(input);
  116. ValidateOutputStream(output);
  117. }
  118.  
  119. [DebuggerStepThrough]
  120. private static void ValidateArguments(Stream input, Stream output, int bufferSize)
  121. {
  122. ValidateInputStream(input);
  123. ValidateOutputStream(output);
  124. ValidateBufferSize(bufferSize);
  125. }
  126.  
  127. [DebuggerStepThrough]
  128. private static void ValidateInputStream(Stream input)
  129. {
  130. if (input == null) throw new ArgumentNullException("input");
  131. if (!input.CanRead) throw new ArgumentException("Cannot read from input stream", "input");
  132. }
  133.  
  134. [DebuggerStepThrough]
  135. private static void ValidateOutputStream(Stream output)
  136. {
  137. if (output == null) throw new ArgumentNullException("output");
  138. if (!output.CanWrite) throw new ArgumentException("Cannot write to output stream", "output");
  139. }
  140.  
  141. [DebuggerStepThrough]
  142. private static void ValidateBufferSize(int bufferSize)
  143. {
  144. if (bufferSize < 0) throw new ArgumentException("Invalid buffer size.", "bufferSize");
  145. }
  146.  
  147. #endregion
  148. }
  149. }
Add Comment
Please, Sign In to add comment