Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hazelcast 3.5 serialization with DataSerializable

I'd like to serialize a Book object :

public class Book implements DataSerializable {

    @Override
    void writeData(ObjectDataOutput out) throws IOException {
        ...
    }
    @Override
    void readData(ObjectDataInput in) throws IOException {
        ...
    }

}

The problem is that i don't know how to instantiate objects of ObjectDataOutput/ObjectDataInput types to serialize/deserialize Book object.

ObjectDataOutputStream implements ObjectDataOutput but I don't know how to instantiate this object to becase it needs SerializationService object which doesn't have public constructors.

So, are there any ways to create an ObjectDataOutput/ObjectDataInput object from FileOutputStream/FileInputStream for example?

Thanks in advance

like image 875
StasKolodyuk Avatar asked Oct 31 '22 23:10

StasKolodyuk


1 Answers

Thanks to @pveentjer I've found the answer.

FileOutputStream fos = new FileOutputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
SerializationService serializationService = new DefaultSerializationServiceBuilder().build();
ObjectDataOutput odo = new ObjectDataOutputStream(bos, serializationService);

Book book = new Book();
book.writeData(odo);
bos.writeTo(fos);
like image 133
StasKolodyuk Avatar answered Nov 09 '22 07:11

StasKolodyuk