Search in sources :

Example 21 with BinaryObjectException

use of org.apache.ignite.binary.BinaryObjectException in project ignite by apache.

the class BinaryReaderExImpl method deserialize0.

/**
     * @return Deserialized object.
     * @throws BinaryObjectException If failed.
     */
@Nullable
private Object deserialize0() throws BinaryObjectException {
    Object obj;
    byte flag = in.readByte();
    switch(flag) {
        case NULL:
            obj = null;
            break;
        case HANDLE:
            int handlePos = start - in.readInt();
            obj = getHandle(handlePos);
            if (obj == null) {
                int retPos = in.position();
                streamPosition(handlePos);
                obj = BinaryUtils.doReadObject(in, ctx, ldr, this);
                streamPosition(retPos);
            }
            break;
        case OBJ:
            if (desc == null)
                desc = ctx.descriptorForTypeId(userType, typeId, ldr, true);
            streamPosition(dataStart);
            if (desc == null)
                throw new BinaryInvalidTypeException("Unknown type ID: " + typeId);
            obj = desc.read(this);
            streamPosition(footerStart + footerLen);
            break;
        case BYTE:
            obj = in.readByte();
            break;
        case SHORT:
            obj = in.readShort();
            break;
        case INT:
            obj = in.readInt();
            break;
        case LONG:
            obj = in.readLong();
            break;
        case FLOAT:
            obj = in.readFloat();
            break;
        case DOUBLE:
            obj = in.readDouble();
            break;
        case CHAR:
            obj = in.readChar();
            break;
        case BOOLEAN:
            obj = in.readBoolean();
            break;
        case DECIMAL:
            obj = BinaryUtils.doReadDecimal(in);
            break;
        case STRING:
            obj = BinaryUtils.doReadString(in);
            break;
        case UUID:
            obj = BinaryUtils.doReadUuid(in);
            break;
        case DATE:
            obj = BinaryUtils.doReadDate(in);
            break;
        case TIMESTAMP:
            obj = BinaryUtils.doReadTimestamp(in);
            break;
        case TIME:
            obj = BinaryUtils.doReadTime(in);
            break;
        case BYTE_ARR:
            obj = BinaryUtils.doReadByteArray(in);
            break;
        case SHORT_ARR:
            obj = BinaryUtils.doReadShortArray(in);
            break;
        case INT_ARR:
            obj = BinaryUtils.doReadIntArray(in);
            break;
        case LONG_ARR:
            obj = BinaryUtils.doReadLongArray(in);
            break;
        case FLOAT_ARR:
            obj = BinaryUtils.doReadFloatArray(in);
            break;
        case DOUBLE_ARR:
            obj = BinaryUtils.doReadDoubleArray(in);
            break;
        case CHAR_ARR:
            obj = BinaryUtils.doReadCharArray(in);
            break;
        case BOOLEAN_ARR:
            obj = BinaryUtils.doReadBooleanArray(in);
            break;
        case DECIMAL_ARR:
            obj = BinaryUtils.doReadDecimalArray(in);
            break;
        case STRING_ARR:
            obj = BinaryUtils.doReadStringArray(in);
            break;
        case UUID_ARR:
            obj = BinaryUtils.doReadUuidArray(in);
            break;
        case DATE_ARR:
            obj = BinaryUtils.doReadDateArray(in);
            break;
        case TIMESTAMP_ARR:
            obj = BinaryUtils.doReadTimestampArray(in);
            break;
        case TIME_ARR:
            obj = BinaryUtils.doReadTimeArray(in);
            break;
        case OBJ_ARR:
            obj = BinaryUtils.doReadObjectArray(in, ctx, ldr, this, true);
            break;
        case COL:
            obj = BinaryUtils.doReadCollection(in, ctx, ldr, this, true, null);
            break;
        case MAP:
            obj = BinaryUtils.doReadMap(in, ctx, ldr, this, true, null);
            break;
        case BINARY_OBJ:
            obj = BinaryUtils.doReadBinaryObject(in, ctx, false);
            ((BinaryObjectImpl) obj).context(ctx);
            if (!GridBinaryMarshaller.KEEP_BINARIES.get())
                obj = ((BinaryObject) obj).deserialize();
            break;
        case ENUM:
            obj = BinaryUtils.doReadEnum(in, BinaryUtils.doReadClass(in, ctx, ldr));
            break;
        case ENUM_ARR:
            obj = BinaryUtils.doReadEnumArray(in, ctx, ldr, BinaryUtils.doReadClass(in, ctx, ldr));
            break;
        case BINARY_ENUM:
            obj = BinaryUtils.doReadBinaryEnum(in, ctx);
            if (!GridBinaryMarshaller.KEEP_BINARIES.get())
                obj = ((BinaryObject) obj).deserialize();
            break;
        case CLASS:
            obj = BinaryUtils.doReadClass(in, ctx, ldr);
            break;
        case PROXY:
            obj = BinaryUtils.doReadProxy(in, ctx, ldr, this);
            break;
        case OPTM_MARSH:
            obj = BinaryUtils.doReadOptimized(in, ctx, ldr);
            break;
        default:
            throw new BinaryObjectException("Invalid flag value: " + flag);
    }
    return obj;
}
Also used : BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryInvalidTypeException(org.apache.ignite.binary.BinaryInvalidTypeException) BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryObjectException(org.apache.ignite.binary.BinaryObjectException) Nullable(org.jetbrains.annotations.Nullable)

