Search in sources :

Example 36 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)

Example 37 with BinaryObjectException

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

the class BinaryUtils method doReadStringArray.

/**
 * @return Value.
 * @throws BinaryObjectException In case of error.
 */
public static String[] doReadStringArray(BinaryInputStream in) throws BinaryObjectException {
    int len = in.readInt();
    String[] arr = new String[len];
    for (int i = 0; i < len; i++) {
        byte flag = in.readByte();
        if (flag == GridBinaryMarshaller.NULL)
            arr[i] = null;
        else {
            if (flag != GridBinaryMarshaller.STRING)
                throw new BinaryObjectException("Invalid flag value: " + flag);
            arr[i] = doReadString(in);
        }
    }
    return arr;
}
Also used : BinaryObjectException(org.apache.ignite.binary.BinaryObjectException)

Example 38 with BinaryObjectException

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

the class BinaryUtils method doReadDateArray.

/**
 * @return Value.
 * @throws BinaryObjectException In case of error.
 */
public static Date[] doReadDateArray(BinaryInputStream in) throws BinaryObjectException {
    int len = in.readInt();
    Date[] arr = new Date[len];
    for (int i = 0; i < len; i++) {
        byte flag = in.readByte();
        if (flag == GridBinaryMarshaller.NULL)
            arr[i] = null;
        else {
            if (flag != GridBinaryMarshaller.DATE)
                throw new BinaryObjectException("Invalid flag value: " + flag);
            arr[i] = doReadDate(in);
        }
    }
    return arr;
}
Also used : Date(java.util.Date) BinaryObjectException(org.apache.ignite.binary.BinaryObjectException)

Example 39 with BinaryObjectException

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

the class BinaryUtils method utf8BytesToStr.

/**
 * Reconstructs string from UTF-8 bytes.
 *
 * @param arr array Byte array.
 * @param off offset Offset in the array.
 * @param len length Byte array lenght.
 * @return string Resulting string.
 */
public static String utf8BytesToStr(byte[] arr, int off, int len) {
    int c, charArrCnt = 0, total = off + len;
    int c2, c3;
    char[] res = new char[len];
    // try reading ascii
    while (off < total) {
        c = (int) arr[off] & 0xff;
        if (c > 127)
            break;
        off++;
        res[charArrCnt++] = (char) c;
    }
    // read other
    while (off < total) {
        c = (int) arr[off] & 0xff;
        switch(c >> 4) {
            case 0:
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
            case 7:
                /* 0xxxxxxx*/
                off++;
                res[charArrCnt++] = (char) c;
                break;
            case 12:
            case 13:
                /* 110x xxxx   10xx xxxx*/
                off += 2;
                if (off > total)
                    throw new BinaryObjectException("Malformed input: partial character at end");
                c2 = (int) arr[off - 1];
                if ((c2 & 0xC0) != 0x80)
                    throw new BinaryObjectException("Malformed input around byte: " + off);
                res[charArrCnt++] = (char) (((c & 0x1F) << 6) | (c2 & 0x3F));
                break;
            case 14:
                /* 1110 xxxx  10xx xxxx  10xx xxxx */
                off += 3;
                if (off > total)
                    throw new BinaryObjectException("Malformed input: partial character at end");
                c2 = (int) arr[off - 2];
                c3 = (int) arr[off - 1];
                if (((c2 & 0xC0) != 0x80) || ((c3 & 0xC0) != 0x80))
                    throw new BinaryObjectException("Malformed input around byte: " + (off - 1));
                res[charArrCnt++] = (char) (((c & 0x0F) << 12) | ((c2 & 0x3F) << 6) | (c3 & 0x3F));
                break;
            default:
                /* 10xx xxxx,  1111 xxxx */
                throw new BinaryObjectException("Malformed input around byte: " + off);
        }
    }
    return len == charArrCnt ? new String(res) : new String(res, 0, charArrCnt);
}
Also used : BinaryObjectException(org.apache.ignite.binary.BinaryObjectException)

Example 40 with BinaryObjectException

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

the class BinaryUtils method doReadTimeArray.

/**
 * @return Value.
 * @throws BinaryObjectException In case of error.
 */
public static Time[] doReadTimeArray(BinaryInputStream in) throws BinaryObjectException {
    int len = in.readInt();
    Time[] arr = new Time[len];
    for (int i = 0; i < len; i++) {
        byte flag = in.readByte();
        if (flag == GridBinaryMarshaller.NULL)
            arr[i] = null;
        else {
            if (flag != GridBinaryMarshaller.TIME)
                throw new BinaryObjectException("Invalid flag value: " + flag);
            arr[i] = doReadTime(in);
        }
    }
    return arr;
}
Also used : Time(java.sql.Time) BinaryObjectException(org.apache.ignite.binary.BinaryObjectException)

Aggregations

BinaryObjectException (org.apache.ignite.binary.BinaryObjectException)50 BinaryObject (org.apache.ignite.binary.BinaryObject)10 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)9 Date (java.util.Date)5 BinaryType (org.apache.ignite.binary.BinaryType)5 HashMap (java.util.HashMap)4 LinkedHashMap (java.util.LinkedHashMap)4 Map (java.util.Map)4 TreeMap (java.util.TreeMap)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 IOException (java.io.IOException)3 Time (java.sql.Time)3 Timestamp (java.sql.Timestamp)3 BinaryMetadata (org.apache.ignite.internal.binary.BinaryMetadata)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 BigDecimal (java.math.BigDecimal)2 BigInteger (java.math.BigInteger)2 UUID (java.util.UUID)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 IgniteException (org.apache.ignite.IgniteException)2