Skip to content

Instantly share code, notes, and snippets.

@atifaziz
Created September 25, 2008 22:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atifaziz/12956 to your computer and use it in GitHub Desktop.
Save atifaziz/12956 to your computer and use it in GitHub Desktop.
namespace Ext.IO
{
#region Imports
using System;
using System.IO;
using System.Diagnostics;
#endregion
public static class StreamExtensions
{
/// <summary>
/// Copies one stream into another using a transfer buffer size of 4K.
/// </summary>
public static void Copy(this Stream input, Stream output)
{
ValidateArguments(input, output);
Copy(input, output, 0);
}
/// <summary>
/// Copies one stream into another using a caller-specified transfer
/// buffer size.
/// </summary>
public static void Copy(this Stream input, Stream output, int bufferSize)
{
ValidateArguments(input, output, bufferSize);
Copy(input, output, bufferSize == 0 ? null : new byte[bufferSize]);
}
/// <summary>
/// Copies one stream into another using a caller-specified transfer
/// buffer. If the buffer is null then a default one of 4K is used.
/// </summary>
public static void Copy(this Stream input, Stream output, byte[] buffer)
{
ValidateArguments(input, output);
buffer = buffer ?? new byte[4096];
int count;
do
{
count = input.Read(buffer, 0, buffer.Length);
output.Write(buffer, 0, count);
}
while (count > 0);
}
/// <summary>
/// Saves the content of the input stream from its current position
/// to the given file path using a default transfer buffer size of
/// 4K.
/// </summary>
public static void SaveToFile(this Stream input, string path)
{
ValidateArguments(input);
SaveToFile(input, path, 0);
}
/// <summary>
/// Saves the content of the input stream from its current position
/// to the given file path using a caller-specified transfer
/// buffer size.
/// </summary>
public static void SaveToFile(this Stream input, string path, int bufferSize)
{
ValidateArguments(input);
using (var output = File.OpenWrite(path))
Copy(input, output, bufferSize);
}
/// <summary>
/// Copies the content of the input stream from its current position
/// to a memory-based stream.
/// </summary>
public static MemoryStream Memorize(this Stream input)
{
ValidateArguments(input);
var output = new MemoryStream();
Copy(input, output);
return output;
}
/// <summary>
/// Returns the remaining contents of the input as an array of
/// unsigned bytes.
/// </summary>
public static byte[] ToArray(this Stream input)
{
ValidateArguments(input);
return input.Memorize().ToArray();
}
#region Argument Validation
[DebuggerStepThrough]
private static void ValidateArguments(Stream input)
{
ValidateInputStream(input);
}
[DebuggerStepThrough]
private static void ValidateArguments(Stream input, Stream output)
{
ValidateInputStream(input);
ValidateOutputStream(output);
}
[DebuggerStepThrough]
private static void ValidateArguments(Stream input, Stream output, int bufferSize)
{
ValidateInputStream(input);
ValidateOutputStream(output);
ValidateBufferSize(bufferSize);
}
[DebuggerStepThrough]
private static void ValidateInputStream(Stream input)
{
if (input == null) throw new ArgumentNullException("input");
if (!input.CanRead) throw new ArgumentException("Cannot read from input stream", "input");
}
[DebuggerStepThrough]
private static void ValidateOutputStream(Stream output)
{
if (output == null) throw new ArgumentNullException("output");
if (!output.CanWrite) throw new ArgumentException("Cannot write to output stream", "output");
}
[DebuggerStepThrough]
private static void ValidateBufferSize(int bufferSize)
{
if (bufferSize < 0) throw new ArgumentException("Invalid buffer size.", "bufferSize");
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment