Java Code Examples for com.esotericsoftware.kryo.Kryo#register()

The following examples show how to use com.esotericsoftware.kryo.Kryo#register() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KryoRedisSerializer.java    From blog_demos with Apache License 2.0 6 votes vote down vote up
@Override
public T deserialize(byte[] bytes) throws SerializationException {
    if (bytes == null || bytes.length <= 0) {
        return null;
    }

    Kryo kryo = kryos.get();
    kryo.setReferences(false);
    kryo.register(clazz);

    try (Input input = new Input(bytes)) {
        return (T) kryo.readClassAndObject(input);
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }

    return null;
}
 
Example 2
Source File: KryoSerialize.java    From search-spring-boot-starter with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings({ "unchecked" })
public <T> T decode(byte[] bytes, Class<T> clazz) {
    Kryo kryo = new Kryo();
    kryo.setReferences(false);
    kryo.register(clazz, new JavaSerializer());

    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
    Input input = new Input(byteArrayInputStream);
    try {
        return (T) kryo.readClassAndObject(input);
    } finally {
        input.close();
        try {
            byteArrayInputStream.close();
        } catch (IOException e) {
        }
    }
}
 
Example 3
Source File: EdgeValueWritable.java    From hgraphdb with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void readFields(final DataInput input) throws IOException {
    try {
        Kryo kryo = new Kryo();
        kryo.register(HBaseEdge.class, new HBaseEdgeSerializer());
        final ByteArrayInputStream bais = new ByteArrayInputStream(WritableUtils.readCompressedByteArray(input));
        this.edge = kryo.readObject(new Input(bais), HBaseEdge.class);
        Class<? extends Writable> cls = Class.forName(Text.readString(input)).asSubclass(Writable.class);
        Writable writable;
        if (cls.equals(NullWritable.class)) {
            writable = NullWritable.get();
        } else {
            writable = cls.newInstance();
        }
        writable.readFields(input);
        this.value = writable != NullWritable.get() ? (V) writable : null;
    } catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
        throw new IOException("Failed writable init", e);
    }
}
 
