Search code examples
javaserializationlucenebitset

Serialize Lucene's OpenBitSet 4.9.0 using Java


Using Lucene's OpenBitSet 4.9.0, I would like to serialize an object instance of the OpenBitSet-class. As the OpenBitSet class does not implement Serializable, I made my own class which extends OpenBitSet:

public class MyBitSet extends OpenBitSet implements Serializable
{
    private static final long serialVersionUID = 1L;
}

However, after deserialization, the original bits are not set. How to correctly implement a serializable OpenBitSet?

Quote from their website:

It also allows one to efficiently implement alternate serialization or interchange formats.


Solution

  • I would do this like following:

    1. Extend OpenBitSet to get access to protected long[] bits and protected int wlen. These are the only ones that provide state.
    2. Implement Externalizable, and (de)serialize those two fields in readExternal and writeExternal.