Search in sources :

Example 1 with HeapBuffer

use of io.atomix.catalyst.buffer.HeapBuffer in project concourse by cinchapi.

the class PluginSerializer method serialize.

/**
 * Return a {@link ByteBuffer} that contains the serialized form of the
 * input {@code object}.
 *
 * @param object the object to serialize
 * @return the serialized form within a ByteBuffer
 */
public ByteBuffer serialize(Object object) {
    ByteBuffer buffer = null;
    if (object instanceof PluginSerializable) {
        HeapBuffer buffer0 = HeapBuffer.allocate();
        buffer0.writeByte(Scheme.PLUGIN_SERIALIZABLE.ordinal());
        buffer0.writeUTF8(object.getClass().getName());
        ((PluginSerializable) object).serialize(buffer0);
        byte[] bytes = new byte[(int) buffer0.position()];
        buffer0.flip();
        buffer0.read(bytes);
        buffer = ByteBuffer.wrap(bytes);
        return buffer;
    } else if (object instanceof RemoteMessage) {
        HeapBuffer buffer0 = (HeapBuffer) ((RemoteMessage) object).serialize();
        buffer = ByteBuffer.allocate((int) buffer0.remaining() + 1);
        buffer.put((byte) Scheme.REMOTE_MESSAGE.ordinal());
        buffer.put(buffer0.array(), 0, (int) buffer0.remaining());
        buffer.flip();
        return buffer;
    } else if (object instanceof ComplexTObject) {
        byte[] bytes = ((ComplexTObject) object).toByteBuffer().array();
        buffer = ByteBuffer.allocate(bytes.length + 1);
        buffer.put((byte) Scheme.COMPLEX_TOBJECT.ordinal());
        buffer.put(bytes);
        buffer.flip();
        return buffer;
    } else if (object instanceof TObject) {
        byte[] bytes = ((TObject) object).getData();
        buffer = ByteBuffer.allocate(bytes.length + 2);
        buffer.put((byte) Scheme.TOBJECT.ordinal());
        buffer.put((byte) ((TObject) object).getType().ordinal());
        buffer.put(bytes);
        buffer.flip();
        return buffer;
    } else if (object instanceof String || object.getClass().isPrimitive() || object instanceof Number || object instanceof Boolean || object instanceof Map || object instanceof List || object instanceof Set) {
        return serialize(ComplexTObject.fromJavaObject(object));
    } else if (object instanceof Serializable) {
        byte[] bytes = ByteBuffers.getByteArray(Serializables.getBytes((Serializable) object));
        byte[] classBytes = object.getClass().getName().getBytes(StandardCharsets.UTF_8);
        buffer = ByteBuffer.allocate(1 + 2 + classBytes.length + bytes.length);
        buffer.put((byte) Scheme.JAVA_SERIALIZABLE.ordinal());
        buffer.putShort((short) classBytes.length);
        buffer.put(classBytes);
        buffer.put(bytes);
        buffer.flip();
        return buffer;
    } else {
        throw new IllegalStateException("Cannot plugin serialize an object of type " + object.getClass());
    }
}
Also used : RemoteMessage(com.cinchapi.concourse.server.plugin.RemoteMessage) ComplexTObject(com.cinchapi.concourse.thrift.ComplexTObject) TObject(com.cinchapi.concourse.thrift.TObject) Serializable(java.io.Serializable) Set(java.util.Set) ComplexTObject(com.cinchapi.concourse.thrift.ComplexTObject) ByteBuffer(java.nio.ByteBuffer) HeapBuffer(io.atomix.catalyst.buffer.HeapBuffer) List(java.util.List) Map(java.util.Map)

Aggregations

RemoteMessage (com.cinchapi.concourse.server.plugin.RemoteMessage)1 ComplexTObject (com.cinchapi.concourse.thrift.ComplexTObject)1 TObject (com.cinchapi.concourse.thrift.TObject)1 HeapBuffer (io.atomix.catalyst.buffer.HeapBuffer)1 Serializable (java.io.Serializable)1 ByteBuffer (java.nio.ByteBuffer)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1