Search in sources :

Example 41 with HazelcastSerializationException

use of com.hazelcast.nio.serialization.HazelcastSerializationException in project hazelcast by hazelcast.

the class DefaultPortableReader method readPortable.

@Override
@Nullable
@SuppressWarnings("unchecked")
public Portable readPortable(@Nonnull String fieldName) throws IOException {
    int currentPos = in.position();
    try {
        FieldDefinition fd = cd.getField(fieldName);
        if (fd == null) {
            throw throwUnknownFieldException(fieldName);
        }
        if (fd.getType() != FieldType.PORTABLE) {
            throw new HazelcastSerializationException("Not a Portable field: " + fieldName);
        }
        int pos = readPosition(fd);
        in.position(pos);
        boolean isNull = in.readBoolean();
        int factoryId = in.readInt();
        int classId = in.readInt();
        checkFactoryAndClass(fd, factoryId, classId);
        if (!isNull) {
            return serializer.read(in, factoryId, classId);
        }
        return null;
    } finally {
        in.position(currentPos);
    }
}
Also used : HazelcastSerializationException(com.hazelcast.nio.serialization.HazelcastSerializationException) FieldDefinition(com.hazelcast.nio.serialization.FieldDefinition) Nullable(javax.annotation.Nullable)

Example 42 with HazelcastSerializationException

use of com.hazelcast.nio.serialization.HazelcastSerializationException in project hazelcast by hazelcast.

the class DefaultPortableReader method readPortableArray.

@Override
@Nullable
public Portable[] readPortableArray(@Nonnull String fieldName) throws IOException {
    int currentPos = in.position();
    try {
        FieldDefinition fd = cd.getField(fieldName);
        if (fd == null) {
            throw throwUnknownFieldException(fieldName);
        }
        if (fd.getType() != FieldType.PORTABLE_ARRAY) {
            throw new HazelcastSerializationException("Not a Portable array field: " + fieldName);
        }
        int position = readPosition(fd);
        in.position(position);
        int len = in.readInt();
        int factoryId = in.readInt();
        int classId = in.readInt();
        if (len == Bits.NULL_ARRAY_LENGTH) {
            return null;
        }
        checkFactoryAndClass(fd, factoryId, classId);
        Portable[] portables = new Portable[len];
        if (len > 0) {
            int offset = in.position();
            for (int i = 0; i < len; i++) {
                int start = in.readInt(offset + i * Bits.INT_SIZE_IN_BYTES);
                in.position(start);
                portables[i] = serializer.read(in, factoryId, classId);
            }
        }
        return portables;
    } finally {
        in.position(currentPos);
    }
}
Also used : Portable(com.hazelcast.nio.serialization.Portable) HazelcastSerializationException(com.hazelcast.nio.serialization.HazelcastSerializationException) FieldDefinition(com.hazelcast.nio.serialization.FieldDefinition) Nullable(javax.annotation.Nullable)

Example 43 with HazelcastSerializationException

use of com.hazelcast.nio.serialization.HazelcastSerializationException in project hazelcast by hazelcast.

the class PortableGenericRecordBuilder method set.

private GenericRecordBuilder set(@Nonnull String fieldName, Object value, FieldType fieldType) {
    FieldDefinition fd = check(fieldName, fieldType);
    int index = fd.getIndex();
    if (isSet[index]) {
        if (!isClone) {
            throw new HazelcastSerializationException("It is illegal to the overwrite the field");
        } else {
            throw new HazelcastSerializationException("Field can only overwritten once with `cloneWithBuilder`");
        }
    }
    objects[index] = value;
    isSet[index] = true;
    return this;
}
Also used : HazelcastSerializationException(com.hazelcast.nio.serialization.HazelcastSerializationException) FieldDefinition(com.hazelcast.nio.serialization.FieldDefinition)

Example 44 with HazelcastSerializationException

use of com.hazelcast.nio.serialization.HazelcastSerializationException in project hazelcast by hazelcast.

the class ClassDefinitionWriter method writeNullPortable.

@Override
public void writeNullPortable(@Nonnull String fieldName, int factoryId, int classId) {
    final ClassDefinition nestedClassDef = context.lookupClassDefinition(factoryId, classId, context.getVersion());
    if (nestedClassDef == null) {
        throw new HazelcastSerializationException("Cannot write null portable without explicitly " + "registering class definition!");
    }
    builder.addPortableField(fieldName, nestedClassDef);
}
Also used : HazelcastSerializationException(com.hazelcast.nio.serialization.HazelcastSerializationException) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition)

Example 45 with HazelcastSerializationException

use of com.hazelcast.nio.serialization.HazelcastSerializationException in project hazelcast by hazelcast.

the class DefaultPortableWriter method setPosition.

private FieldDefinition setPosition(@Nonnull String fieldName, @Nonnull FieldType fieldType) throws IOException {
    if (raw) {
        throw new HazelcastSerializationException("Cannot write Portable fields after getRawDataOutput() is called!");
    }
    FieldDefinition fd = cd.getField(fieldName);
    if (fd == null) {
        throw new HazelcastSerializationException("Invalid field name: '" + fieldName + "' for ClassDefinition {id: " + cd.getClassId() + ", version: " + cd.getVersion() + "}");
    }
    if (writtenFields.add(fieldName)) {
        int pos = out.position();
        int index = fd.getIndex();
        out.writeInt(offset + index * INT_SIZE_IN_BYTES, pos);
        out.writeShort(fieldName.length());
        out.writeBytes(fieldName);
        out.writeByte(fieldType.getId());
    } else {
        throw new HazelcastSerializationException("Field '" + fieldName + "' has already been written!");
    }
    return fd;
}
Also used : HazelcastSerializationException(com.hazelcast.nio.serialization.HazelcastSerializationException) FieldDefinition(com.hazelcast.nio.serialization.FieldDefinition)

Aggregations

HazelcastSerializationException (com.hazelcast.nio.serialization.HazelcastSerializationException)50 ClassDefinition (com.hazelcast.nio.serialization.ClassDefinition)10 FieldDefinition (com.hazelcast.nio.serialization.FieldDefinition)10 IOException (java.io.IOException)9 QuickTest (com.hazelcast.test.annotation.QuickTest)8 Test (org.junit.Test)8 Config (com.hazelcast.config.Config)5 HazelcastInstance (com.hazelcast.core.HazelcastInstance)5 ClassDefinitionBuilder (com.hazelcast.nio.serialization.ClassDefinitionBuilder)5 ClientConfig (com.hazelcast.client.config.ClientConfig)4 JavaSerializationFilterConfig (com.hazelcast.config.JavaSerializationFilterConfig)4 SerializationUtil.createSerializerAdapter (com.hazelcast.internal.serialization.impl.SerializationUtil.createSerializerAdapter)4 Portable (com.hazelcast.nio.serialization.Portable)4 TestDeserialized (example.serialization.TestDeserialized)4 Map (java.util.Map)4 HazelcastException (com.hazelcast.core.HazelcastException)3 FieldKind (com.hazelcast.nio.serialization.FieldKind)3 HashMap (java.util.HashMap)3 HazelcastInstanceNotActiveException (com.hazelcast.core.HazelcastInstanceNotActiveException)2 BufferObjectDataInput (com.hazelcast.internal.nio.BufferObjectDataInput)2