/** * 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()); } } } }
final int nextId = kryo.getNextRegistrationId();
/** * Creates a Kryo instance. * * @return Kryo instance */ @Override public Kryo create() { LOGGER.trace("Creating Kryo instance for {}", this); Kryo kryo = new Kryo(); kryo.setClassLoader(classLoader); kryo.setRegistrationRequired(registrationRequired); // If compatible serialization is enabled, override the default serializer. if (compatible) { kryo.setDefaultSerializer(CompatibleFieldSerializer::new); } // TODO rethink whether we want to use StdInstantiatorStrategy kryo.setInstantiatorStrategy( new Kryo.DefaultInstantiatorStrategy(new StdInstantiatorStrategy())); for (RegistrationBlock block : registeredBlocks) { int id = block.begin(); if (id == FLOATING_ID) { id = kryo.getNextRegistrationId(); } for (Pair<Class<?>[], Serializer<?>> entry : block.types()) { register(kryo, entry.getLeft(), entry.getRight(), id++); } } return kryo; }
public void init() { firstAvailableRegistrationId = kryo.getNextRegistrationId(); nextAvailableRegistrationId = firstAvailableRegistrationId; }
/** * 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()); } } } }
/** * 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()); } } } }
/** Registers the class using the lowest, next available integer ID and the specified serializer. If the class is already * registered, the existing entry is updated with the new serializer. Registering a primitive also affects the corresponding * primitive wrapper. * <p> * Because the ID assigned is affected by the IDs registered before it, the order classes are registered is important when * using this method. The order must be the same at deserialization as it was for serialization. */ public Registration register (Class type, Serializer serializer) { Registration registration = classResolver.getRegistration(type); if (registration != null) { registration.setSerializer(serializer); return registration; } return classResolver.register(new Registration(type, serializer, getNextRegistrationId())); }
/** Registers the class using the lowest, next available integer ID and the specified serializer. If the class is already * registered, the existing entry is updated with the new serializer. Registering a primitive also affects the corresponding * primitive wrapper. * <p> * Because the ID assigned is affected by the IDs registered before it, the order classes are registered is important when * using this method. The order must be the same at deserialization as it was for serialization. */ public Registration register (Class type, Serializer serializer) { Registration registration = classResolver.getRegistration(type); if (registration != null) { registration.setSerializer(serializer); return registration; } return classResolver.register(new Registration(type, serializer, getNextRegistrationId())); }
/** Registers the class using the lowest, next available integer ID and the specified serializer. If the class is already * registered, the existing entry is updated with the new serializer. Registering a primitive also affects the corresponding * primitive wrapper. * <p> * Because the ID assigned is affected by the IDs registered before it, the order classes are registered is important when * using this method. The order must be the same at deserialization as it was for serialization. */ public Registration register (Class type, Serializer serializer) { Registration registration = classResolver.getRegistration(type); if (registration != null) { registration.setSerializer(serializer); return registration; } return classResolver.register(new Registration(type, serializer, getNextRegistrationId())); }
/** Registers the class using the lowest, next available integer ID and the specified serializer. If the class is already * registered, the existing entry is updated with the new serializer. Registering a primitive also affects the corresponding * primitive wrapper. * <p> * Because the ID assigned is affected by the IDs registered before it, the order classes are registered is important when * using this method. The order must be the same at deserialization as it was for serialization. */ public Registration register (Class type, Serializer serializer) { Registration registration = classResolver.getRegistration(type); if (registration != null) { registration.setSerializer(serializer); return registration; } return classResolver.register(new Registration(type, serializer, getNextRegistrationId())); }
/** * Creates a Kryo instance. * * @return Kryo instance */ @Override public Kryo create() { log.trace("Creating Kryo instance for {}", this); Kryo kryo = new Kryo(); kryo.setRegistrationRequired(registrationRequired); // TODO rethink whether we want to use StdInstantiatorStrategy kryo.setInstantiatorStrategy( new Kryo.DefaultInstantiatorStrategy(new StdInstantiatorStrategy())); for (RegistrationBlock block : registeredBlocks) { int id = block.begin(); if (id == FLOATING_ID) { id = kryo.getNextRegistrationId(); } for (Pair<Class<?>[], com.esotericsoftware.kryo.Serializer<?>> entry : block.types()) { register(kryo, entry.getLeft(), entry.getRight(), id++); } } return kryo; }
/** * Creates a Kryo instance. * * @return Kryo instance */ @Override public Kryo create() { log.trace("Creating Kryo instance for {}", this); Kryo kryo = new Kryo(); kryo.setRegistrationRequired(registrationRequired); // If compatible serialization is enabled, override the default serializer. if (compatible) { kryo.setDefaultSerializer(CompatibleFieldSerializer::new); } // TODO rethink whether we want to use StdInstantiatorStrategy kryo.setInstantiatorStrategy( new Kryo.DefaultInstantiatorStrategy(new StdInstantiatorStrategy())); for (RegistrationBlock block : registeredBlocks) { int id = block.begin(); if (id == FLOATING_ID) { id = kryo.getNextRegistrationId(); } for (Pair<Class<?>[], Serializer<?>> entry : block.types()) { register(kryo, entry.getLeft(), entry.getRight(), id++); } } return kryo; }
/** * Creates a Kryo instance. * * @return Kryo instance */ @Override public Kryo create() { log.trace("Creating Kryo instance for {}", this); Kryo kryo = new Kryo(); kryo.setClassLoader(classLoader); kryo.setRegistrationRequired(registrationRequired); // If compatible serialization is enabled, override the default serializer. if (compatible) { kryo.setDefaultSerializer(CompatibleFieldSerializer::new); } // TODO rethink whether we want to use StdInstantiatorStrategy kryo.setInstantiatorStrategy( new Kryo.DefaultInstantiatorStrategy(new StdInstantiatorStrategy())); for (RegistrationBlock block : registeredBlocks) { int id = block.begin(); if (id == FLOATING_ID) { id = kryo.getNextRegistrationId(); } for (Pair<Class<?>[], Serializer<?>> entry : block.types()) { register(kryo, entry.getLeft(), entry.getRight(), id++); } } return kryo; }
int nextRegistrationId = kryo.getNextRegistrationId(); nextRegistrationId += 9;