public static int Read7BitEncodedInt(this Stream stream)
{
int ret = 0;
int shift = 0;
int len;
byte b;
for (len = 0; len < 5; ++len)
{
b = (byte)stream.ReadByte();
ret = ret | ((b & 0x7f) << shift);
shift += 7;
if ((b & 0x80) == 0)
break;
}
if (len < 5)
return ret;
throw new FormatException("Too many bytes in what should have been a 7 bit encoded Int32.");
}