Search in sources :

Example 1 with BufferPool

use of com.hazelcast.internal.serialization.impl.bufferpool.BufferPool 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 notActiveExceptionSupplier.get();
        }
        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 : BufferPool(com.hazelcast.internal.serialization.impl.bufferpool.BufferPool) SerializationUtil.createSerializerAdapter(com.hazelcast.internal.serialization.impl.SerializationUtil.createSerializerAdapter) CompactWithSchemaStreamSerializerAdapter(com.hazelcast.internal.serialization.impl.compact.CompactWithSchemaStreamSerializerAdapter) CompactStreamSerializerAdapter(com.hazelcast.internal.serialization.impl.compact.CompactStreamSerializerAdapter) SerializationUtil.isNullData(com.hazelcast.internal.serialization.impl.SerializationUtil.isNullData) Data(com.hazelcast.internal.serialization.Data) BufferObjectDataInput(com.hazelcast.internal.nio.BufferObjectDataInput)

Example 2 with BufferPool

use of com.hazelcast.internal.serialization.impl.bufferpool.BufferPool in project hazelcast by hazelcast.

the class AbstractSerializationService method toBytes.

@Override
public byte[] toBytes(int padding, Object obj, PartitioningStrategy strategy) {
    checkNotNull(obj);
    BufferPool pool = bufferPoolThreadLocal.get();
    BufferObjectDataOutput out = pool.takeOutputBuffer();
    try {
        SerializerAdapter serializer = serializerFor(obj);
        int partitionHash = calculatePartitionHash(obj, strategy);
        out.writeInt(partitionHash, ByteOrder.BIG_ENDIAN);
        out.writeInt(serializer.getTypeId(), ByteOrder.BIG_ENDIAN);
        serializer.write(out, obj);
        return out.toByteArray(padding);
    } catch (Throwable e) {
        throw handleSerializeException(obj, e);
    } finally {
        pool.returnOutputBuffer(out);
    }
}
Also used : BufferObjectDataOutput(com.hazelcast.nio.BufferObjectDataOutput) BufferPool(com.hazelcast.internal.serialization.impl.bufferpool.BufferPool) SerializationUtil.createSerializerAdapter(com.hazelcast.internal.serialization.impl.SerializationUtil.createSerializerAdapter)

Example 3 with BufferPool

use of com.hazelcast.internal.serialization.impl.bufferpool.BufferPool 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;
    }
    final int typeId = data.getType();
    final SerializerAdapter serializer = serializerFor(typeId);
    if (serializer == null) {
        if (active) {
            throw newHazelcastSerializationException(typeId);
        }
        throw notActiveExceptionSupplier.get();
    }
    Object obj = null;
    BufferPool pool = bufferPoolThreadLocal.get();
    BufferObjectDataInput in = pool.takeInputBuffer(data);
    try {
        ClassLocator.onStartDeserialization();
        obj = serializer.read(in);
        if (managedContext != null) {
            obj = managedContext.initialize(obj);
        }
        return (T) obj;
    } catch (Throwable e) {
        throw handleException(e);
    } finally {
        ClassLocator.onFinishDeserialization();
        serializer.conditionallyReturnInputBufferToPool(obj, in, pool);
    }
}
Also used : BufferPool(com.hazelcast.internal.serialization.impl.bufferpool.BufferPool) SerializationUtil.createSerializerAdapter(com.hazelcast.internal.serialization.impl.SerializationUtil.createSerializerAdapter) CompactWithSchemaStreamSerializerAdapter(com.hazelcast.internal.serialization.impl.compact.CompactWithSchemaStreamSerializerAdapter) CompactStreamSerializerAdapter(com.hazelcast.internal.serialization.impl.compact.CompactStreamSerializerAdapter) SerializationUtil.isNullData(com.hazelcast.internal.serialization.impl.SerializationUtil.isNullData) Data(com.hazelcast.internal.serialization.Data) BufferObjectDataInput(com.hazelcast.internal.nio.BufferObjectDataInput)

Example 4 with BufferPool

use of com.hazelcast.internal.serialization.impl.bufferpool.BufferPool in project hazelcast by hazelcast.

the class AbstractSerializationService method toBytes.

private byte[] toBytes(Object obj, int leftPadding, boolean writeHash, PartitioningStrategy strategy, ByteOrder serializerTypeIdByteOrder, boolean includeSchema) {
    checkNotNull(obj);
    checkNotNull(serializerTypeIdByteOrder);
    BufferPool pool = bufferPoolThreadLocal.get();
    BufferObjectDataOutput out = pool.takeOutputBuffer();
    try {
        out.position(leftPadding);
        SerializerAdapter serializer = serializerFor(obj, includeSchema);
        if (writeHash) {
            int partitionHash = calculatePartitionHash(obj, strategy);
            out.writeInt(partitionHash, BIG_ENDIAN);
        }
        out.writeInt(serializer.getTypeId(), serializerTypeIdByteOrder);
        serializer.write(out, obj);
        return out.toByteArray();
    } catch (Throwable e) {
        throw handleSerializeException(obj, e);
    } finally {
        pool.returnOutputBuffer(out);
    }
}
Also used : BufferObjectDataOutput(com.hazelcast.internal.nio.BufferObjectDataOutput) BufferPool(com.hazelcast.internal.serialization.impl.bufferpool.BufferPool) SerializationUtil.createSerializerAdapter(com.hazelcast.internal.serialization.impl.SerializationUtil.createSerializerAdapter) CompactWithSchemaStreamSerializerAdapter(com.hazelcast.internal.serialization.impl.compact.CompactWithSchemaStreamSerializerAdapter) CompactStreamSerializerAdapter(com.hazelcast.internal.serialization.impl.compact.CompactStreamSerializerAdapter)

Aggregations

SerializationUtil.createSerializerAdapter (com.hazelcast.internal.serialization.impl.SerializationUtil.createSerializerAdapter)4 BufferPool (com.hazelcast.internal.serialization.impl.bufferpool.BufferPool)4 CompactStreamSerializerAdapter (com.hazelcast.internal.serialization.impl.compact.CompactStreamSerializerAdapter)3 CompactWithSchemaStreamSerializerAdapter (com.hazelcast.internal.serialization.impl.compact.CompactWithSchemaStreamSerializerAdapter)3 BufferObjectDataInput (com.hazelcast.internal.nio.BufferObjectDataInput)2 Data (com.hazelcast.internal.serialization.Data)2 SerializationUtil.isNullData (com.hazelcast.internal.serialization.impl.SerializationUtil.isNullData)2 BufferObjectDataOutput (com.hazelcast.internal.nio.BufferObjectDataOutput)1 BufferObjectDataOutput (com.hazelcast.nio.BufferObjectDataOutput)1