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
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With