0

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.

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
Neman
  • 1,237
  • 2
  • 13
  • 16

2 Answers2

0

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.
mindas
  • 26,463
  • 15
  • 97
  • 154
0

Thanks, I ended with this solution:

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

    @Override
    public void writeExternal(ObjectOutput out) throws IOException
    {
        out.writeObject(bits);
        out.writeInt(wlen);     
    }

    @Override
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
    {
        bits = (long[]) in.readObject();
        wlen = in.readInt();
    }
}
Neman
  • 1,237
  • 2
  • 13
  • 16