use of org.apache.geode.internal.ObjToByteArraySerializer in project geode by apache.
the class DataSerializer method writeObjectAsByteArray.
/**
* Serialize the given object <code>obj</code> into a byte array using
* {@link #writeObject(Object, DataOutput)} and then writes the byte array to the given data
* output <code>out</code> in the same format {@link #writeByteArray(byte[], DataOutput)} does.
* This method will serialize a <code>null</code> obj and not throw a
* <code>NullPointerException</code>.
*
* @param obj the object to serialize and write
* @param out the data output to write the byte array to
* @throws IllegalArgumentException if a problem occurs while serialize <code>obj</code>
* @throws IOException if a problem occurs while writing to <code>out</code>
*
* @see #readByteArray
* @since GemFire 5.0.2
*/
public static void writeObjectAsByteArray(Object obj, DataOutput out) throws IOException {
Object object = obj;
if (obj instanceof CachedDeserializable) {
if (obj instanceof StoredObject) {
StoredObject so = (StoredObject) obj;
if (logger.isTraceEnabled(LogMarker.SERIALIZER)) {
logger.trace(LogMarker.SERIALIZER, "writeObjectAsByteArray StoredObject");
}
so.sendAsByteArray(out);
return;
} else {
object = ((CachedDeserializable) obj).getSerializedValue();
}
}
if (logger.isTraceEnabled(LogMarker.SERIALIZER)) {
if (object == null) {
logger.trace(LogMarker.SERIALIZER, "writeObjectAsByteArray null");
} else {
logger.trace(LogMarker.SERIALIZER, "writeObjectAsByteArray obj.getClass={}", object.getClass());
}
}
if (object instanceof byte[] || object == null) {
writeByteArray((byte[]) object, out);
} else if (out instanceof ObjToByteArraySerializer) {
((ObjToByteArraySerializer) out).writeAsSerializedByteArray(object);
} else /*
* else if (obj instanceof Sendable) { ((Sendable)obj).sendTo(out); }
*/
{
HeapDataOutputStream hdos;
if (object instanceof HeapDataOutputStream) {
hdos = (HeapDataOutputStream) object;
} else {
Version v = InternalDataSerializer.getVersionForDataStreamOrNull(out);
if (v == null) {
v = Version.CURRENT;
}
hdos = new HeapDataOutputStream(v);
try {
DataSerializer.writeObject(object, hdos);
} catch (IOException e) {
RuntimeException e2 = new IllegalArgumentException(LocalizedStrings.DataSerializer_PROBELM_WHILE_SERIALIZING.toLocalizedString());
e2.initCause(e);
throw e2;
}
}
InternalDataSerializer.writeArrayLength(hdos.size(), out);
hdos.sendTo(out);
}
}
Aggregations