Kryo

使用Kryo序列化和反序列化
https://github.com/EsotericSoftware/kryo
工具类:

package com.gillion.cache.manager.utils;


import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.Registration;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 使用Kryo序列化对象帮助类
 * Created by admin on 2017/4/17.
 */
public class KryoUtil {

    private static final ThreadLocal kryos = new ThreadLocal() {
        protected Kryo initialValue() {
            Kryo kryo = new Kryo();
            // configure kryo instance, customize settings
            kryo.setReferences(false);
            kryo.register(Collection.class);
            kryo.register(Map.class);
            return kryo;
        }
    };
    private static Map registrationMap = new ConcurrentHashMap();

    /**
     * 序列化
     *
     * @param obj 序列化对象
     * @return 序列化后的byte[]值
     */
    public static  byte[] serializer(T obj) {
        Class clazz = (Class) obj.getClass();
        Kryo kryo = kryos.get();
        if (!registrationMap.containsKey(clazz)) {
            Registration registration = kryo.register(clazz);
            registrationMap.put(clazz, registration);
        }
        ByteArrayOutputStream outputStream = null;
        Output output = null;
        byte[] bytes;
        try {
            outputStream = new ByteArrayOutputStream();
            output = new Output(outputStream);
            kryo.writeObject(output, obj);
            output.flush();
            bytes = outputStream.toByteArray();
            return bytes;
        } finally {
            try {
                if (output != null) {
                    output.close();
                }
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (Exception ignore) {

            }
        }

    }

    /**
     * 反序列化
     *
     * @param data  序列化后的byte[]值
     * @param clazz 反序列化后的对象
     * @return 返回的对象
     */

    public static  T deserializer(byte[] data, Class clazz) {
        Kryo kryo = kryos.get();
        Registration registration = registrationMap.get(clazz);
        if (registration == null) {
            registration = kryo.register(clazz);
            registrationMap.put(clazz, registration);
        }
        T object = null;
        ByteArrayInputStream byteArrayInputStream = null;
        Input input;
        try {
            byteArrayInputStream = new ByteArrayInputStream(data);
            input = new Input(byteArrayInputStream);
            object = (T) kryo.readObject(input, registration.getType());
            input.close();
        } finally {
            try {
                if (byteArrayInputStream != null) {
                    byteArrayInputStream.close();
                }
            } catch (IOException ignore) {
            }
        }
        return object;
    }

}

你可能感兴趣的:(Kryo)