Example 22 with BinaryObjectException

use of org.apache.ignite.binary.BinaryObjectException in project ignite by apache.

the class BinaryWriterExImpl method writeFieldId.

/**
     * @param fieldName Field name.
     * @throws org.apache.ignite.binary.BinaryObjectException If fields are not allowed.
     */
private void writeFieldId(String fieldName) throws BinaryObjectException {
    A.notNull(fieldName, "fieldName");
    if (rawOffPos != 0)
        throw new BinaryObjectException("Individual field can't be written after raw writer is acquired.");
    if (mapper == null)
        mapper = ctx.userTypeMapper(typeId);
    assert mapper != null;
    int id = mapper.fieldId(typeId, fieldName);
    writeFieldId(id);
}
Also used : BinaryObjectException(org.apache.ignite.binary.BinaryObjectException)

Example 23 with BinaryObjectException

use of org.apache.ignite.binary.BinaryObjectException in project ignite by apache.

the class BinaryBuilderReader method parseValue.

/**
     * @return Parsed value.
     */
public Object parseValue() {
    int valPos = pos;
    byte type = arr[pos++];
    int plainLazyValLen;
    boolean modifiableLazyVal = false;
    switch(type) {
        case GridBinaryMarshaller.NULL:
            return null;
        case GridBinaryMarshaller.HANDLE:
            {
                int objStart = pos - 1 - readInt();
                BinaryObjectBuilderImpl res = objMap.get(objStart);
                if (res == null) {
                    res = new BinaryObjectBuilderImpl(new BinaryBuilderReader(this, objStart), objStart);
                    objMap.put(objStart, res);
                }
                return res;
            }
        case GridBinaryMarshaller.OBJ:
            {
                pos--;
                BinaryObjectBuilderImpl res = objMap.get(pos);
                if (res == null) {
                    res = new BinaryObjectBuilderImpl(new BinaryBuilderReader(this, pos), pos);
                    objMap.put(pos, res);
                }
                pos += readInt(GridBinaryMarshaller.TOTAL_LEN_POS);
                return res;
            }
        case GridBinaryMarshaller.BYTE:
            return arr[pos++];
        case GridBinaryMarshaller.SHORT:
            {
                Object res = BinaryPrimitives.readShort(arr, pos);
                pos += 2;
                return res;
            }
        case GridBinaryMarshaller.INT:
            return readInt();
        case GridBinaryMarshaller.LONG:
            plainLazyValLen = 8;
            break;
        case GridBinaryMarshaller.FLOAT:
            plainLazyValLen = 4;
            break;
        case GridBinaryMarshaller.DOUBLE:
            plainLazyValLen = 8;
            break;
        case GridBinaryMarshaller.CHAR:
            plainLazyValLen = 2;
            break;
        case GridBinaryMarshaller.BOOLEAN:
            return arr[pos++] != 0;
        case GridBinaryMarshaller.DECIMAL:
            plainLazyValLen = /** scale */
            4 + /** mag len */
            4 + /** mag bytes count */
            readInt(4);
            break;
        case GridBinaryMarshaller.STRING:
            plainLazyValLen = 4 + readStringLength();
            break;
        case GridBinaryMarshaller.UUID:
            plainLazyValLen = 8 + 8;
            break;
        case GridBinaryMarshaller.DATE:
            plainLazyValLen = 8;
            break;
        case GridBinaryMarshaller.TIMESTAMP:
            plainLazyValLen = 8 + 4;
            break;
        case GridBinaryMarshaller.TIME:
            plainLazyValLen = 8;
            break;
        case GridBinaryMarshaller.BYTE_ARR:
            plainLazyValLen = 4 + readLength();
            modifiableLazyVal = true;
            break;
        case GridBinaryMarshaller.SHORT_ARR:
            plainLazyValLen = 4 + readLength() * 2;
            modifiableLazyVal = true;
            break;
        case GridBinaryMarshaller.INT_ARR:
            plainLazyValLen = 4 + readLength() * 4;
            modifiableLazyVal = true;
            break;
        case GridBinaryMarshaller.LONG_ARR:
            plainLazyValLen = 4 + readLength() * 8;
            modifiableLazyVal = true;
            break;
        case GridBinaryMarshaller.FLOAT_ARR:
            plainLazyValLen = 4 + readLength() * 4;
            modifiableLazyVal = true;
            break;
        case GridBinaryMarshaller.DOUBLE_ARR:
            plainLazyValLen = 4 + readLength() * 8;
            modifiableLazyVal = true;
            break;
        case GridBinaryMarshaller.CHAR_ARR:
            plainLazyValLen = 4 + readLength() * 2;
            modifiableLazyVal = true;
            break;
        case GridBinaryMarshaller.BOOLEAN_ARR:
            plainLazyValLen = 4 + readLength();
            modifiableLazyVal = true;
            break;
        case GridBinaryMarshaller.OBJ_ARR:
            return new BinaryObjectArrayLazyValue(this);
        case GridBinaryMarshaller.DATE_ARR:
            {
                int size = readInt();
                Date[] res = new Date[size];
                for (int i = 0; i < res.length; i++) {
                    byte flag = arr[pos++];
                    if (flag == GridBinaryMarshaller.NULL)
                        continue;
                    if (flag != GridBinaryMarshaller.DATE)
                        throw new BinaryObjectException("Invalid flag value: " + flag);
                    long time = BinaryPrimitives.readLong(arr, pos);
                    pos += 8;
                    res[i] = new Date(time);
                }
                return res;
            }
        case GridBinaryMarshaller.TIMESTAMP_ARR:
            {
                int size = readInt();
                Timestamp[] res = new Timestamp[size];
                for (int i = 0; i < res.length; i++) {
                    byte flag = arr[pos++];
                    if (flag == GridBinaryMarshaller.NULL)
                        continue;
                    if (flag != GridBinaryMarshaller.TIMESTAMP)
                        throw new BinaryObjectException("Invalid flag value: " + flag);
                    long time = BinaryPrimitives.readLong(arr, pos);
                    pos += 8;
                    int nano = BinaryPrimitives.readInt(arr, pos);
                    pos += 4;
                    Timestamp ts = new Timestamp(time);
                    ts.setNanos(ts.getNanos() + nano);
                    res[i] = ts;
                }
                return res;
            }
        case GridBinaryMarshaller.TIME_ARR:
            {
                int size = readInt();
                Time[] res = new Time[size];
                for (int i = 0; i < res.length; i++) {
                    byte flag = arr[pos++];
                    if (flag == GridBinaryMarshaller.NULL)
                        continue;
                    if (flag != GridBinaryMarshaller.TIME)
                        throw new BinaryObjectException("Invalid flag value: " + flag);
                    long time = BinaryPrimitives.readLong(arr, pos);
                    pos += 8;
                    res[i] = new Time(time);
                }
                return res;
            }
        case GridBinaryMarshaller.UUID_ARR:
        case GridBinaryMarshaller.STRING_ARR:
        case GridBinaryMarshaller.DECIMAL_ARR:
            {
                int size = readInt();
                for (int i = 0; i < size; i++) {
                    byte flag = arr[pos++];
                    if (flag == GridBinaryMarshaller.UUID)
                        pos += 8 + 8;
                    else if (flag == GridBinaryMarshaller.STRING)
                        pos += 4 + readStringLength();
                    else if (flag == GridBinaryMarshaller.DECIMAL) {
                        // scale value
                        pos += 4;
                        pos += 4 + readLength();
                    } else
                        assert flag == GridBinaryMarshaller.NULL;
                }
                return new BinaryModifiableLazyValue(this, valPos, pos - valPos);
            }
        case GridBinaryMarshaller.COL:
            {
                int size = readInt();
                byte colType = arr[pos++];
                switch(colType) {
                    case GridBinaryMarshaller.USER_COL:
                    case GridBinaryMarshaller.ARR_LIST:
                        return new BinaryLazyArrayList(this, size);
                    case GridBinaryMarshaller.LINKED_LIST:
                        return new BinaryLazyLinkedList(this, size);
                    case GridBinaryMarshaller.HASH_SET:
                    case GridBinaryMarshaller.LINKED_HASH_SET:
                        return new BinaryLazySet(this, size);
                }
                throw new BinaryObjectException("Unknown collection type: " + colType);
            }
        case GridBinaryMarshaller.MAP:
            return BinaryLazyMap.parseMap(this);
        case GridBinaryMarshaller.ENUM:
            return new BinaryBuilderEnum(this);
        case GridBinaryMarshaller.ENUM_ARR:
            return new BinaryEnumArrayLazyValue(this);
        case GridBinaryMarshaller.BINARY_OBJ:
            {
                int size = readInt();
                pos += size;
                int start = readInt();
                BinaryObjectImpl binaryObj = new BinaryObjectImpl(ctx, arr, pos - 4 - size + start);
                return new BinaryPlainBinaryObject(binaryObj);
            }
        case GridBinaryMarshaller.OPTM_MARSH:
            {
                final BinaryHeapInputStream bin = BinaryHeapInputStream.create(arr, pos);
                final Object obj = BinaryUtils.doReadOptimized(bin, ctx, U.resolveClassLoader(ctx.configuration()));
                pos = bin.position();
                return obj;
            }
        default:
            throw new BinaryObjectException("Invalid flag value: " + type);
    }
    BinaryAbstractLazyValue res;
    if (modifiableLazyVal)
        res = new BinaryModifiableLazyValue(this, valPos, 1 + plainLazyValLen);
    else
        res = new BinaryPlainLazyValue(this, valPos, 1 + plainLazyValLen);
    pos += plainLazyValLen;
    return res;
}
Also used : BinaryObjectImpl(org.apache.ignite.internal.binary.BinaryObjectImpl) Time(java.sql.Time) Timestamp(java.sql.Timestamp) Date(java.util.Date) BinaryHeapInputStream(org.apache.ignite.internal.binary.streams.BinaryHeapInputStream) BinaryObjectException(org.apache.ignite.binary.BinaryObjectException)

