private void copy(Stream from, Stream to, long length)
{
long l = 0;
long size = 10000;
int c = -1;
byte[] b = new byte[size];
while (c != 0 && l < length)
{
c = from.Read(b, 0, (int)(l + size < length ? b.Length : length - l));
l += c;
to.Write(b, 0, c);
}
//pad stream, if input stream was too short
if (l < length)
{
for (int i = 0; i < size; i++)
b[i] = 0;
while (l < length)
{
if (length - l > size)
to.Write(b, 0, (int)size);
else
to.Write(b, 0, (int)(length - l));
l += size;
}
}
}