Search in sources :

Example 76 with ObjectDataInput

use of com.hazelcast.nio.ObjectDataInput 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());
}
Also used : ObjectDataOutput(com.hazelcast.nio.ObjectDataOutput) DataSerializable(com.hazelcast.nio.serialization.DataSerializable) Serializable(java.io.Serializable) SerializationConfig(com.hazelcast.config.SerializationConfig) SerializationService(com.hazelcast.internal.serialization.SerializationService) Data(com.hazelcast.internal.serialization.Data) GlobalSerializerConfig(com.hazelcast.config.GlobalSerializerConfig) JavaSerializer(com.hazelcast.internal.serialization.impl.defaultserializers.JavaDefaultSerializers.JavaSerializer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StreamSerializer(com.hazelcast.nio.serialization.StreamSerializer) ObjectDataInput(com.hazelcast.nio.ObjectDataInput) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 77 with ObjectDataInput

use of com.hazelcast.nio.ObjectDataInput in project hazelcast by hazelcast.

the class APortable method readPortable.

public void readPortable(PortableReader reader) throws IOException {
    bool = reader.readBoolean("bool");
    b = reader.readByte("b");
    c = reader.readChar("c");
    d = reader.readDouble("d");
    s = reader.readShort("s");
    f = reader.readFloat("f");
    i = reader.readInt("i");
    l = reader.readLong("l");
    str = reader.readString("str");
    p = reader.readPortable("p");
    booleans = reader.readBooleanArray("booleans");
    bytes = reader.readByteArray("bs");
    chars = reader.readCharArray("cs");
    doubles = reader.readDoubleArray("ds");
    shorts = reader.readShortArray("ss");
    floats = reader.readFloatArray("fs");
    ints = reader.readIntArray("is");
    longs = reader.readLongArray("ls");
    strings = reader.readStringArray("strs");
    portables = reader.readPortableArray("ps");
    booleansNull = reader.readBooleanArray("booleansNull");
    bytesNull = reader.readByteArray("bsNull");
    charsNull = reader.readCharArray("csNull");
    doublesNull = reader.readDoubleArray("dsNull");
    shortsNull = reader.readShortArray("ssNull");
    floatsNull = reader.readFloatArray("fsNull");
    intsNull = reader.readIntArray("isNull");
    longsNull = reader.readLongArray("lsNull");
    stringsNull = reader.readStringArray("strsNull");
    ObjectDataInput dataInput = reader.getRawDataInput();
    bool = dataInput.readBoolean();
    b = dataInput.readByte();
    c = dataInput.readChar();
    d = dataInput.readDouble();
    s = dataInput.readShort();
    f = dataInput.readFloat();
    i = dataInput.readInt();
    l = dataInput.readLong();
    str = dataInput.readString();
    booleans = dataInput.readBooleanArray();
    bytes = dataInput.readByteArray();
    chars = dataInput.readCharArray();
    doubles = dataInput.readDoubleArray();
    shorts = dataInput.readShortArray();
    floats = dataInput.readFloatArray();
    ints = dataInput.readIntArray();
    longs = dataInput.readLongArray();
    strings = dataInput.readStringArray();
    booleansNull = dataInput.readBooleanArray();
    bytesNull = dataInput.readByteArray();
    charsNull = dataInput.readCharArray();
    doublesNull = dataInput.readDoubleArray();
    shortsNull = dataInput.readShortArray();
    floatsNull = dataInput.readFloatArray();
    intsNull = dataInput.readIntArray();
    longsNull = dataInput.readLongArray();
    stringsNull = dataInput.readStringArray();
    byteSize = dataInput.readByte();
    bytesFully = new byte[byteSize];
    dataInput.readFully(bytesFully);
    bytesOffset = new byte[2];
    dataInput.readFully(bytesOffset, 0, 2);
    int strSize = dataInput.readInt();
    strChars = new char[strSize];
    for (int j = 0; j < strSize; j++) {
        strChars[j] = dataInput.readChar();
    }
    strBytes = new byte[strSize];
    dataInput.readFully(strBytes);
    unsignedByte = dataInput.readUnsignedByte();
    unsignedShort = dataInput.readUnsignedShort();
    portableObject = dataInput.readObject();
    identifiedDataSerializableObject = dataInput.readObject();
    customByteArraySerializableObject = dataInput.readObject();
    customStreamSerializableObject = dataInput.readObject();
    data = readData(dataInput);
}
Also used : ObjectDataInput(com.hazelcast.nio.ObjectDataInput)

Example 78 with ObjectDataInput

use of com.hazelcast.nio.ObjectDataInput 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);
    }
}
Also used : ObjectDataOutput(com.hazelcast.nio.ObjectDataOutput) HazelcastInstance(com.hazelcast.core.HazelcastInstance) GlobalSerializerConfig(com.hazelcast.config.GlobalSerializerConfig) Config(com.hazelcast.config.Config) NearCacheConfig(com.hazelcast.config.NearCacheConfig) StreamSerializer(com.hazelcast.nio.serialization.StreamSerializer) ObjectDataInput(com.hazelcast.nio.ObjectDataInput) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) GlobalSerializerConfig(com.hazelcast.config.GlobalSerializerConfig) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 79 with ObjectDataInput

