Why size of 'BigInteger' differs when saved from what 'bitLength' reports?

82 views Asked by At

I have a variable named out that is a BigInteger. When trying to get the length of this variable in bits using

out.bitLength();

I receive 46.

If I save this in file using

ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("./testBig.dat"));
oos.writeObject(out);
oos.close();

I get a file that is 208 bytes.

Can someone explain to me why those two values differ?

1

There are 1 answers

0
Jesper On BEST ANSWER

This is because ObjectOutputStream stores objects in Java's serialization format; it does not just store the raw content of the BigInteger object.

You can only read the content back by deserializing it, for example with an ObjectInputStream.