Search in sources :

Example 11 with BufferObjectDataInput

use of com.hazelcast.internal.nio.BufferObjectDataInput in project hazelcast by hazelcast.

the class PortableClassVersionTest method testDifferentClassVersionsUsingDataWriteAndRead.

// used in EE, so needs to be package private
static void testDifferentClassVersionsUsingDataWriteAndRead(InternalSerializationService serializationService, InternalSerializationService serializationService2) throws Exception {
    NamedPortable portableV1 = new NamedPortable("portable-v1", 111);
    Data dataV1 = serializationService.toData(portableV1);
    // emulate socket write by writing data to stream
    BufferObjectDataOutput out = serializationService.createObjectDataOutput(1024);
    writeData(out, dataV1);
    byte[] bytes = out.toByteArray();
    // emulate socket read by reading data from stream
    BufferObjectDataInput in = serializationService2.createObjectDataInput(bytes);
    dataV1 = readData(in);
    // serialize new portable version
    NamedPortableV2 portableV2 = new NamedPortableV2("portable-v2", 123, 500);
    Data dataV2 = serializationService2.toData(portableV2);
    NamedPortable v1FromV2 = serializationService.toObject(dataV2);
    assertEquals(portableV2.name, v1FromV2.name);
    assertEquals(portableV2.myint, v1FromV2.myint);
    NamedPortableV2 v2FromV1 = serializationService2.toObject(dataV1);
    assertEquals(portableV1.name, v2FromV1.name);
    assertEquals(portableV1.myint, v2FromV1.myint);
    assertNull(v2FromV1.v);
}
Also used : BufferObjectDataOutput(com.hazelcast.internal.nio.BufferObjectDataOutput) Data(com.hazelcast.internal.serialization.Data) IOUtil.writeData(com.hazelcast.internal.nio.IOUtil.writeData) IOUtil.readData(com.hazelcast.internal.nio.IOUtil.readData) BufferObjectDataInput(com.hazelcast.internal.nio.BufferObjectDataInput)

Example 12 with BufferObjectDataInput

use of com.hazelcast.internal.nio.BufferObjectDataInput in project hazelcast by hazelcast.

the class GenericRecordBuilderTest method testWriteReadGenericRecordToObjectDataInput.

@Test
public void testWriteReadGenericRecordToObjectDataInput() throws IOException {
    ClassDefinitionBuilder classDefinitionBuilder = new ClassDefinitionBuilder(1, 1);
    classDefinitionBuilder.addIntField("age");
    classDefinitionBuilder.addStringField("name");
    ClassDefinition classDefinition = classDefinitionBuilder.build();
    InternalSerializationService serializationService = new DefaultSerializationServiceBuilder().build();
    BufferObjectDataOutput objectDataOutput = serializationService.createObjectDataOutput();
    List<GenericRecord> list = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        GenericRecord record = GenericRecordBuilder.portable(classDefinition).setInt32("age", i).setString("name", " " + i).build();
        objectDataOutput.writeObject(record);
        list.add(record);
    }
    byte[] bytes = objectDataOutput.toByteArray();
    BufferObjectDataInput objectDataInput = serializationService.createObjectDataInput(bytes);
    for (int i = 0; i < 10; i++) {
        GenericRecord record = objectDataInput.readObject();
        assertEquals(list.get(i), record);
    }
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) BufferObjectDataOutput(com.hazelcast.internal.nio.BufferObjectDataOutput) ArrayList(java.util.ArrayList) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition) GenericRecord(com.hazelcast.nio.serialization.GenericRecord) BufferObjectDataInput(com.hazelcast.internal.nio.BufferObjectDataInput) ClassDefinitionBuilder(com.hazelcast.nio.serialization.ClassDefinitionBuilder) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 13 with BufferObjectDataInput

use of com.hazelcast.internal.nio.BufferObjectDataInput in project hazelcast by hazelcast.

the class MorphingPortableReaderTest method before.

@Before
public void before() throws Exception {
    service1 = (SerializationServiceV1) new DefaultSerializationServiceBuilder().addPortableFactory(TestSerializationConstants.PORTABLE_FACTORY_ID, new PortableFactory() {

        public Portable create(int classId) {
            return new MorphingBasePortable();
        }
    }).build();
    service2 = (SerializationServiceV1) new DefaultSerializationServiceBuilder().addPortableFactory(TestSerializationConstants.PORTABLE_FACTORY_ID, new PortableFactory() {

        public Portable create(int classId) {
            return new MorphingPortable();
        }
    }).build();
    Data data = service1.toData(new MorphingBasePortable((byte) 1, true, (char) 2, (short) 3, 4, 5, 1f, 2d, "test"));
    BufferObjectDataInput in = service2.createObjectDataInput(data);
    PortableSerializer portableSerializer = service2.getPortableSerializer();
    reader = portableSerializer.createMorphingReader(in);
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) Data(com.hazelcast.internal.serialization.Data) PortableFactory(com.hazelcast.nio.serialization.PortableFactory) BufferObjectDataInput(com.hazelcast.internal.nio.BufferObjectDataInput) Before(org.junit.Before)

