All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.vertexium.serializer.kryo.KryoVertexiumSerializer Maven / Gradle / Ivy

There is a newer version: 4.10.0
Show newest version
package org.vertexium.serializer.kryo;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.esotericsoftware.kryo.io.UnsafeInput;
import com.esotericsoftware.kryo.io.UnsafeOutput;
import org.vertexium.VertexiumSerializer;

public class KryoVertexiumSerializer implements VertexiumSerializer {
    private static final byte[] EMPTY = new byte[0];
    private final ThreadLocal kryo = new ThreadLocal() {
        @Override
        protected Kryo initialValue() {
            return new KryoFactory().createKryo();
        }
    };

    @Override
    public byte[] objectToBytes(Object object) {
        if (object == null) {
            return EMPTY;
        }
        Output output = new UnsafeOutput(2000, -1);
        kryo.get().writeClassAndObject(output, object);
        return output.toBytes();
    }

    @Override
    public  T bytesToObject(byte[] bytes) {
        if (bytes == null || bytes.length == 0) {
            return null;
        }
        Input input = new UnsafeInput(bytes);
        return (T) kryo.get().readClassAndObject(input);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy