Giriş
Şu satırı dahil ederiz
import com.hazelcast.nio.serialization.DataSerializable;
java.io.Externalizable gibidir. Sadece biraz daha verimli olduğu söyleniyor. com.hazelcast.nio.ObjectDataOutput
com.hazelcast.nio.ObjectDataInput
stream'lerini kullanır
DataSerializable arayüzünden kalıtan sınıfları okuyup yazan sınıf DataSerializableSerializer.
constructor
Açıklaması şöyle
Classes conforming to DataSerializable should provide a no-arguments constructor.
Örnek
Şöyle yaparız. Burada sınıfı no-argument constructor sağlamamış. Yani biraz yanlış var
class Car implements DataSerializable { private String name; private String number; @Override public void writeData(ObjectDataOutput out) throws IOException { out.writeUTF(name); out.writeUTF(number); } @Override public void readData(ObjectDataInput in) throws IOException { name = in.readUTF(); number = in.readUTF(); } }
Örnek - Test
Şöyle yaparız
import com.hazelcast.internal.serialization.InternalSerializationService; import com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder; import com.hazelcast.internal.serialization.impl.ObjectDataInputStream; import com.hazelcast.internal.serialization.impl.ObjectDataOutputStream; @Test public void testWriteData() throws Exception { Foo foo = ... DefaultSerializationServiceBuilder defaultSerializationServiceBuilder = new DefaultSerializationServiceBuilder(); InternalSerializationService serializationService = defaultSerializationServiceBuilder .setUseNativeByteOrder(false) .setAllowUnsafe(false) .setByteOrder(ByteOrder.BIG_ENDIAN) .build(); ByteArrayOutputStream bout = new ByteArrayOutputStream(); ObjectDataOutput out = new ObjectDataOutputStream(bout, serializationService); foo.writeData(out); Foo foo2 = new InsertProcessorSupplier(); byte[] byteArray = bout.toByteArray(); ByteArrayInputStream bin = new ByteArrayInputStream(byteArray); ObjectDataInput in = new ObjectDataInputStream(bin, serializationService); foo2.readData(in); }
Hiç yorum yok:
Yorum Gönder