@Override public ExtensionCriterion read(Kryo kryo, Input input, Class<ExtensionCriterion> type) { ExtensionSelectorType exType = (ExtensionSelectorType) kryo.readClassAndObject(input); DeviceId deviceId = (DeviceId) kryo.readClassAndObject(input); DriverService driverService = DefaultServiceDirectory.getService(DriverService.class); DriverHandler handler = new DefaultDriverHandler( new DefaultDriverData(driverService.getDriver(deviceId), deviceId)); ExtensionSelectorResolver resolver = handler.behaviour(ExtensionSelectorResolver.class); ExtensionSelector selector = resolver.getExtensionSelector(exType); byte[] bytes = (byte[]) kryo.readClassAndObject(input); selector.deserialize(bytes); return Criteria.extension(selector, deviceId); }
public void write(Kryo kryo, Output output, ArrayMetaData<long[]> object) { try { long[] arrData = arrayMetaData.getArrData(); arrayMetaData.send(output); int arrSegNum = arrayMetaData.getSegNum(); for (int i = 0; i < arrSegNum; i++) { int from = arrayMetaData.getFrom(i); int to = arrayMetaData.getTo(i); for (int j = from; j < to; j++) { output.writeLong(arrData[j]); } } } catch (IOException e) { LOG.error("double array write exception", e); System.exit(1); } }
@Override public MapMetaData<Short> read(Kryo kryo, Input input, Class<MapMetaData<Short>> type) { try { thatMapMetaData = mapMetaData.recv(input); int thatMapSegNum = thatMapMetaData.getSegNum(); List<Map<String, Short>> mapDataList = new ArrayList<>(thatMapSegNum); thatMapMetaData.setMapDataList(mapDataList); for (int i = 0; i < thatMapSegNum; i++) { int dataNum = thatMapMetaData.getDataNum(i); Map<String, Short> mapData = new HashMap<>(dataNum); mapDataList.add(mapData); for (int j = 0; j < dataNum; j++) { String key = input.readString(); Short val = input.readShort(); mapData.put(key, val); } } } catch (IOException e) { LOG.error("double array read exception", e); System.exit(1); } return thatMapMetaData; }
public static Kryo createKryoInstance() { Kryo kryo = new Kryo(); // add custom serialisers to work with joda time (the kryo version that we get when including spark as a dependency cannot handle joda time by default) // see https://github.com/magro/kryo-serializers kryo.register( Arrays.asList( "" ).getClass(), new ArraysAsListSerializer() ); kryo.register( Collections.EMPTY_LIST.getClass(), new CollectionsEmptyListSerializer() ); kryo.register( Collections.EMPTY_MAP.getClass(), new CollectionsEmptyMapSerializer() ); kryo.register( Collections.EMPTY_SET.getClass(), new CollectionsEmptySetSerializer() ); kryo.register( Collections.singletonList( "" ).getClass(), new CollectionsSingletonListSerializer() ); kryo.register( Collections.singleton( "" ).getClass(), new CollectionsSingletonSetSerializer() ); kryo.register( Collections.singletonMap( "", "" ).getClass(), new CollectionsSingletonMapSerializer() ); kryo.register( GregorianCalendar.class, new GregorianCalendarSerializer() ); kryo.register( InvocationHandler.class, new JdkProxySerializer() ); UnmodifiableCollectionsSerializer.registerSerializers( kryo ); SynchronizedCollectionsSerializer.registerSerializers( kryo ); // custom serializers for non-jdk libs return kryo; }
@Override public RegisteredService read(final Kryo kryo, final Input input, final Class<RegisteredService> type) { final AbstractRegisteredService svc = new RegexRegisteredService(); svc.setServiceId(kryo.readObject(input, String.class)); svc.setName(kryo.readObject(input, String.class)); svc.setDescription(kryo.readObject(input, String.class)); svc.setId(kryo.readObject(input, Long.class)); svc.setEvaluationOrder(kryo.readObject(input, Integer.class)); svc.setLogo(kryo.readObject(input, URL.class)); svc.setLogoutType(kryo.readObject(input, LogoutType.class)); svc.setLogoutUrl(kryo.readObject(input, URL.class)); svc.setRequiredHandlers(kryo.readObject(input, ImmutableSet.class)); svc.setTheme(kryo.readObject(input, String.class)); svc.setPublicKey(readObjectByReflection(kryo, input, RegisteredServicePublicKey.class)); svc.setProxyPolicy(readObjectByReflection(kryo, input, RegisteredServiceProxyPolicy.class)); svc.setAttributeReleasePolicy(readObjectByReflection(kryo, input, RegisteredServiceAttributeReleasePolicy.class)); svc.setUsernameAttributeProvider(readObjectByReflection(kryo, input, RegisteredServiceUsernameAttributeProvider.class)); svc.setAccessStrategy(readObjectByReflection(kryo, input, RegisteredServiceAccessStrategy.class)); return svc; }
public void write (Kryo kryo, Output output, IntMap map) { int length = map.size; output.writeVarInt(length, true); output.writeBoolean(false); // whether type is written (in case future version of IntMap supports type awareness) Serializer valueSerializer = null; if (valueGenericType != null) { if (valueSerializer == null) valueSerializer = kryo.getSerializer(valueGenericType); valueGenericType = null; } for (Iterator iter = map.iterator(); iter.hasNext();) { IntMap.Entry entry = (IntMap.Entry)iter.next(); output.writeInt(entry.key); if (valueSerializer != null) { kryo.writeObjectOrNull(output, entry.value, valueSerializer); } else kryo.writeClassAndObject(output, entry.value); } }
public void write (Kryo kryo, Output output, ObjectFloatMap map) { int length = map.size; output.writeVarInt(length, true); output.writeBoolean(false); // whether type is written (in case future version of ObjectFloatMap supports type awareness) Serializer keySerializer = null; if (keyGenericType != null) { if (keySerializer == null) keySerializer = kryo.getSerializer(keyGenericType); keyGenericType = null; } for (Iterator iter = map.iterator(); iter.hasNext();) { ObjectFloatMap.Entry entry = (ObjectFloatMap.Entry)iter.next(); if (keySerializer != null) { kryo.writeObject(output, entry.key, keySerializer); } else kryo.writeClassAndObject(output, entry.key); output.writeFloat(entry.value); } }
@Override public Queue read(Kryo kryo, Input input, Class<Queue> type) { int length = input.readVarInt(true); Registration registration = kryo.readClass(input); Class cls = registration == null ? Object.class : registration.getType(); Queue queue = new Queue(length, cls); kryo.reference(queue); Class elementClass = null; Serializer serializer = null; if (genericType != null) { elementClass = genericType; serializer = kryo.getSerializer(genericType); genericType = null; } if (serializer != null) { for (int i = 0; i < length; i++) queue.addLast(kryo.readObjectOrNull(input, elementClass, serializer)); } else { for (int i = 0; i < length; i++) queue.addLast(kryo.readClassAndObject(input)); } return queue; }
@Override public void write(final Kryo kryo, final Output output, final RegisteredService service) { kryo.writeObject(output, service.getServiceId()); kryo.writeObject(output, StringUtils.defaultIfEmpty(service.getName(), StringUtils.EMPTY)); kryo.writeObject(output, StringUtils.defaultIfEmpty(service.getDescription(), StringUtils.EMPTY)); kryo.writeObject(output, service.getId()); kryo.writeObject(output, service.getEvaluationOrder()); kryo.writeObject(output, ObjectUtils.defaultIfNull(service.getLogo(), getEmptyUrl())); kryo.writeObject(output, service.getLogoutType()); kryo.writeObject(output, ObjectUtils.defaultIfNull(service.getLogoutUrl(), getEmptyUrl())); kryo.writeObject(output, new HashSet<>(service.getRequiredHandlers())); kryo.writeObject(output, StringUtils.defaultIfEmpty(service.getTheme(), StringUtils.EMPTY)); writeObjectByReflection(kryo, output, ObjectUtils.defaultIfNull(service.getPublicKey(), new RegisteredServicePublicKeyImpl())); writeObjectByReflection(kryo, output, ObjectUtils.defaultIfNull(service.getProxyPolicy(), new RefuseRegisteredServiceProxyPolicy())); writeObjectByReflection(kryo, output, ObjectUtils.defaultIfNull(service.getAttributeReleasePolicy(), new ReturnAllowedAttributeReleasePolicy())); writeObjectByReflection(kryo, output, ObjectUtils.defaultIfNull(service.getUsernameAttributeProvider(), new DefaultRegisteredServiceUsernameProvider())); writeObjectByReflection(kryo, output, ObjectUtils.defaultIfNull(service.getAccessStrategy(), new DefaultRegisteredServiceAccessStrategy())); }
@Override public RegisteredService read(final Kryo kryo, final Input input, final Class<RegisteredService> type) { final AbstractRegisteredService svc = new RegexRegisteredService(); svc.setServiceId(kryo.readObject(input, String.class)); svc.setName(kryo.readObject(input, String.class)); svc.setDescription(kryo.readObject(input, String.class)); svc.setId(kryo.readObject(input, Long.class)); svc.setEvaluationOrder(kryo.readObject(input, Integer.class)); svc.setLogo(kryo.readObject(input, URL.class)); svc.setLogoutType(kryo.readObject(input, LogoutType.class)); svc.setLogoutUrl(kryo.readObject(input, URL.class)); svc.setRequiredHandlers(kryo.readObject(input, HashSet.class)); svc.setTheme(kryo.readObject(input, String.class)); svc.setPublicKey(readObjectByReflection(kryo, input)); svc.setProxyPolicy(readObjectByReflection(kryo, input)); svc.setAttributeReleasePolicy(readObjectByReflection(kryo, input)); svc.setUsernameAttributeProvider(readObjectByReflection(kryo, input)); svc.setAccessStrategy(readObjectByReflection(kryo, input)); return svc; }
@Override public final void read (Kryo kryo, Input input) { pushHeader(kryo, this); input.readInt(true); //if this class ever evolves, version can be used for backward compatibility Class dataType = kryo.readClass(input).getType(); gdxMajorVersion = input.readInt(true); gdxMinorVersion = input.readInt(true); gdxRevisionVersion = input.readInt(true); writtenVersion = input.readInt(true); minimumReadVersion = input.readInt(true); minimumReadVersionString = input.readString(); useCompactColor = input.readBoolean(); includePixmapDrawingParams = input.readBoolean(); readExtra(kryo, input); if (dataType != null && minimumReadVersion <= currentReadWriteVersion){ data = (T)kryo.readObject(input, dataType); } popHeader(kryo); }
public void write (Kryo kryo, Output output, LongMap map) { int length = map.size; output.writeVarInt(length, true); output.writeBoolean(false); // whether type is written (in case future version of LongMap supports type awareness) Serializer valueSerializer = null; if (valueGenericType != null) { if (valueSerializer == null) valueSerializer = kryo.getSerializer(valueGenericType); valueGenericType = null; } for (Iterator iter = map.iterator(); iter.hasNext();) { LongMap.Entry entry = (LongMap.Entry)iter.next(); output.writeLong(entry.key); if (valueSerializer != null) { kryo.writeObjectOrNull(output, entry.value, valueSerializer); } else kryo.writeClassAndObject(output, entry.value); } }
/** * 序列化 * * @param obj 需要序更列化的对象 * @return 序列化后的byte 数组 * @throws MythException 异常 */ @Override public byte[] serialize(Object obj) throws MythException { byte[] bytes; try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { //获取kryo对象 Kryo kryo = new Kryo(); Output output = new Output(outputStream); kryo.writeObject(output, obj); bytes = output.toBytes(); output.flush(); } catch (Exception ex) { throw new MythException("kryo serialize error" + ex.getMessage()); } return bytes; }
@Override public IpPrefix read(Kryo kryo, Input input, Class<IpPrefix> type) { int octLen = input.readInt(); checkArgument(octLen <= IpAddress.INET6_BYTE_LENGTH); byte[] octs = new byte[octLen]; input.readBytes(octs); int prefLen = input.readInt(); // Use the address size to decide whether it is IPv4 or IPv6 address if (octLen == IpAddress.INET_BYTE_LENGTH) { return IpPrefix.valueOf(IpAddress.Version.INET, octs, prefLen); } if (octLen == IpAddress.INET6_BYTE_LENGTH) { return IpPrefix.valueOf(IpAddress.Version.INET6, octs, prefLen); } return null; // Shouldn't be reached }
@Override public ByteBuf encode(Object in) throws IOException { Kryo kryo = null; ByteBuf out = ByteBufAllocator.DEFAULT.buffer(); try { ByteBufOutputStream baos = new ByteBufOutputStream(out); Output output = new Output(baos); kryo = kryoPool.get(); kryo.writeClassAndObject(output, in); output.close(); return baos.buffer(); } catch (Exception e) { out.release(); if (e instanceof RuntimeException) { throw (RuntimeException) e; } throw new RedissonKryoCodecException(e); } finally { if (kryo != null) { kryoPool.yield(kryo); } } }
/** * Returns a new {@link LogicalPlanDeserializer} * @param cluster cluster to inject during deserialization * @param catalog catalog used during deserializing tables * @param registry registry used during deserializing storage plugins */ public static LogicalPlanDeserializer forDeserialization(final RelOptCluster cluster, final CalciteCatalogReader catalog, final StoragePluginRegistry registry) { final Kryo kryo = new Kryo(); kryo.getFieldSerializerConfig().setUseAsm(true); final RelSerializer serializer = RelSerializer.newBuilder(kryo, cluster, catalog, registry).build(); return new LogicalPlanDeserializer() { @Override public RelNode deserialize(final byte[] data) { try { return serializer.deserialize(data); } catch (Throwable e) { throw new KryoDeserializationException(e); } } }; }
public void write(Kryo kryo, Output output, ArrayMetaData<int[]> object) { try { int[] arrData = arrayMetaData.getArrData(); arrayMetaData.send(output); int arrSegNum = arrayMetaData.getSegNum(); for (int i = 0; i < arrSegNum; i++) { int from = arrayMetaData.getFrom(i); int to = arrayMetaData.getTo(i); for (int j = from; j < to; j++) { output.writeInt(arrData[j]); } } } catch (IOException e) { LOG.error("double array write exception", e); System.exit(1); } }
public void write(Kryo kryo, Output output, MapMetaData<Double> object) { try { List<Map<String, Double>> mapDataList = mapMetaData.getMapDataList(); mapMetaData.send(output); int mapSegNum = mapMetaData.getSegNum(); for (int i = 0; i < mapSegNum; i++) { Map<String, Double> mapData = mapDataList.get(i); for (Map.Entry<String, Double> entry : mapData.entrySet()) { output.writeString(entry.getKey()); output.writeDouble(entry.getValue()); } if (mapMetaData.getCollective() == Collective.GATHER || mapMetaData.getCollective() == Collective.SCATTER || mapMetaData.getCollective() == Collective.REDUCE_SCATTER) { mapData.clear(); } } } catch (IOException e) { LOG.error("double array write exception", e); System.exit(1); } }
@Override public IntSet read(Kryo kryo, Input input, Class<IntSet> type) { int length = input.readVarInt(true); IntSet array = new IntSet(length); for (int i = 0; i < length; i++) { array.add(input.readInt()); } return array; }
/** * Serializes given object to OutputStream using Kryo instance in pool. * * @param obj Object to serialize * @param stream to write to * @param bufferSize size of the buffer in front of the stream */ public void serialize(final Object obj, final OutputStream stream, final int bufferSize) { ByteBufferOutput out = new ByteBufferOutput(stream, bufferSize); Kryo kryo = borrow(); try { kryo.writeClassAndObject(out, obj); out.flush(); } finally { release(kryo); } }
public static Object readObject(InputStream inputStream) { Kryo kryo = new Kryo(); kryo.register(java.lang.invoke.SerializedLambda.class); kryo.register(ClosureSerializer.Closure.class, new ClosureSerializer()); kryo.setInstantiatorStrategy(new Kryo.DefaultInstantiatorStrategy(new StdInstantiatorStrategy())); Input input = new Input(inputStream); return kryo.readClassAndObject(input); }
/** * Register {@code type} and {@code serializer} to {@code kryo} instance. * * @param kryo Kryo instance * @param type type to register * @param serializer Specific serializer to register or null to use default. * @param id type registration id to use */ private void register(Kryo kryo, Class<?> type, Serializer<?> serializer, int id) { Registration existing = kryo.getRegistration(id); if (existing != null) { if (existing.getType() != type) { log.error("{}: Failed to register {} as {}, {} was already registered.", friendlyName(), type, id, existing.getType()); throw new IllegalStateException(String.format( "Failed to register %s as %s, %s was already registered.", type, id, existing.getType())); } // falling through to register call for now. // Consider skipping, if there's reasonable // way to compare serializer equivalence. } Registration r; if (serializer == null) { r = kryo.register(type, id); } else { r = kryo.register(type, serializer, id); } if (r.getId() != id) { log.warn("{}: {} already registed as {}. Skipping {}.", friendlyName(), r.getType(), r.getId(), id); } log.trace("{} registered as {}", r.getType(), r.getId()); }
private <T> T clone(T obj){ Kryo kryo = kryoFactory.getKryo(); try { Output output = new Output(1024, 1024 * 500); kryo.writeClassAndObject(output, obj); output.flush(); Input input = new Input(output.toBytes()); T t = (T) kryo.readClassAndObject(input); return t; } finally { kryoFactory.returnKryo(kryo);; } }
public MapMetaData<String> read(Kryo kryo, Input input, Class<MapMetaData<String>> type) { try { thatMapMetaData = mapMetaData.recv(input); int thatMapSegNum = thatMapMetaData.getSegNum(); List<Map<String, String>> thatMapListData = new ArrayList<>(thatMapSegNum); List<Integer> thatDataNums = new ArrayList<>(thatMapSegNum); for (int i = 0; i < thatMapSegNum; i++) { Map<String, String> thisMapData = mapMetaData.getMapDataList().get(i); int dataNum = thatMapMetaData.getDataNum(i); for (int j = 0; j < dataNum; j++) { String key = input.readString(); String val = input.readString(); String thisVal = thisMapData.get(key); if (thisVal == null) { thisMapData.put(key, val); } else { thisMapData.put(key, operator.apply(thisVal, val)); } } thatMapListData.add(thisMapData); thatDataNums.add(thisMapData.size()); } thatMapMetaData.setMapDataList(thatMapListData); thatMapMetaData.setDataNums(thatDataNums); } catch (IOException e) { LOG.error("double array read exception", e); System.exit(1); } return thatMapMetaData; }
@Override public void write(Kryo kryo, Output output, Segment segment) { Vector3 a = segment.a; output.writeFloat(a.x); output.writeFloat(a.y); output.writeFloat(a.z); Vector3 b = segment.b; output.writeFloat(b.x); output.writeFloat(b.y); output.writeFloat(b.z); }
@Override public Ray read(Kryo kryo, Input input, Class<Ray> type) { Ray ray = new Ray(); Vector3 origin = ray.origin; origin.x = input.readFloat(); origin.y = input.readFloat(); origin.z = input.readFloat(); Vector3 direction = ray.direction; direction.x = input.readFloat(); direction.y = input.readFloat(); direction.z = input.readFloat(); return ray; }
public static ByteBuffer toByteBuffer(Object obj){ Kryo kryo = new Kryo(); Output output = new Output(1024*1024); kryo.writeClassAndObject(output, obj); output.close(); return ByteBuffer.wrap(output.toBytes()); }
@Override public void write(Kryo kryo, Output output, Sphere sphere) { Vector3 center = sphere.center; output.writeFloat(center.x); output.writeFloat(center.y); output.writeFloat(center.z); output.writeFloat(sphere.radius); }
@Override public T read(final Kryo kryo, final Input input, final Class<T> type) { final boolean isKnown = kryo.readObject(input, Boolean.class); final T result; if (isKnown) { final RelDistribution.Type kind = kryo.readObject(input, RelDistribution.Type.class); result = (T)distributionMap.get(kind); } else { result = super.read(kryo, input, type); } final T normalized = (T) result.getTraitDef().canonize(result); kryo.reference(normalized); return normalized; }
@Override public Ip4Prefix read(Kryo kryo, Input input, Class<Ip4Prefix> type) { int octLen = input.readInt(); checkArgument(octLen <= Ip4Address.BYTE_LENGTH); byte[] octs = new byte[octLen]; input.readBytes(octs); int prefLen = input.readInt(); return Ip4Prefix.valueOf(octs, prefLen); }
@Override protected SamlService createService(final Kryo kryo, final Input input, final String id, final String originalUrl, final String artifactId) { final String requestId = kryo.readObject(input, String.class); try { return (SamlService) CONSTRUCTOR.newInstance(id, originalUrl, artifactId, new SimpleHttpClientFactoryBean().getObject(), requestId); } catch (final Exception e) { throw new IllegalStateException("Error creating SamlService", e); } }
@Override public void write(Kryo kryo, Output output, SplitInfo object) { output.writeFloat(object.lossChg); output.writeInt(object.splitIndex); output.writeFloat(object.splitValue); output.writeInt(object.splitSlotInterval.length); for (int i = 0; i < object.splitSlotInterval.length; i++) { output.writeInt(object.splitSlotInterval[i]); } }
@Override public void write(Kryo kryo, Output output, Ellipse ellipse) { output.writeFloat(ellipse.x); output.writeFloat(ellipse.y); output.writeFloat(ellipse.width); output.writeFloat(ellipse.height); }
@Override public URI read(final Kryo kryo, final Input input, final Class<URI> type) { final String uriString = kryo.readObject(input, String.class); try { return new URI(uriString); } catch (final URISyntaxException e) { throw new RuntimeException(String.format("unable to deserialize URI from uri string: %s", uriString), e); } }
private static Kryo registerCollection(Kryo destination, Set<Class<?>> types) { List<Class<?>> sorted = sort(types); for (Class<?> registerableType : sorted) destination = registerType(destination, registerableType); return destination; }
public MapMetaData<T> read(Kryo kryo, Input input, Class<MapMetaData<T>> type) { try { thatMapMetaData = mapMetaData.recv(input); int thatMapSegNum = thatMapMetaData.getSegNum(); List<Map<String, T>> thatMapListData = new ArrayList<>(thatMapSegNum); List<Integer> thatDataNums = new ArrayList<>(thatMapSegNum); for (int i = 0; i < thatMapSegNum; i++) { Map<String, T> thisMapData = mapMetaData.getMapDataList().get(i); int dataNum = thatMapMetaData.getDataNum(i); for (int j = 0; j < dataNum; j++) { String key = input.readString(); T val = serializer.read(kryo, input, this.type); T thisVal = thisMapData.get(key); if (thisVal == null) { thisMapData.put(key, val); } else { thisMapData.put(key, operator.apply(thisVal, val)); } } thatMapListData.add(thisMapData); thatDataNums.add(thisMapData.size()); } thatMapMetaData.setMapDataList(thatMapListData); thatMapMetaData.setDataNums(thatDataNums); } catch (IOException e) { LOG.error("double array read exception", e); System.exit(1); } return thatMapMetaData; }
@Override public URL read(final Kryo kryo, final Input input, final Class<URL> type) { final String url = kryo.readObject(input, String.class); try { return new URL(url); } catch (final MalformedURLException e) { throw Throwables.propagate(e); } }
@Override public GridPoint3 read(Kryo kryo, Input input, Class<GridPoint3> type) { int x = input.readInt(); int y = input.readInt(); int z = input.readInt(); return new GridPoint3(x, y, z); }
@Override public void write(Kryo kryo, Output output, List<T> object) { output.writeInt(object.size()); for (T val : object) { valSerializer.write(kryo, output, val); } }
public static KnowledgeBase deserialise(File location) throws FileNotFoundException { System.out.println("Deserialising Knowledge Base"); Kryo kryo = KryoFactory.createKryoInstance(); Input input = new Input(new FileInputStream(location)); KnowledgeBase kb = kryo.readObject(input, KnowledgeBase.class); input.close(); return kb; }