Reputation: 4524
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
Upvotes: 5
Views: 614
Reputation: 4524
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);
Upvotes: 2