Example 24 with BinaryObjectException

use of org.apache.ignite.binary.BinaryObjectException in project ignite by apache.

the class BinaryBuilderReader method readString.

/**
     * Reads string.
     *
     * @return String.
     */
public String readString() {
    byte flag = readByte();
    if (flag == GridBinaryMarshaller.NULL)
        return null;
    if (flag != GridBinaryMarshaller.STRING)
        throw new BinaryObjectException("Failed to deserialize String.");
    int len = readInt();
    String str = new String(arr, pos, len, UTF_8);
    pos += len;
    return str;
}
Also used : BinaryObjectException(org.apache.ignite.binary.BinaryObjectException)

Example 25 with BinaryObjectException

use of org.apache.ignite.binary.BinaryObjectException in project ignite by apache.

the class BinaryUtils method mergeEnumValues.

/**
     * Merges enum value mappings and checks for conflicts.
     *
     * Possible conflicts:
     * - same name is used for different ordinal values.
     * - ordinal value is used more than once.
     *
     * @param typeName Name of the type.
     * @param oldValues Old enum value mapping.
     * @param newValues New enum value mapping.
     * @throws BinaryObjectException in case of name or value conflict.
     */
public static Map<String, Integer> mergeEnumValues(String typeName, @Nullable Map<String, Integer> oldValues, Map<String, Integer> newValues) throws BinaryObjectException {
    assert newValues != null;
    int size = (oldValues != null) ? oldValues.size() + newValues.size() : newValues.size();
    Map<Integer, String> revMap = new LinkedHashMap<>(size);
    Map<String, Integer> mergedMap = new LinkedHashMap<>(size);
    if (oldValues != null) {
        //assuming that old values were validated earlier once.
        for (Map.Entry<String, Integer> e : oldValues.entrySet()) {
            revMap.put(e.getValue(), e.getKey());
            mergedMap.put(e.getKey(), e.getValue());
        }
    }
    for (Map.Entry<String, Integer> e : newValues.entrySet()) {
        String prevName = revMap.put(e.getValue(), e.getKey());
        if (prevName != null && !prevName.equals(e.getKey()))
            throw new BinaryObjectException("Conflicting enum values. Name '" + e.getKey() + "' uses ordinal value (" + e.getValue() + ") that is also used for name '" + prevName + "' [typeName='" + typeName + "']");
        Integer prevVal = mergedMap.put(e.getKey(), e.getValue());
        if (prevVal != null && !prevVal.equals(e.getValue()))
            throw new BinaryObjectException("Conflicting enum values. Value (" + e.getValue() + ") has name '" + e.getKey() + "' that is also used for value '" + prevVal + "' [typeName='" + typeName + "']");
    }
    return mergedMap;
}
Also used : BigInteger(java.math.BigInteger) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) BinaryObjectException(org.apache.ignite.binary.BinaryObjectException) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

BinaryObjectException (org.apache.ignite.binary.BinaryObjectException)42 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)9 BinaryObject (org.apache.ignite.binary.BinaryObject)8 Date (java.util.Date)4 HashMap (java.util.HashMap)4 LinkedHashMap (java.util.LinkedHashMap)4 Map (java.util.Map)4 TreeMap (java.util.TreeMap)4 BinarySerializer (org.apache.ignite.binary.BinarySerializer)4 BinaryType (org.apache.ignite.binary.BinaryType)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 BigInteger (java.math.BigInteger)2 Time (java.sql.Time)2 Timestamp (java.sql.Timestamp)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 BinaryBasicIdMapper (org.apache.ignite.binary.BinaryBasicIdMapper)2 BinaryInvalidTypeException (org.apache.ignite.binary.BinaryInvalidTypeException)2 BinaryObjectBuilder (org.apache.ignite.binary.BinaryObjectBuilder)2 BinaryTypeConfiguration (org.apache.ignite.binary.BinaryTypeConfiguration)2 BinaryEnumObjectImpl (org.apache.ignite.internal.binary.BinaryEnumObjectImpl)2