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;
}
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;
}
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;
}
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);
}
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;
}
Aggregations