public static byte[] GetBuffer(Stream stream, int index)
{
byte[] buffer = null;
if (stream != null)
{
buffer = new byte[stream.Length - index];
//Setting position to ensure at beginning.
long currentPosition = 0;
if (stream.CanSeek)
{
currentPosition = stream.Position;
stream.Position = index;
}
int bytesRead = 0;
int totalRead = 0;
do
{
bytesRead = stream.Read(buffer, totalRead, buffer.Length - totalRead);
totalRead += bytesRead;
} while (bytesRead > 0 && totalRead < buffer.Length);
if (stream.CanSeek)
{
stream.Position = currentPosition;
}
}
return buffer;
}