use of com.hazelcast.nio.ObjectDataInput in project hazelcast by hazelcast.

the class PartitionLostListenerTest method test_internalPartitionLostEvent_deserialization.

@Test
public void test_internalPartitionLostEvent_deserialization() throws IOException {
    PartitionLostEventImpl internalEvent = new PartitionLostEventImpl();
    ObjectDataInput input = mock(ObjectDataInput.class);
    when(input.readInt()).thenReturn(1, 2);
    internalEvent.readData(input);
    assertEquals(1, internalEvent.getPartitionId());
    assertEquals(2, internalEvent.getLostReplicaIndex());
}
Also used : ObjectDataInput(com.hazelcast.nio.ObjectDataInput) PartitionLostEventImpl(com.hazelcast.internal.partition.PartitionLostEventImpl) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 80 with ObjectDataInput

use of com.hazelcast.nio.ObjectDataInput in project hazelcast-jet by hazelcast.

the class DistributedIntSummaryStatisticsTest method readData.

@Test
public void readData() throws IOException {
    // Given
    DistributedIntSummaryStatistics stats = new DistributedIntSummaryStatistics();
    ObjectDataInput in = mock(ObjectDataInput.class);
    when(in.readLong()).thenReturn(1L).thenReturn(2L);
    when(in.readInt()).thenReturn(3).thenReturn(4);
    // When
    stats.readData(in);
    // Then
    assertEquals(1, stats.getCount());
    assertEquals(2, stats.getSum());
    assertEquals(3, stats.getMin());
    assertEquals(4, stats.getMax());
}
Also used : ObjectDataInput(com.hazelcast.nio.ObjectDataInput) Test(org.junit.Test)

Aggregations

ObjectDataInput (com.hazelcast.nio.ObjectDataInput)81 Test (org.junit.Test)35 QuickTest (com.hazelcast.test.annotation.QuickTest)32 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)24 BufferObjectDataInput (com.hazelcast.internal.nio.BufferObjectDataInput)17 FieldKind (com.hazelcast.nio.serialization.FieldKind)16 IOException (java.io.IOException)13 ObjectDataOutput (com.hazelcast.nio.ObjectDataOutput)12 Nullable (javax.annotation.Nullable)9 GlobalSerializerConfig (com.hazelcast.config.GlobalSerializerConfig)7 SerializationConfig (com.hazelcast.config.SerializationConfig)6 StreamSerializer (com.hazelcast.nio.serialization.StreamSerializer)6 Data (com.hazelcast.nio.serialization.Data)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Data (com.hazelcast.internal.serialization.Data)3 SerializationService (com.hazelcast.internal.serialization.SerializationService)3 DefaultSerializationServiceBuilder (com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder)3 HeapData (com.hazelcast.internal.serialization.impl.HeapData)3 SerializationService (com.hazelcast.spi.serialization.SerializationService)3 SerializerConfig (com.hazelcast.config.SerializerConfig)2