I have a BitSet and want to write it to a file- I came across a solution to use a ObjectOutputStream using the writeObject method.
I looked at the ObjectOutputStream in the java API and saw that you can write other things (byte, int, short etc)
I tried to check out the class so I tried to write a byte to a file using the following code but the result gives me a file with 7 bytes instead of 1 byte
my question is what are the first 6 bytes in the file? why are they there?
my question is relevant to a BitSet because i don't want to start writing lots of data to a file and realize I have random bytes inserted in the file without knowing what they are.
here is the code:
byte[] bt = new byte[]{'A'};
File outFile = new File("testOut.txt");
FileOutputStream fos = new FileOutputStream(outFile);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.write(bt);
oos.close();
thanks for any help
Avner
The serialisation format, like many others, includes a header with magic number and version information. When you use
DataOutput
/OutputStream
methods onObjectOutputStream
are placed in the middle of the serialised data (with no type information). This is typically only done inwriteObject
implementations after a call todefaultWriteObject
or use ofputFields
.If you only use the saved BitSet in Java, the serialization works fine. However, it's kind of annoying if you want share the bitset across multi platforms. Besides the overhead of Java serialization, the BitSet is stored in units of 8-bytes. This can generate too much overhead if your bitset is small.
We wrote this small class so we can exract byte arrays from BitSet. Depending on your usecase, it might work better than Java serialization for you.
You could be writing any objects out to an
ObjectOutputStream
, so the stream holds information about the types written as well as the data needed to reconstitute the object.If you know that the stream will always contain a BitSet, don't use an
ObjectOutputStream
- and if space is a premium, then convert theBitSet
to a set of bytes where each bit corresponds to a bit in theBitSet
, then write that directly to the underlying stream (e.g. aFileOutputStream
as in your example).The other bytes will be type information.
Basically ObjectOutputStream is a class used to write Serializable objects to some destination (usually a file). It makes more sense if you think about InputObjectStream. It has a readObject() method on it. How does Java know what Object to instantiate? Easy: there is type information in there.