Search in sources :

Example 1 with BufferObjectDataInput

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

the class MulticastService method receive.

private JoinMessage receive() {
    try {
        try {
            multicastSocket.receive(datagramPacketReceive);
        } catch (IOException ignore) {
            return null;
        }
        try {
            final byte[] data = datagramPacketReceive.getData();
            final int offset = datagramPacketReceive.getOffset();
            final BufferObjectDataInput input = node.getSerializationService().createObjectDataInput(data);
            input.position(offset);
            final byte packetVersion = input.readByte();
            if (packetVersion != Packet.VERSION) {
                logger.warning("Received a JoinRequest with a different packet version! This -> " + Packet.VERSION + ", Incoming -> " + packetVersion + ", Sender -> " + datagramPacketReceive.getAddress());
                return null;
            }
            try {
                return input.readObject();
            } finally {
                input.close();
            }
        } catch (Exception e) {
            if (e instanceof EOFException || e instanceof HazelcastSerializationException) {
                long now = System.currentTimeMillis();
                if (now - lastLoggedJoinSerializationFailure > JOIN_SERIALIZATION_ERROR_SUPPRESSION_MILLIS) {
                    lastLoggedJoinSerializationFailure = now;
                    logger.warning("Received a JoinRequest with an incompatible binary-format. " + "An old version of Hazelcast may be using the same multicast discovery port. " + "Are you running multiple Hazelcast clusters on this host? " + "(This message will be suppressed for 60 seconds). ");
                }
            } else {
                throw e;
            }
        }
    } catch (Exception e) {
        logger.warning(e);
    }
    return null;
}
Also used : HazelcastSerializationException(com.hazelcast.nio.serialization.HazelcastSerializationException) EOFException(java.io.EOFException) IOException(java.io.IOException) BufferObjectDataInput(com.hazelcast.nio.BufferObjectDataInput) HazelcastSerializationException(com.hazelcast.nio.serialization.HazelcastSerializationException) IOException(java.io.IOException) EOFException(java.io.EOFException)

Example 2 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, Class aClass) {
    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, aClass);
        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)

Example 3 with BufferObjectDataInput

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

the class BufferPoolImpl method takeInputBuffer.

@Override
public BufferObjectDataInput takeInputBuffer(Data data) {
    BufferObjectDataInput in = inputQueue.poll();
    if (in == null) {
        in = serializationService.createObjectDataInput((byte[]) null);
    }
    in.init(data.toByteArray(), HeapData.DATA_OFFSET);
    return in;
}
Also used : BufferObjectDataInput(com.hazelcast.nio.BufferObjectDataInput)

Example 4 with BufferObjectDataInput

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

the class PortablePositionNavigator method navigateContextToNextPortableTokenFromPortableField.

// returns true if managed to advance, false if advance failed due to null field
private static boolean navigateContextToNextPortableTokenFromPortableField(PortableNavigatorContext ctx) throws IOException {
    BufferObjectDataInput in = ctx.getIn();
    // find the field position that's stored in the fieldDefinition int the context and navigate to it
    int pos = getStreamPositionOfTheField(ctx);
    in.position(pos);
    // check if it's null, if so return false indicating that the navigation has failed
    boolean isNull = in.readBoolean();
    if (isNull) {
        return false;
    }
    // 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();
    int versionId = in.readInt();
    // initialise context with the given portable field for further navigation
    ctx.advanceContextToNextPortableToken(factoryId, classId, versionId);
    return true;
}
Also used : BufferObjectDataInput(com.hazelcast.nio.BufferObjectDataInput)

Example 5 with BufferObjectDataInput

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

the class PortablePositionNavigator method createPositionForSingleCellPrimitiveArrayAccess.

private static PortablePosition createPositionForSingleCellPrimitiveArrayAccess(PortableNavigatorContext ctx, PortablePathCursor path, int index) throws IOException {
    // assumes position.getIndex() >= 0
    BufferObjectDataInput in = ctx.getIn();
    in.position(getStreamPositionOfTheField(ctx));
    // read the array length and ignore it, it has been already validated
    in.readInt();
    int streamPosition;
    if (ctx.getCurrentFieldType() == FieldType.UTF || ctx.getCurrentFieldType() == FieldType.UTF_ARRAY) {
        // UTF arrays actually need iterating over all elements to read the length of each element
        // it's impossible to dead-reckon about a cell's position
        int currentIndex = 0;
        while (index > currentIndex) {
            int indexElementLen = in.readInt();
            in.position(in.position() + indexElementLen);
            currentIndex++;
        }
        streamPosition = in.position();
    } else {
        // in primitive non-utf arrays we can dead-reckon about a cell's position
        streamPosition = in.position() + index * ctx.getCurrentFieldType().getSingleType().getTypeSize();
    }
    return PortablePositionFactory.createSinglePrimitivePosition(ctx.getCurrentFieldDefinition(), streamPosition, index, path.isLastToken());
}
Also used : 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