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

artoria.serialize.support.KryoSerializer Maven / Gradle / Ivy

The newest version!
package artoria.serialize.support;

import artoria.core.Serializer;
import artoria.util.Assert;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.esotericsoftware.kryo.pool.KryoCallback;
import com.esotericsoftware.kryo.pool.KryoPool;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

/**
 * Kryo serializer.
 * @author Kahle
 */
@Deprecated
public class KryoSerializer implements Serializer {
    private final KryoPool kryoPool;

    public KryoSerializer(KryoPool kryoPool) {
        Assert.notNull(kryoPool, "Parameter \"kryoPool\" must not null. ");
        this.kryoPool = kryoPool;
    }

    @Override
    public Object serialize(final Object object) {
        Assert.notNull(object, "Parameter \"object\" must not null. ");
        return kryoPool.run(new KryoCallback() {
            @Override
            public Object execute(Kryo kryo) {
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                Output output = new Output(outputStream);
                kryo.writeClassAndObject(output, object);
                output.flush();
                return outputStream.toByteArray();
            }
        });
    }

    @Override
    public Object deserialize(final Object data) {
        Assert.notNull(data, "Parameter \"data\" must not null. ");
        return kryoPool.run(new KryoCallback() {
            @Override
            public Object execute(Kryo kryo) {
            ByteArrayInputStream inputStream = new ByteArrayInputStream((byte[]) data);
            Input input = new Input(inputStream);
            return kryo.readClassAndObject(input);
            }
        });
    }

}