Example 4
Source File: KryoUtils.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Apply a list of {@link KryoRegistration} to a Kryo instance. The list of registrations is
 * assumed to already be a final resolution of all possible registration overwrites.
 *
 * <p>The registrations are applied in the given order and always specify the registration id as
 * the next available id in the Kryo instance (providing the id just extra ensures nothing is
 * overwritten, and isn't strictly required);
 *
 * @param kryo the Kryo instance to apply the registrations
 * @param resolvedRegistrations the registrations, which should already be resolved of all possible registration overwrites
 */
public static void applyRegistrations(Kryo kryo, Collection<KryoRegistration> resolvedRegistrations) {

	Serializer<?> serializer;
	for (KryoRegistration registration : resolvedRegistrations) {
		serializer = registration.getSerializer(kryo);

		if (serializer != null) {
			kryo.register(registration.getRegisteredClass(), serializer, kryo.getNextRegistrationId());
		} else {
			kryo.register(registration.getRegisteredClass(), kryo.getNextRegistrationId());
		}
	}
}
 
Example 5
Source File: KryoSerialization.java    From RuinsOfRevenge with MIT License 5 votes vote down vote up
public KryoSerialization (Kryo kryo) {
	this.kryo = kryo;

	kryo.register(RegisterTCP.class);
	kryo.register(RegisterUDP.class);
	kryo.register(KeepAlive.class);
	kryo.register(DiscoverHost.class);
	kryo.register(Ping.class);

	input = new Input(byteBufferInputStream, 512);
	output = new Output(byteBufferOutputStream, 512);
}
 
Example 6
Source File: KryoContentCodec.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
public KryoContentCodec(int dimension, CodecDefinition definition) {
    this.codecDefinition = definition;
    Kryo kryo = new Kryo();
    kryo.setReferences(true);
    kryo.setRegistrationRequired(true);
    for (ClassDefinition classDefinition : definition.getClassDefinitions()) {
        Class<?> clazz = classDefinition.getType();
        if (clazz == void.class || clazz == Void.class) {
            // TODO
            continue;
        }
        kryo.register(clazz);
        if (clazz.isPrimitive()) {
            for (int index = 0; index < dimension; index++) {
                Object array = Array.newInstance(clazz, 0);
                kryo.register(array.getClass());
                clazz = array.getClass();
            }
        } else {
            Type type = clazz;
            for (int index = 0; index < dimension; index++) {
                type = TypeUtility.genericArrayType(type);
                kryo.register(TypeUtility.getRawType(type, null));
            }
        }

    }
    this.kryo = kryo;
}
 
Example 7
Source File: KryoSerialization.java    From craft-atom with MIT License 5 votes vote down vote up
private static Kryo newKryo() {
	Kryo kryo = new Kryo();
    kryo.register(RpcBody.class);
    kryo.register(RpcMethod.class);
    kryo.setDefaultSerializer(CompatibleFieldSerializer.class);
    return kryo;
}
 
Example 8
Source File: KryoUtils.java    From kylin with Apache License 2.0 5 votes vote down vote up
public static Kryo getKryo() {
    if (_Kryo.get() == null) {
        Kryo kryo = new Kryo();
        kryo.setInstantiatorStrategy(new Kryo.DefaultInstantiatorStrategy(new StdInstantiatorStrategy()));
        kryo.register(BitSet.class, new BitSetSerializer());
        _Kryo.set(kryo);
    }

    return _Kryo.get();
}
 
Example 9
Source File: KryoUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Apply a list of {@link KryoRegistration} to a Kryo instance. The list of registrations is
 * assumed to already be a final resolution of all possible registration overwrites.
 *
 * <p>The registrations are applied in the given order and always specify the registration id as
 * the next available id in the Kryo instance (providing the id just extra ensures nothing is
 * overwritten, and isn't strictly required);
 *
 * @param kryo the Kryo instance to apply the registrations
 * @param resolvedRegistrations the registrations, which should already be resolved of all possible registration overwrites
 */
public static void applyRegistrations(Kryo kryo, Collection<KryoRegistration> resolvedRegistrations) {

	Serializer<?> serializer;
	for (KryoRegistration registration : resolvedRegistrations) {
		serializer = registration.getSerializer(kryo);

		if (serializer != null) {
			kryo.register(registration.getRegisteredClass(), serializer, kryo.getNextRegistrationId());
		} else {
			kryo.register(registration.getRegisteredClass(), kryo.getNextRegistrationId());
		}
	}
}
 
Example 10
Source File: DefaultStateSerializer.java    From jstorm with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a {@link DefaultStateSerializer} instance with the given list
 * of classes registered in kryo.
 *
 * @param classesToRegister the classes to register.
 */
public DefaultStateSerializer(List<Class<?>> classesToRegister) {
    kryo = new Kryo();
    output = new Output(2000, 2000000000);
    for (Class<?> klazz : classesToRegister) {
        kryo.register(klazz);
    }
}
 
Example 11
Source File: EventFields.java    From accumulo-recipes with Apache License 2.0 5 votes vote down vote up
public static synchronized void initializeKryo(Kryo kryo) {
    if (kryoInitialized)
        return;
    valueSerializer = new ByteArraySerializer();
    kryo.register(byte[].class, valueSerializer);

    kryoInitialized = true;
}
 
Example 12
Source File: DeepKryoRegistrator.java    From deep-spark with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void registerClasses(Kryo kryo) {
    kryo.register(Cell.class);
    kryo.register(Cells.class);
    kryo.register(IDeepType.class);
    UnmodifiableCollectionsSerializer.registerSerializers(kryo);
}
 
Example 13
Source File: Network.java    From rpc-bench with Apache License 2.0 5 votes vote down vote up
public static void register(final EndPoint endPoint) {
  final Kryo kryo = endPoint.getKryo();
  kryo.setReferences(false);
  kryo.register(Ping.class);
  kryo.register(Pong.class);
  kryo.register(Size.class);
  kryo.register(Price.class);
}
 
Example 14
Source File: KryoUtils.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
public static Kryo getKryo() {
    if (_Kryo.get() == null) {
        Kryo kryo = new Kryo();
        kryo.setInstantiatorStrategy(new Kryo.DefaultInstantiatorStrategy(new StdInstantiatorStrategy()));
        kryo.register(BitSet.class, new BitSetSerializer());
        _Kryo.set(kryo);
    }

    return _Kryo.get();
}
 
Example 15
Source File: NetManager.java    From gdx-proto with Apache License 2.0 5 votes vote down vote up
public static void registerKryoClasses(Kryo k) {
	Class[] classes = new Class[]{
			ServerUpdate.class,
			EntityUpdate.class,
			EntityUpdate[].class,
			Vector3.class,
			Vector3[].class,
			Matrix4.class,
			float[].class,
			ClientUpdate.class,
			CommandPackage.class,
			ClientRequest.class,
			ServerMessage.class,
			ServerMessage.AssignPlayerEntityId.class,
			ServerMessage.DestroyEntity.class,
			ServerMessage.ServerInfo.class,
			ServerMessage.LevelGeometry.class,
			ChatMessage.class,
			EntityInfoRequest.class,
			EntityInfoRequest.Response.class,
			BulletPackage.class,
			Entity.EntityGraphicsType.class,
			LevelStatic.class,
			LevelStatic[].class,
	};
	for (Class clazz : classes) {
		k.register(clazz);
	}
}
 
Example 16
Source File: KryoSerializer.java    From incubator-heron with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"rawtypes", "unchecked"})
private static void registerDefaultSerializers(Kryo k) {
  // Default serializers
  k.register(byte[].class);
  k.register(ArrayList.class, new ArrayListSerializer());
  k.register(HashMap.class, new HashMapSerializer());
  k.register(HashSet.class, new HashSetSerializer());
  k.register(BigInteger.class, new DefaultSerializers.BigIntegerSerializer());
  k.register(Values.class);
}
 
