use of com.orientechnologies.orient.core.serialization.OSerializableStream in project orientdb by orientechnologies.
the class OStreamSerializerAnyStreamable method fromStream.
/**
* Re-Create any object if the class has a public constructor that accepts a String as unique parameter.
*/
public Object fromStream(final byte[] iStream) throws IOException {
if (iStream == null || iStream.length == 0)
// NULL VALUE
return null;
final int classNameSize = OBinaryProtocol.bytes2int(iStream);
if (classNameSize <= 0) {
final String message = "Class signature not found in ANY element: " + Arrays.toString(iStream);
OLogManager.instance().error(this, message);
throw new OSerializationException(message);
}
final String className = new String(iStream, 4, classNameSize, "UTF-8");
try {
final OSerializableStream stream;
// CHECK FOR ALIASES
if (className.equalsIgnoreCase("q"))
// QUERY
stream = new OSQLSynchQuery<Object>();
else if (className.equalsIgnoreCase("c"))
// SQL COMMAND
stream = new OCommandSQL();
else if (className.equalsIgnoreCase("s"))
// SCRIPT COMMAND
stream = new OCommandScript();
else
// CREATE THE OBJECT BY INVOKING THE EMPTY CONSTRUCTOR
stream = (OSerializableStream) Class.forName(className).newInstance();
return stream.fromStream(OArrays.copyOfRange(iStream, 4 + classNameSize, iStream.length));
} catch (Exception e) {
final String message = "Error on unmarshalling content. Class: " + className;
OLogManager.instance().error(this, message, e);
throw OException.wrapException(new OSerializationException(message), e);
}
}
use of com.orientechnologies.orient.core.serialization.OSerializableStream in project orientdb by orientechnologies.
the class OStringSerializerAnyStreamable method toStream.
/**
* Serialize the class name size + class name + object content
*
* @param iValue
*/
public StringBuilder toStream(final StringBuilder iOutput, Object iValue) {
if (iValue != null) {
if (!(iValue instanceof OSerializableStream))
throw new OSerializationException("Cannot serialize the object since it's not implements the OSerializableStream interface");
OSerializableStream stream = (OSerializableStream) iValue;
iOutput.append(iValue.getClass().getName());
iOutput.append(OStreamSerializerHelper.SEPARATOR);
iOutput.append(OBase64Utils.encodeBytes(stream.toStream()));
}
return iOutput;
}
use of com.orientechnologies.orient.core.serialization.OSerializableStream in project orientdb by orientechnologies.
the class OStringSerializerAnyStreamable method fromStream.
/**
* Re-Create any object if the class has a public constructor that accepts a String as unique parameter.
*/
public Object fromStream(final String iStream) {
if (iStream == null || iStream.length() == 0)
// NULL VALUE
return null;
OSerializableStream instance = null;
int propertyPos = iStream.indexOf(':');
int pos = iStream.indexOf(OStreamSerializerHelper.SEPARATOR);
if (pos < 0 || propertyPos > -1 && pos > propertyPos) {
instance = new ODocument();
pos = -1;
} else {
final String className = iStream.substring(0, pos);
try {
final Class<?> clazz = Class.forName(className);
instance = (OSerializableStream) clazz.newInstance();
} catch (Exception e) {
final String message = "Error on unmarshalling content. Class: " + className;
OLogManager.instance().error(this, message, e);
throw OException.wrapException(new OSerializationException(message), e);
}
}
instance.fromStream(OBase64Utils.decode(iStream.substring(pos + 1)));
return instance;
}
use of com.orientechnologies.orient.core.serialization.OSerializableStream in project orientdb by orientechnologies.
the class OStringSerializerEmbedded method toStream.
/**
* Serialize the class name size + class name + object content
*
* @param iValue
*/
public StringBuilder toStream(final StringBuilder iOutput, Object iValue) {
if (iValue != null) {
if (iValue instanceof ODocumentSerializable)
iValue = ((ODocumentSerializable) iValue).toDocument();
if (!(iValue instanceof OSerializableStream))
throw new OSerializationException("Cannot serialize the object since it's not implements the OSerializableStream interface");
OSerializableStream stream = (OSerializableStream) iValue;
iOutput.append(iValue.getClass().getName());
iOutput.append(OStreamSerializerHelper.SEPARATOR);
try {
iOutput.append(new String(stream.toStream(), "UTF-8"));
} catch (UnsupportedEncodingException e) {
throw OException.wrapException(new OSerializationException("Error serializing embedded object"), e);
}
}
return iOutput;
}
use of com.orientechnologies.orient.core.serialization.OSerializableStream in project orientdb by orientechnologies.
the class ORecordSerializerBinaryV0 method serializeValue.
@SuppressWarnings("unchecked")
public int serializeValue(final BytesContainer bytes, Object value, final OType type, final OType linkedType) {
int pointer = 0;
switch(type) {
case INTEGER:
case LONG:
case SHORT:
pointer = OVarIntSerializer.write(bytes, ((Number) value).longValue());
break;
case STRING:
pointer = writeString(bytes, value.toString());
break;
case DOUBLE:
long dg = Double.doubleToLongBits(((Number) value).doubleValue());
pointer = bytes.alloc(OLongSerializer.LONG_SIZE);
OLongSerializer.INSTANCE.serializeLiteral(dg, bytes.bytes, pointer);
break;
case FLOAT:
int fg = Float.floatToIntBits(((Number) value).floatValue());
pointer = bytes.alloc(OIntegerSerializer.INT_SIZE);
OIntegerSerializer.INSTANCE.serializeLiteral(fg, bytes.bytes, pointer);
break;
case BYTE:
pointer = bytes.alloc(1);
bytes.bytes[pointer] = ((Number) value).byteValue();
break;
case BOOLEAN:
pointer = bytes.alloc(1);
bytes.bytes[pointer] = ((Boolean) value) ? (byte) 1 : (byte) 0;
break;
case DATETIME:
if (value instanceof Number) {
pointer = OVarIntSerializer.write(bytes, ((Number) value).longValue());
} else
pointer = OVarIntSerializer.write(bytes, ((Date) value).getTime());
break;
case DATE:
long dateValue;
if (value instanceof Number) {
dateValue = ((Number) value).longValue();
} else
dateValue = ((Date) value).getTime();
dateValue = convertDayToTimezone(ODateHelper.getDatabaseTimeZone(), TimeZone.getTimeZone("GMT"), dateValue);
pointer = OVarIntSerializer.write(bytes, dateValue / MILLISEC_PER_DAY);
break;
case EMBEDDED:
pointer = bytes.offset;
if (value instanceof ODocumentSerializable) {
ODocument cur = ((ODocumentSerializable) value).toDocument();
cur.field(ODocumentSerializable.CLASS_NAME, value.getClass().getName());
serialize(cur, bytes, false);
} else {
serialize((ODocument) value, bytes, false);
}
break;
case EMBEDDEDSET:
case EMBEDDEDLIST:
if (value.getClass().isArray())
pointer = writeEmbeddedCollection(bytes, Arrays.asList(OMultiValue.array(value)), linkedType);
else
pointer = writeEmbeddedCollection(bytes, (Collection<?>) value, linkedType);
break;
case DECIMAL:
BigDecimal decimalValue = (BigDecimal) value;
pointer = bytes.alloc(ODecimalSerializer.INSTANCE.getObjectSize(decimalValue));
ODecimalSerializer.INSTANCE.serialize(decimalValue, bytes.bytes, pointer);
break;
case BINARY:
pointer = writeBinary(bytes, (byte[]) (value));
break;
case LINKSET:
case LINKLIST:
Collection<OIdentifiable> ridCollection = (Collection<OIdentifiable>) value;
pointer = writeLinkCollection(bytes, ridCollection);
break;
case LINK:
if (!(value instanceof OIdentifiable))
throw new OValidationException("Value '" + value + "' is not a OIdentifiable");
pointer = writeOptimizedLink(bytes, (OIdentifiable) value);
break;
case LINKMAP:
pointer = writeLinkMap(bytes, (Map<Object, OIdentifiable>) value);
break;
case EMBEDDEDMAP:
pointer = writeEmbeddedMap(bytes, (Map<Object, Object>) value);
break;
case LINKBAG:
pointer = ((ORidBag) value).toStream(bytes);
break;
case CUSTOM:
if (!(value instanceof OSerializableStream))
value = new OSerializableWrapper((Serializable) value);
pointer = writeString(bytes, value.getClass().getName());
writeBinary(bytes, ((OSerializableStream) value).toStream());
break;
case TRANSIENT:
break;
case ANY:
break;
}
return pointer;
}
Aggregations