Example 14 with BufferObjectDataInput

use of com.hazelcast.internal.nio.BufferObjectDataInput in project hazelcast by hazelcast.

the class CollectionTxnUtilTest method testWriteRead.

@Test
public void testWriteRead() throws IOException {
    InternalSerializationService ss = new DefaultSerializationServiceBuilder().build();
    BufferObjectDataOutput out = ss.createObjectDataOutput();
    CollectionTxnUtil.write(out, operationList);
    BufferObjectDataInput in = ss.createObjectDataInput(out.toByteArray());
    List<Operation> resultList = CollectionTxnUtil.read(in);
    assertEquals(operationList.size(), resultList.size());
    for (int i = 0; i < operationList.size(); i++) {
        assertEquals(operationList.get(i), resultList.get(i));
    }
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) BufferObjectDataOutput(com.hazelcast.internal.nio.BufferObjectDataOutput) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) Operation(com.hazelcast.spi.impl.operationservice.Operation) BackupAwareOperation(com.hazelcast.spi.impl.operationservice.BackupAwareOperation) BufferObjectDataInput(com.hazelcast.internal.nio.BufferObjectDataInput) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 15 with BufferObjectDataInput

use of com.hazelcast.internal.nio.BufferObjectDataInput in project hazelcast by hazelcast.

the class ReceiverTasklet method tryFillInbox.

private void tryFillInbox() {
    try {
        long totalBytes = 0;
        long totalItems = 0;
        for (byte[] payload; (payload = incoming.poll()) != null; ) {
            BufferObjectDataInput input = serializationService.createObjectDataInput(payload, PACKET_HEADER_SIZE);
            final int itemCount = input.readInt();
            for (int i = 0; i < itemCount; i++) {
                final int mark = input.position();
                final Object item = input.readObject();
                final int itemSize = input.position() - mark;
                inbox.add(new ObjWithPtionIdAndSize(item, input.readInt(), itemSize));
            }
            totalItems += itemCount;
            totalBytes += input.position();
            tracker.madeProgress();
        }
        bytesInCounter.inc(totalBytes);
        itemsInCounter.inc(totalItems);
    } catch (IOException e) {
        throw rethrow(e);
    }
}
Also used : IOException(java.io.IOException) BufferObjectDataInput(com.hazelcast.internal.nio.BufferObjectDataInput)

Aggregations

BufferObjectDataInput (com.hazelcast.internal.nio.BufferObjectDataInput)28 QuickTest (com.hazelcast.test.annotation.QuickTest)12 Test (org.junit.Test)12 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)11 Data (com.hazelcast.internal.serialization.Data)10 BufferObjectDataOutput (com.hazelcast.internal.nio.BufferObjectDataOutput)6 HeapData (com.hazelcast.internal.serialization.impl.HeapData)5 IOException (java.io.IOException)5 DefaultSerializationServiceBuilder (com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder)3 ClassDefinition (com.hazelcast.nio.serialization.ClassDefinition)3 InternalSerializationService (com.hazelcast.internal.serialization.InternalSerializationService)2 SerializationUtil.createSerializerAdapter (com.hazelcast.internal.serialization.impl.SerializationUtil.createSerializerAdapter)2 SerializationUtil.isNullData (com.hazelcast.internal.serialization.impl.SerializationUtil.isNullData)2 BufferPool (com.hazelcast.internal.serialization.impl.bufferpool.BufferPool)2 CompactStreamSerializerAdapter (com.hazelcast.internal.serialization.impl.compact.CompactStreamSerializerAdapter)2 CompactWithSchemaStreamSerializerAdapter (com.hazelcast.internal.serialization.impl.compact.CompactWithSchemaStreamSerializerAdapter)2 HazelcastSerializationException (com.hazelcast.nio.serialization.HazelcastSerializationException)2 HazelcastException (com.hazelcast.core.HazelcastException)1 IOUtil.readData (com.hazelcast.internal.nio.IOUtil.readData)1 IOUtil.writeData (com.hazelcast.internal.nio.IOUtil.writeData)1