use of com.hazelcast.nio.ObjectDataOutput in project hazelcast by hazelcast.
the class SerializationIssueTest method testGlobalSerializer_withOverrideJavaSerializable.
@Test
public void testGlobalSerializer_withOverrideJavaSerializable() {
GlobalSerializerConfig globalSerializerConfig = new GlobalSerializerConfig();
globalSerializerConfig.setOverrideJavaSerialization(true);
final AtomicInteger writeCounter = new AtomicInteger();
final AtomicInteger readCounter = new AtomicInteger();
final JavaSerializer javaSerializer = new JavaSerializer(true, false, null);
SerializationConfig serializationConfig = new SerializationConfig().setGlobalSerializerConfig(globalSerializerConfig.setImplementation(new StreamSerializer<Object>() {
@Override
public void write(ObjectDataOutput out, Object v) throws IOException {
writeCounter.incrementAndGet();
if (v instanceof Serializable) {
out.writeBoolean(true);
javaSerializer.write(out, v);
} else if (v instanceof DummyValue) {
out.writeBoolean(false);
out.writeString(((DummyValue) v).s);
out.writeInt(((DummyValue) v).k);
}
}
@Override
public Object read(ObjectDataInput in) throws IOException {
readCounter.incrementAndGet();
boolean java = in.readBoolean();
if (java) {
return javaSerializer.read(in);
}
return new DummyValue(in.readString(), in.readInt());
}
public int getTypeId() {
return 123;
}
public void destroy() {
}
}));
SerializationService ss1 = new DefaultSerializationServiceBuilder().setConfig(serializationConfig).build();
DummyValue value = new DummyValue("test", 111);
Data data1 = ss1.toData(value);
Data data2 = ss1.toData(new Foo());
Assert.assertNotNull(data1);
Assert.assertNotNull(data2);
assertEquals(2, writeCounter.get());
SerializationService ss2 = new DefaultSerializationServiceBuilder().setConfig(serializationConfig).build();
Object o1 = ss2.toObject(data1);
Object o2 = ss2.toObject(data2);
Assert.assertEquals(value, o1);
Assert.assertNotNull(o2);
assertEquals(2, readCounter.get());
}
use of com.hazelcast.nio.ObjectDataOutput in project hazelcast by hazelcast.
the class IssuesTest method testIssue1067GlobalSerializer.
@Test
public void testIssue1067GlobalSerializer() {
TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
final Config config = getConfig();
GlobalSerializerConfig globalSerializerConfig = new GlobalSerializerConfig();
globalSerializerConfig.setOverrideJavaSerialization(false);
config.getSerializationConfig().setGlobalSerializerConfig(globalSerializerConfig.setImplementation(new StreamSerializer() {
public void write(ObjectDataOutput out, Object object) throws IOException {
}
public Object read(ObjectDataInput in) throws IOException {
return new DummyValue();
}
public int getTypeId() {
return 123;
}
public void destroy() {
}
}));
HazelcastInstance hz = factory.newHazelcastInstance(config);
IMap<Object, Object> map = hz.getMap("test");
for (int i = 0; i < 10; i++) {
map.put(i, new DummyValue());
}
assertEquals(10, map.size());
HazelcastInstance hz2 = factory.newHazelcastInstance(config);
IMap<Object, Object> map2 = hz2.getMap("test");
assertEquals(10, map2.size());
assertEquals(10, map.size());
for (int i = 0; i < 10; i++) {
Object o = map2.get(i);
assertNotNull(o);
assertTrue(o instanceof DummyValue);
}
}
use of com.hazelcast.nio.ObjectDataOutput in project hazelcast by hazelcast.
the class Employee method writePortable.
public void writePortable(PortableWriter writer) throws IOException {
writer.writeString("n", name);
writer.writeInt("a", age);
writer.writeByte("b", by);
writer.writeChar("c", c);
writer.writeBoolean("bo", bool);
writer.writeShort("s", s);
writer.writeInt("i", i);
writer.writeLong("l", l);
writer.writeFloat("f", f);
writer.writeDouble("d", d);
writer.writeString("str", str);
writer.writeString("utfstr", utfStr);
writer.writeByteArray("bb", byteArray);
writer.writeCharArray("cc", charArray);
writer.writeBooleanArray("ba", boolArray);
writer.writeShortArray("ss", shortArray);
writer.writeIntArray("ii", integerArray);
writer.writeFloatArray("ff", floatArray);
writer.writeDoubleArray("dd", doubleArray);
ObjectDataOutput out = writer.getRawDataOutput();
out.writeObject(by);
out.writeObject(c);
out.writeObject(bool);
out.writeObject(s);
out.writeObject(i);
out.writeObject(f);
out.writeObject(d);
out.writeObject(str);
out.writeObject(utfStr);
}
Aggregations