/** * Factory method for Kryo serializers. * * @return */ protected Kryo createKryo() { return new KryoReflectionFactorySupport(); }
/** * {@inheritDoc} */ @Override @SuppressWarnings( "unchecked" ) public <T> T newInstance( final Class<T> type ) { if (type == null) { throw new IllegalArgumentException("type cannot be null."); } Constructor<?> constructor = _constructors.get( type ); if ( constructor == null ) { constructor = getNoArgsConstructor( type ); if ( constructor == null ) { constructor = newConstructorForSerialization( type ); } _constructors.put( type, constructor ); } return (T) newInstanceFrom( constructor ); }
@SuppressWarnings("unchecked") @Override public Object create(final Kryo kryo, final Input input, @SuppressWarnings("rawtypes") final Class type) { return KryoReflectionFactorySupport.newInstanceFromReflectionFactory( type ); }
@SuppressWarnings( "unchecked" ) public static <T> T newInstanceFromReflectionFactory( final Class<T> type ) { Constructor<?> constructor = _constructors.get( type ); if ( constructor == null ) { constructor = newConstructorForSerialization( type ); _constructors.put( type, constructor ); } return (T) newInstanceFrom( constructor ); }
/** * Write kryo. * * @param <T> the type parameter * @param obj the obj * @param file the file */ public static <T> void writeKryo(T obj, OutputStream file) { try { Output output = new Output(buffer.get()); new KryoReflectionFactorySupport().writeClassAndObject(output, obj); output.close(); IOUtils.write(Arrays.copyOf(output.getBuffer(), output.position()), file); file.close(); } catch (IOException e) { throw new RuntimeException(e); } }
/** * Read kryo t. * * @param <T> the type parameter * @param file the file * @return the t */ public static <T> T readKryo(File file) { try { byte[] bytes = Files.readAllBytes(file.toPath()); Input input = new Input(buffer.get(), 0, bytes.length); return (T) new KryoReflectionFactorySupport().readClassAndObject(input); } catch (IOException e) { throw new RuntimeException(e); } } }
@Override public Serializer<?> getDefaultSerializer(@SuppressWarnings("rawtypes") final Class clazz) { if (EnumMap.class.isAssignableFrom(clazz)) { return new EnumMapSerializer(); } if (SubListSerializers.ArrayListSubListSerializer.canSerialize(clazz) || SubListSerializers.JavaUtilSubListSerializer.canSerialize(clazz)) { return SubListSerializers.createFor(clazz); } return super.getDefaultSerializer(clazz); } };
@Override public void writeClassAndObject(Output buffer, Object object) { if (object != null) { Class<? extends Object> target = object.getClass(); for (Class<?> cls : blackList) { if (cls.isAssignableFrom(target)) { throw new IllegalArgumentException("Should not serialize class with type: " + cls.getName()); } } } super.writeClassAndObject(buffer, object); if (object != null) { LOG.error("Wrote '{}' bytes for object: '{}'", buffer.position(), object.getClass()); } }
@Override public Registration writeClass(Output output, Class type) { Registration ret; before(output, type); ret = super.writeClass(output, type); after(output, type); return ret; }
@SuppressWarnings( "unchecked" ) public static <T> T newInstanceFromReflectionFactory( final Class<T> type ) { Constructor<?> constructor = _constructors.get( type ); if ( constructor == null ) { constructor = newConstructorForSerialization( type ); _constructors.put( type, constructor ); } return (T) newInstanceFrom( constructor ); }
@SuppressWarnings("unchecked") public com.esotericsoftware.kryo.Serializer<?> getDefaultSerializer(@SuppressWarnings("rawtypes") Class type) { if (EnumSet.class.isAssignableFrom(type) ) { return new EnumSetSerializer(); } if (EnumMap.class.isAssignableFrom(type) ) { return new EnumMapSerializer(); } if (Collection.class.isAssignableFrom(type) ) { return new CopyForIterateCollectionSerializer(); } if (Map.class.isAssignableFrom(type) ) { return new CopyForIterateMapSerializer(); } if (Date.class.isAssignableFrom(type) ) { return new DateSerializer(type); } return super.getDefaultSerializer(type); }; };
@Override public void writeClassAndObject(Output output, Object object) { before(output, object); super.writeClassAndObject(output, object); after(output, object); }
PojoModelSerializer() { kryo = new KryoReflectionFactorySupport(); new ExtendedJDKKryoRegistrar().apply(kryo); new GuavaKryoRegistrar().apply(kryo); new AllScalaRegistrar().apply(kryo); }
/** * {@inheritDoc} */ @Override @SuppressWarnings( "unchecked" ) public <T> T newInstance( final Class<T> type ) { if (type == null) { throw new IllegalArgumentException("type cannot be null."); } Constructor<?> constructor = _constructors.get( type ); if ( constructor == null ) { constructor = getNoArgsConstructor( type ); if ( constructor == null ) { constructor = newConstructorForSerialization( type ); } _constructors.put( type, constructor ); } return (T) newInstanceFrom( constructor ); }
@SuppressWarnings("unchecked") @Override public Object create(final Kryo kryo, final Input input, @SuppressWarnings("rawtypes") final Class type) { return KryoReflectionFactorySupport.newInstanceFromReflectionFactory( type ); }