Search in sources :

Example 21 with BufferObjectDataInput

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

the class BufferPoolTest method returnInputBuffer.

@Test
public void returnInputBuffer() {
    BufferObjectDataInput in = mock(BufferObjectDataInput.class);
    bufferPool.returnInputBuffer(in);
    // lets see if the item was pushed on the queue
    assertEquals(1, bufferPool.inputQueue.size());
    // we need to make sure clear was called
    verify(in, times(1)).clear();
}
Also used : BufferObjectDataInput(com.hazelcast.nio.BufferObjectDataInput) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 22 with BufferObjectDataInput

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

the class OperationSerializationTest method copy.

private Operation copy(Operation op) {
    try {
        BufferObjectDataOutput out = serializationService.createObjectDataOutput(1000);
        op.writeData(out);
        BufferObjectDataInput in = serializationService.createObjectDataInput(out.toByteArray());
        Constructor constructor = op.getClass().getConstructor();
        constructor.setAccessible(true);
        Operation copiedOperation = (Operation) constructor.newInstance();
        copiedOperation.readData(in);
        return copiedOperation;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : BufferObjectDataOutput(com.hazelcast.nio.BufferObjectDataOutput) Constructor(java.lang.reflect.Constructor) BufferObjectDataInput(com.hazelcast.nio.BufferObjectDataInput) IOException(java.io.IOException)

Example 23 with BufferObjectDataInput

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

the class PortablePositionNavigator method createPositionForPortableFieldAccess.

private static PortablePosition createPositionForPortableFieldAccess(PortableNavigatorContext ctx, PortablePathCursor path) throws IOException {
    BufferObjectDataInput in = ctx.getIn();
    in.position(getStreamPositionOfTheField(ctx));
    // read and validate portable properties
    boolean nil = in.readBoolean();
    int factoryId = in.readInt();
    int classId = in.readInt();
    int streamPosition = in.position();
    validateFactoryAndClass(ctx.getCurrentFieldDefinition(), factoryId, classId, path.path());
    return PortablePositionFactory.createSinglePortablePosition(ctx.getCurrentFieldDefinition(), streamPosition, factoryId, classId, nil, path.isLastToken());
}
Also used : BufferObjectDataInput(com.hazelcast.nio.BufferObjectDataInput)

Example 24 with BufferObjectDataInput

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

the class PortablePositionNavigator method navigateContextToNextPortableTokenFromPortableArrayCell.

// this navigation always succeeds since the caller validates if the index is inbound
private static void navigateContextToNextPortableTokenFromPortableArrayCell(PortableNavigatorContext ctx, PortablePathCursor path, int index) throws IOException {
    BufferObjectDataInput in = ctx.getIn();
    // find the array field position that's stored in the fieldDefinition int the context and navigate to it
    int pos = getStreamPositionOfTheField(ctx);
    in.position(pos);
    // read array length and ignore
    in.readInt();
    // read factory and class Id and validate if it's the same as expected in the fieldDefinition
    int factoryId = in.readInt();
    int classId = in.readInt();
    validateFactoryAndClass(ctx.getCurrentFieldDefinition(), factoryId, classId, path.path());
    // calculate the offset of the cell given by the index
    final int cellOffset = in.position() + index * Bits.INT_SIZE_IN_BYTES;
    in.position(cellOffset);
    // read the position of the portable addressed in this array cell (array contains portable position only)
    int portablePosition = in.readInt();
    // navigate to portable position and read it's version
    in.position(portablePosition);
    int versionId = in.readInt();
    // initialise context with the given portable field for further navigation
    ctx.advanceContextToNextPortableToken(factoryId, classId, versionId);
}
Also used : BufferObjectDataInput(com.hazelcast.nio.BufferObjectDataInput)

Example 25 with BufferObjectDataInput

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

the class AbstractSerializationService method toObject.

@Override
public final <T> T toObject(final Object object) {
    if (!(object instanceof Data)) {
        return (T) object;
    }
    Data data = (Data) object;
    if (isNullData(data)) {
        return null;
    }
    BufferPool pool = bufferPoolThreadLocal.get();
    BufferObjectDataInput in = pool.takeInputBuffer(data);
    try {
        ClassLocator.onStartDeserialization();
        final int typeId = data.getType();
        final SerializerAdapter serializer = serializerFor(typeId);
        if (serializer == null) {
            if (active) {
                throw newHazelcastSerializationException(typeId);
            }
            throw new HazelcastInstanceNotActiveException();
        }
        Object obj = serializer.read(in);
        if (managedContext != null) {
            obj = managedContext.initialize(obj);
        }
        return (T) obj;
    } catch (Throwable e) {
        throw handleException(e);
    } finally {
        ClassLocator.onFinishDeserialization();
        pool.returnInputBuffer(in);
    }
}
Also used : HazelcastInstanceNotActiveException(com.hazelcast.core.HazelcastInstanceNotActiveException) BufferPool(com.hazelcast.internal.serialization.impl.bufferpool.BufferPool) SerializationUtil.createSerializerAdapter(com.hazelcast.internal.serialization.impl.SerializationUtil.createSerializerAdapter) SerializationUtil.isNullData(com.hazelcast.internal.serialization.impl.SerializationUtil.isNullData) Data(com.hazelcast.nio.serialization.Data) BufferObjectDataInput(com.hazelcast.nio.BufferObjectDataInput)

Aggregations

BufferObjectDataInput (com.hazelcast.nio.BufferObjectDataInput)25 QuickTest (com.hazelcast.test.annotation.QuickTest)11 Test (org.junit.Test)11 ParallelTest (com.hazelcast.test.annotation.ParallelTest)10 BufferObjectDataOutput (com.hazelcast.nio.BufferObjectDataOutput)6 Data (com.hazelcast.nio.serialization.Data)6 ClassDefinition (com.hazelcast.nio.serialization.ClassDefinition)3 IOException (java.io.IOException)3 HazelcastInstanceNotActiveException (com.hazelcast.core.HazelcastInstanceNotActiveException)2 InternalSerializationService (com.hazelcast.internal.serialization.InternalSerializationService)2 DefaultSerializationServiceBuilder (com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder)2 HeapData (com.hazelcast.internal.serialization.impl.HeapData)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 HazelcastSerializationException (com.hazelcast.nio.serialization.HazelcastSerializationException)2 CustomSerializationTest (com.hazelcast.nio.serialization.CustomSerializationTest)1 MorphingBasePortable (com.hazelcast.nio.serialization.MorphingBasePortable)1 MorphingPortable (com.hazelcast.nio.serialization.MorphingPortable)1 PortableFactory (com.hazelcast.nio.serialization.PortableFactory)1