org.jupiter.serialization.kryo.KryoSerializer Maven / Gradle / Ivy
/*
* Copyright (c) 2015 The Jupiter Project
*
* Licensed under the Apache License, version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jupiter.serialization.kryo;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.esotericsoftware.kryo.serializers.JavaSerializer;
import org.jupiter.common.concurrent.collection.ConcurrentSet;
import org.jupiter.common.util.internal.InternalThreadLocal;
import org.jupiter.serialization.Serializer;
import org.jupiter.serialization.SerializerType;
import org.objenesis.strategy.StdInstantiatorStrategy;
/**
* Kryo的序列化/反序列化实现.
*
* 要注意的是关掉了对存在循环引用的类型的支持, 如果一定要序列化/反序列化循环引用的类型,
* 可以通过 {@link #setJavaSerializer(Class)} 设置该类型使用Java的序列化/反序列化机制,
* 对性能有一点影响, 但只是影响一个'点', 不影响'面'.
*
* jupiter
* org.jupiter.serialization.kryo
*
* @author jiachun.fjc
*/
public class KryoSerializer extends Serializer {
private static ConcurrentSet> useJavaSerializerTypes = new ConcurrentSet<>();
static {
useJavaSerializerTypes.add(Throwable.class);
}
private static final InternalThreadLocal kryoThreadLocal = new InternalThreadLocal() {
@Override
protected Kryo initialValue() throws Exception {
Kryo kryo = new Kryo();
for (Class type : useJavaSerializerTypes) {
kryo.addDefaultSerializer(type, JavaSerializer.class);
}
kryo.setInstantiatorStrategy(new StdInstantiatorStrategy());
kryo.setRegistrationRequired(false);
kryo.setReferences(false);
return kryo;
}
};
// 目的是复用 Output 中的 byte[]
private static final InternalThreadLocal