2015-07-21 65 views
5

我想序列化Book對象:Hazelcast 3.5系列化與DataSerializable

public class Book implements DataSerializable { 

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

} 

的問題是,我不知道如何實例ObjectDataOutput/ObjectDataInput類型的對象序列化/反序列化Book對象。

ObjectDataOutputStream實現ObjectDataOutput,但我不知道怎麼到這個對象實例化becase的需要不具有公共構造SerializationService對象。

所以,在那裏創建FileOutputStream中/的FileInputStream的ObjectDataOutput/ObjectDataInput對象例如任何方式?

在此先感謝

+1

爲什麼你需要實例呢?它們由平臺提供。如果你真的想實例化它們,你可能想看看DefaultSerializationServiceBuilder並創建一個SerializationService實例並從那裏獲取流。 – pveentjer

+0

@pveentjer,你是什麼意思「他們是由平臺提供的」?你能提供任何例子嗎?我想將Hazelcast對象序列化到文件系統中 – StasKolodyuk

回答

2

感謝@pveentjer我找到了答案。

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);