public static double ReadDouble(Stream stream)
{
if (stream == null) {
throw new ArgumentNullException("stream");
}
int b = stream.ReadByte();
if (b >= 0x00 && b < 0x18) {
return (double)b;
}
if (b >= 0x20 && b < 0x38) {
return (double)(-1 - b);
}
while ((b >> 5) == 6) {
// Skip tags until a tag character is no longer read
if (b == 0xd8) {
stream.ReadByte();
} else if (b == 0xd9) {
stream.Position += 2;
} else if (b == 0xda) {
stream.Position += 4;
} else if (b == 0xdb) {
stream.Position += 8;
} else if (b > 0xdb) {
throw new IOException("Not a 32-bit integer");
}
b = stream.ReadByte();
}
if (b >= 0x00 && b < 0x18) {
return (double)b;
}
if (b >= 0x20 && b < 0x38) {
return (double)(-1 - b);
}
if (b == 0xf9 || b == 0xfa || b == 0xfb) {
// Read a floating-point number
return ReadFP(stream, b);
}
if (b == 0x18 || b == 0x19 || b == 0x1a || b == 0x38 ||
b == 0x39 || b == 0x3a) { // covers headbytes 0x18-0x1a and 0x38-0x3A
return (double)ReadInteger(stream, b, false);
}
throw new IOException("Not a double");
}