2009-12-11 09:12:31 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2009-05-22 14:37:50 +00:00
|
|
|
|
|
|
|
|
|
namespace Jellyfish.Library
|
|
|
|
|
{
|
|
|
|
|
public static class StreamExtensions
|
|
|
|
|
{
|
|
|
|
|
public static byte[] ReadAllBytes(this Stream stream)
|
|
|
|
|
{
|
2009-12-11 09:12:31 +00:00
|
|
|
|
if (stream == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("stream");
|
|
|
|
|
}
|
|
|
|
|
|
2009-05-22 14:37:50 +00:00
|
|
|
|
int count = (int)stream.Length;
|
|
|
|
|
byte[] buffer = new byte[count];
|
|
|
|
|
ReadBlock(stream, buffer, 0, count);
|
|
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int ReadBlock(this Stream stream, byte[] buffer, int offset, int count)
|
|
|
|
|
{
|
2009-12-11 09:12:31 +00:00
|
|
|
|
if (stream == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("stream");
|
|
|
|
|
}
|
|
|
|
|
|
2009-05-22 14:37:50 +00:00
|
|
|
|
int total = 0;
|
|
|
|
|
int read;
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
total += read = stream.Read(buffer, offset + total, count - total);
|
|
|
|
|
}
|
|
|
|
|
while ((read > 0) && (total < count));
|
|
|
|
|
|
|
|
|
|
return total;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|