Example 17
Source File: RyaSubGraphKafkaSerDe.java    From rya with Apache License 2.0 4 votes vote down vote up
public RyaSubGraphKafkaSerDe() {
    kryo = new Kryo();
    kryo.register(RyaSubGraph.class,new RyaSubGraphSerializer());
}
 
Example 18
Source File: MarkDuplicatesSparkUtilsUnitTest.java    From gatk with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void registerClasses(Kryo kryo) {
    kryo.register(SAMRecord.class, new SAMRecordSerializer());
}
 
Example 19
Source File: KryoObjectCopyHelper.java    From sdk-rest with MIT License 4 votes vote down vote up
public static <T> T copy(T entity) {
	Kryo kryo = new Kryo();
	kryo.register( DateTime.class, new JodaDateTimeSerializer() );
	return kryo.copy(entity);
}
 
Example 20
Source File: FuncotatorTestUtils.java    From gatk with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Takes an input object and returns the value of the object after it has been serialized and then deserialized in Kryo.
 * Requires the class of the input object as a parameter because it's not generally possible to get the class of a
 * generified method parameter with reflection.
 *
 * @param input instance of inputClazz.  Never {@code null}
 * @param inputClazz class to cast input
 * @param <T> class to attempt.  Same or subclass of inputClazz
 * @return serialized and deserialized instance of input.  Throws exception if serialization round trip fails.
 */
public static <T> T assertRoundTripInKryo(final T input, final Class<?> inputClazz, final List<Consumer<Kryo>> preprocessingFunctions) {
    Utils.nonNull(input);
    final Kryo kryo = new Kryo();
    preprocessingFunctions.stream().forEach(f -> f.accept(kryo));
    kryo.register(inputClazz);
    return kryo.copy(input);
}