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

com.base4j.cache.serializer.FstSnappySerializer Maven / Gradle / Ivy

There is a newer version: 1.3.1
Show newest version
package com.base4j.cache.serializer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xerial.snappy.Snappy;

import java.io.IOException;

/**
 * Fast-Serialization搭载Snappy实现,对序列化数据做压缩
 */
public class FstSnappySerializer implements Serializer {
    private static final Logger log = LoggerFactory.getLogger(FstSnappySerializer.class);

    private final Serializer inner;

    public FstSnappySerializer() {
        this(new FstSerializer());
    }

    public FstSnappySerializer(Serializer innerSerializer) {
        this.inner = innerSerializer;
    }

    @Override
    public byte[] serialize(T value) {
        try {
            return Snappy.compress(inner.serialize(value));
        } catch (IOException e) {
            log.warn("序列化失败, value = " + value, e);
            return EMPTY_BYTES;
        }
    }

    @Override
    public T deserialize(byte[] bytes) {
        if (SerializerTools.isEmpty(bytes)) {
            return null;
        }
        try {
            return inner.deserialize(Snappy.uncompress(bytes));
        } catch (IOException e) {
            log.warn("反序列化 bytes 失败.", e);
            return null;
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy