use of org.apache.ignite.binary.BinaryObjectException in project ignite by apache.
the class BinaryMarshaller method unmarshal0.
/** {@inheritDoc} */
@Override
protected <T> T unmarshal0(InputStream in, @Nullable ClassLoader clsLdr) throws IgniteCheckedException {
ByteArrayOutputStream buf = new ByteArrayOutputStream();
// returns number of bytes remaining.
try {
byte[] arr = new byte[4096];
int cnt;
while ((cnt = in.read(arr)) != -1) buf.write(arr, 0, cnt);
buf.flush();
return impl.deserialize(buf.toByteArray(), clsLdr);
} catch (IOException e) {
throw new BinaryObjectException("Failed to unmarshal the object from InputStream", e);
}
}
use of org.apache.ignite.binary.BinaryObjectException in project ignite by apache.
the class BinaryObjectExImpl method toString.
/** {@inheritDoc} */
@Override
public String toString() {
try {
BinaryReaderHandles ctx = new BinaryReaderHandles();
ctx.put(start(), this);
return toString(ctx, new IdentityHashMap<BinaryObject, Integer>());
} catch (BinaryObjectException e) {
throw new IgniteException("Failed to create string representation of binary object.", e);
}
}
use of org.apache.ignite.binary.BinaryObjectException in project ignite by apache.
the class BinaryObjectBuilderImpl method checkMetadata.
/**
* Checks metadata when a BinaryObject is being serialized.
*
* @param meta Current metadata.
* @param fieldsMeta Map holding metadata information that has to be updated.
* @param newVal Field value being serialized.
* @param name Field name.
* @param fieldId Field ID.
*/
private Map<String, BinaryFieldMetadata> checkMetadata(BinaryType meta, Map<String, BinaryFieldMetadata> fieldsMeta, Object newVal, String name, int fieldId) {
String oldFldTypeName = meta == null ? null : meta.fieldTypeName(name);
boolean nullFieldVal = false;
int newFldTypeId;
if (newVal instanceof BinaryValueWithType) {
newFldTypeId = ((BinaryValueWithType) newVal).typeId();
if (((BinaryValueWithType) newVal).value() == null)
nullFieldVal = true;
} else // Detect Enum and Enum array type.
if (newVal instanceof BinaryEnumObjectImpl)
newFldTypeId = GridBinaryMarshaller.ENUM;
else if (newVal.getClass().isArray() && newVal.getClass().getComponentType() == BinaryObject.class) {
BinaryObject[] arr = (BinaryObject[]) newVal;
newFldTypeId = arr.length > 0 && arr[0] instanceof BinaryEnumObjectImpl ? GridBinaryMarshaller.ENUM_ARR : GridBinaryMarshaller.OBJ_ARR;
} else
newFldTypeId = BinaryUtils.typeByClass(newVal.getClass());
if (oldFldTypeName == null) {
// It's a new field, we have to add it to metadata.
if (fieldsMeta == null) {
if (BinaryUtils.FIELDS_SORTED_ORDER)
fieldsMeta = new TreeMap<>();
else
fieldsMeta = new LinkedHashMap<>();
}
fieldsMeta.put(name, new BinaryFieldMetadata(newFldTypeId, fieldId));
} else if (!nullFieldVal) {
String newFldTypeName = BinaryUtils.fieldTypeName(newFldTypeId);
if (!F.eq(newFldTypeName, oldFldTypeName)) {
throw new BinaryObjectException("Wrong value has been set [" + "typeName=" + (typeName == null ? meta.typeName() : typeName) + ", fieldName=" + name + ", fieldType=" + oldFldTypeName + ", assignedValueType=" + newFldTypeName + ']');
}
}
return fieldsMeta;
}
use of org.apache.ignite.binary.BinaryObjectException in project ignite by apache.
the class BinaryContext method registerUserClassDescriptor.
/**
* Creates and registers {@link BinaryClassDescriptor} for the given user {@code class}.
*
* @param desc Old descriptor that should be re-registered.
* @return Class descriptor.
*/
private BinaryClassDescriptor registerUserClassDescriptor(BinaryClassDescriptor desc) {
boolean registered;
try {
registered = marshCtx.registerClassName(JAVA_ID, desc.typeId(), desc.describedClass().getName());
} catch (IgniteCheckedException e) {
throw new BinaryObjectException("Failed to register class.", e);
}
if (registered) {
BinarySerializer serializer = desc.initialSerializer();
if (serializer == null)
serializer = serializerForClass(desc.describedClass());
desc = new BinaryClassDescriptor(this, desc.describedClass(), true, desc.typeId(), desc.typeName(), desc.affFieldKeyName(), desc.mapper(), serializer, true, true);
descByCls.put(desc.describedClass(), desc);
}
return desc;
}
use of org.apache.ignite.binary.BinaryObjectException in project ignite by apache.
the class BinaryContext method registerUserClassDescriptor.
/**
* Creates and registers {@link BinaryClassDescriptor} for the given user {@code class}.
*
* @param cls Class.
* @return Class descriptor.
*/
private BinaryClassDescriptor registerUserClassDescriptor(Class<?> cls, boolean deserialize) {
boolean registered;
final String clsName = cls.getName();
BinaryInternalMapper mapper = userTypeMapper(clsName);
final String typeName = mapper.typeName(clsName);
final int typeId = mapper.typeId(clsName);
try {
registered = marshCtx.registerClassName(JAVA_ID, typeId, cls.getName());
} catch (IgniteCheckedException e) {
throw new BinaryObjectException("Failed to register class.", e);
}
BinarySerializer serializer = serializerForClass(cls);
String affFieldName = affinityFieldName(cls);
BinaryClassDescriptor desc = new BinaryClassDescriptor(this, cls, true, typeId, typeName, affFieldName, mapper, serializer, true, registered);
if (!deserialize)
metaHnd.addMeta(typeId, new BinaryMetadata(typeId, typeName, desc.fieldsMeta(), affFieldName, null, desc.isEnum(), cls.isEnum() ? enumMap(cls) : null).wrap(this));
descByCls.put(cls, desc);
typeId2Mapper.putIfAbsent(typeId, mapper);
return desc;
}
Aggregations