/** * Sets the next Kryo registration Id for following register entries. * * @param id Kryo registration Id * @return this * @see Kryo#register(Class, Serializer, int) */ public Builder nextId(final int id) { if (!types.isEmpty()) { if (id != FLOATING_ID && id < blockHeadId + types.size()) { if (LOGGER.isWarnEnabled()) { LOGGER.warn("requested nextId {} could potentially overlap " + "with existing registrations {}+{} ", id, blockHeadId, types.size(), new RuntimeException()); } } blocks.add(new RegistrationBlock(this.blockHeadId, types)); types = new ArrayList<>(); } this.blockHeadId = id; return this; }
private Builder register(RegistrationBlock block) { if (block.begin() != FLOATING_ID) { // flush pending types nextId(block.begin()); blocks.add(block); nextId(block.begin() + block.types().size()); } else { // flush pending types final int addedBlockBegin = blockHeadId + types.size(); nextId(addedBlockBegin); blocks.add(new RegistrationBlock(addedBlockBegin, block.types())); nextId(addedBlockBegin + block.types().size()); } return this; }
/** * 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; }
/** * Sets the next Kryo registration Id for following register entries. * * @param id Kryo registration Id * @return this * @see Kryo#register(Class, Serializer, int) */ public Builder nextId(final int id) { if (!types.isEmpty()) { if (id != FLOATING_ID && id < blockHeadId + types.size()) { if (log.isWarnEnabled()) { log.warn("requested nextId {} could potentially overlap " + "with existing registrations {}+{} ", id, blockHeadId, types.size(), new RuntimeException()); } } blocks.add(new RegistrationBlock(this.blockHeadId, types)); types = new ArrayList<>(); } this.blockHeadId = id; return this; }
@SuppressWarnings("unchecked") private static List<RegistrationBlock> buildRegistrationBlocks(NamespaceConfig config) { List<Pair<Class<?>[], Serializer<?>>> types = new ArrayList<>(); List<RegistrationBlock> blocks = new ArrayList<>(); blocks.addAll(Namespaces.BASIC.registeredBlocks); for (NamespaceTypeConfig type : config.getTypes()) { try { if (type.getId() == null) { types.add(Pair.of(new Class[]{type.getType()}, type.getSerializer() != null ? type.getSerializer().newInstance() : null)); } else { blocks.add(new RegistrationBlock(type.getId(), Collections.singletonList(Pair.of(new Class[]{type.getType()}, type.getSerializer().newInstance())))); } } catch (InstantiationException | IllegalAccessException e) { throw new ConfigurationException("Failed to instantiate serializer from configuration", e); } } blocks.add(new RegistrationBlock(FLOATING_ID, types)); return blocks; }
private Builder register(RegistrationBlock block) { if (block.begin() != FLOATING_ID) { // flush pending types nextId(block.begin()); blocks.add(block); nextId(block.begin() + block.types().size()); } else { // flush pending types final int addedBlockBegin = blockHeadId + types.size(); nextId(addedBlockBegin); blocks.add(new RegistrationBlock(addedBlockBegin, block.types())); nextId(addedBlockBegin + block.types().size()); } return this; }
/** * Gets the number of classes registered in this Kryo namespace. * * @return size of namespace */ public int size() { return (int) registeredBlocks.stream() .flatMap(block -> block.types().stream()) .count(); }
/** * Builds a {@link Namespace} instance. * * @param friendlyName friendly name for the namespace * @return KryoNamespace */ public Namespace build(String friendlyName) { if (!types.isEmpty()) { blocks.add(new RegistrationBlock(this.blockHeadId, types)); } return new Namespace(blocks, classLoader, registrationRequired, compatible, friendlyName).populate(1); }
/** * 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; }
@SuppressWarnings("unchecked") private static List<RegistrationBlock> buildRegistrationBlocks(NamespaceConfig config) { List<Pair<Class<?>[], Serializer<?>>> types = new ArrayList<>(); List<RegistrationBlock> blocks = new ArrayList<>(); blocks.addAll(Namespaces.BASIC.registeredBlocks); for (NamespaceTypeConfig type : config.getTypes()) { try { if (type.getId() == null) { types.add(Pair.of(new Class[]{type.getType()}, type.getSerializer() != null ? type.getSerializer().newInstance() : null)); } else { blocks.add(new RegistrationBlock(type.getId(), Collections.singletonList(Pair.of(new Class[]{type.getType()}, type.getSerializer().newInstance())))); } } catch (InstantiationException | IllegalAccessException e) { throw new ConfigurationException("Failed to instantiate serializer from configuration", e); } } blocks.add(new RegistrationBlock(FLOATING_ID, types)); return blocks; }
/** * Builds a {@link Namespace} instance. * * @param friendlyName friendly name for the namespace * @return KryoNamespace */ public Namespace build(String friendlyName) { if (!types.isEmpty()) { blocks.add(new RegistrationBlock(this.blockHeadId, types)); } return new Namespace(blocks, classLoader, registrationRequired, compatible, friendlyName).populate(1); }
/** * Gets the number of classes registered in this Kryo namespace. * * @return size of namespace */ public int size() { return (int) registeredBlocks.stream() .flatMap(block -> block.types().stream()) .count(); }