Java Code Examples for com.esotericsoftware.kryo.Kryo

The following are top voted examples for showing how to use com.esotericsoftware.kryo.Kryo. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: athena   File: ExtensionCriterionSerializer.java   Source Code and License 6 votes vote down vote up
@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);
}
 
Example 2
Project: ytk-mp4j   File: LongOperand.java   Source Code and License 6 votes vote down vote up
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);
    }
}
 
Example 3
Project: ytk-mp4j   File: ShortOperand.java   Source Code and License 6 votes vote down vote up
@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;
}
 
Example 4
Project: T2KMatch   File: KryoFactory.java   Source Code and License 6 votes vote down vote up
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;
}
 
Example 5
Project: springboot-shiro-cas-mybatis   File: RegisteredServiceSerializer.java   Source Code and License 6 votes vote down vote up
@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;
}
 
Example 6
Project: gdx-cclibs   File: IntMapSerializer.java   Source Code and License 6 votes vote down vote up
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);
    }
}
 
Example 7
Project: gdx-cclibs   File: ObjectFloatMapSerializer.java   Source Code and License 6 votes vote down vote up
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);
    }
}
 
Example 8
Project: gdx-cclibs   File: QueueSerializer.java   Source Code and License 6 votes vote down vote up
@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;
}
 
Example 9
Project: cas-5.1.0   File: RegisteredServiceSerializer.java   Source Code and License 6 votes vote down vote up
@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()));
}
 
Example 10
Project: cas-5.1.0   File: RegisteredServiceSerializer.java   Source Code and License 6 votes vote down vote up
@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;
}
 
Example 11
Project: gdx-cclibs   File: GraphHeader.java   Source Code and License 6 votes vote down vote up
@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);
}
 
Example 12
Project: gdx-cclibs   File: LongMapSerializer.java   Source Code and License 6 votes vote down vote up
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);
    }
}
 
Example 13
Project: myth   File: KryoSerializer.java   Source Code and License 6 votes vote down vote up
/**
 * 序列化
 *
 * @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;
}
 
Example 14
Project: athena   File: IpPrefixSerializer.java   Source Code and License 6 votes vote down vote up
@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
}
 
Example 15
Project: JRediClients   File: KryoCodec.java   Source Code and License 6 votes vote down vote up
@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);
        }
    }
}
 
Example 16
Project: dremio-oss   File: KryoLogicalPlanSerializers.java   Source Code and License 6 votes vote down vote up
/**
 * 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);
      }
    }

  };
}
 
Example 17
Project: ytk-mp4j   File: IntOperand.java   Source Code and License 6 votes vote down vote up
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);
    }
}
 
Example 18
Project: ytk-mp4j   File: DoubleOperand.java   Source Code and License 6 votes vote down vote up
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);
    }
}
 
Example 19
Project: hype   File: SerializationUtil.java   Source Code and License 5 votes vote down vote up
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);
}
 
Example 20
Project: athena   File: KryoNamespace.java   Source Code and License 5 votes vote down vote up
/**
 * 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());
}
 
Example 21
Project: hibatis   File: CacheContext.java   Source Code and License 5 votes vote down vote up
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);;
	}
}
 
Example 22
Project: ytk-mp4j   File: StringOperand.java   Source Code and License 5 votes vote down vote up
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;
}
 
Example 23
Project: gdx-cclibs   File: SegmentSerializer.java   Source Code and License 5 votes vote down vote up
@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);
}
 
Example 24
Project: gdx-cclibs   File: RaySerializer.java   Source Code and License 5 votes vote down vote up
@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;
}
 
Example 25
Project: algorithm.annotation   File: KryoTool.java   Source Code and License 5 votes vote down vote up
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());
}
 
Example 26
Project: gdx-cclibs   File: SphereSerializer.java   Source Code and License 5 votes vote down vote up
@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);
}
 
Example 27
Project: dremio-oss   File: RelTraitSerializers.java   Source Code and License 5 votes vote down vote up
@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;
}
 
Example 28
Project: athena   File: Ip4PrefixSerializer.java   Source Code and License 5 votes vote down vote up
@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);
}
 
Example 29
Project: springboot-shiro-cas-mybatis   File: SamlServiceSerializer.java   Source Code and License 5 votes vote down vote up
@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);
    }
}
 
Example 30
Project: ytk-learn   File: SplitInfo.java   Source Code and License 5 votes vote down vote up
@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]);
    }
}
 
Example 31
Project: gdx-cclibs   File: EllipseSerializer.java   Source Code and License 5 votes vote down vote up
@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);
}
 
Example 32
Project: dremio-oss   File: JavaSerializers.java   Source Code and License 5 votes vote down vote up
@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);
  }
}
 
Example 33
Project: MMORPG_Prototype   File: PacketsRegisterer.java   Source Code and License 5 votes vote down vote up
private static Kryo registerCollection(Kryo destination, Set<Class<?>> types)
{
    List<Class<?>> sorted = sort(types);
    for (Class<?> registerableType : sorted)
        destination = registerType(destination, registerableType);

    return destination;
}
 
Example 34
Project: ytk-mp4j   File: ObjectOperand.java   Source Code and License 5 votes vote down vote up
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;
}
 
Example 35
Project: cas-5.1.0   File: URLSerializer.java   Source Code and License 5 votes vote down vote up
@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);
    }
}
 
Example 36
Project: gdx-cclibs   File: GridPoint3Serializer.java   Source Code and License 5 votes vote down vote up
@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);
}
 
Example 37
Project: ytk-mp4j   File: ProcessCommSlave.java   Source Code and License 5 votes vote down vote up
@Override
public void write(Kryo kryo, Output output, List<T> object) {
    output.writeInt(object.size());
    for (T val : object) {
        valSerializer.write(kryo, output, val);
    }
}
 
Example 38
Project: T2KMatch   File: KnowledgeBase.java   Source Code and License 5 votes vote down vote up
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;
}
 
Example 39
Project: gdx-cclibs   File: AbstractBooleanArraySerializer.java   Source Code and License 5 votes vote down vote up
@Override
public void write(Kryo kryo, Output output, T bitSource) {
    int numBits = numBits(bitSource);
    output.writeInt(numBits, true);
    long[] array = new long[(numBits >>> 6) + 1];
    for (int i = 0; i < numBits; i++) {
        if (get(bitSource, i)) {
            final int word = i >>> 6;
            array[word] |= 1L << (i & 0x3F);
        }
    }
    output.writeLongs(array);
}
 
Example 40
Project: JRediClients   File: KryoCodec.java   Source Code and License 5 votes vote down vote up
/**
 * Sub classes can customize the Kryo instance by overriding this method
 *
 * @return create Kryo instance
 */
protected Kryo createInstance() {
    Kryo kryo = new Kryo();
    if (classLoader != null) {
        kryo.setClassLoader(classLoader);
    }
    kryo.setReferences(false);
    for (Class<?> clazz : classes) {
        kryo.register(clazz);
    }
    return kryo;
}
 
Example 41
Project: MobiRNN-EMDL17   File: Util.java   Source Code and License 5 votes vote down vote up
public static float[][][] getInputData(String folder) throws IOException {
    if (cachedInputs != null) {
        return cachedInputs;
    }

    final float[][][] inputs;
    final Kryo kryo = new Kryo();
    kryo.register(float[][][].class);
    final File dataBinFile = new File(getDataPath() + File.separator + "data-small.bin");
    if (dataBinFile.exists()) {
        Logger.i("begin reading input data bin: %s", dataBinFile.getAbsolutePath());
        Input input = new Input(new FileInputStream(dataBinFile));
        inputs = kryo.readObject(input, float[][][].class);
        input.close();
        Logger.i("begin reading input data bin: %s", dataBinFile.getAbsolutePath());
    } else {
        Logger.i("begin parsing input data");
        String inputFilePath =
                folder + File.separator + "test_data" + File.separator + "sensor";
        inputs = parseInputData(inputFilePath);
        Logger.i("end parsing input data");
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Output output = new Output(new FileOutputStream(dataBinFile));
                    kryo.writeObject(output, inputs);
                    output.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    cachedInputs = inputs;
    return cachedInputs;
}
 
Example 42
Project: athena   File: EncodableDiscreteResourcesSerializer.java   Source Code and License 5 votes vote down vote up
@Override
public EncodableDiscreteResources read(Kryo kryo, Input input, Class<EncodableDiscreteResources> cls) {
    DiscreteResource parent = kryo.readObject(input, DiscreteResource.class);
    @SuppressWarnings("unchecked")
    Set<EncodedDiscreteResources> resources = kryo.readObject(input, LinkedHashSet.class);

    return EncodableDiscreteResources.of(parent,
            resources.stream()
                    .flatMap(x -> x.values(parent.id()).stream())
                    .collect(Collectors.toCollection(LinkedHashSet::new)));
}
 
Example 43
Project: webapp-tyust   File: KryoSerialize.java   Source Code and License 5 votes vote down vote up
/**
 * kryo序列化
 * @param output
 * @param object
 * @throws IOException
 */
@Override
public void serialize(OutputStream output, Object object) throws IOException {
    Kryo kryo = pool.borrow();
    Output out = new Output(output);
    kryo.writeClassAndObject(out, object);
    out.close();
    output.close();
    pool.release(kryo);
}
 
Example 44
Project: gdx-cclibs   File: CatmullRomSplineSerializer.java   Source Code and License 5 votes vote down vote up
@Override
public void write(Kryo kryo, Output output, CatmullRomSpline catmullRomSpline) {
    Class<? extends Vector> vectorType = null;
    if (catmullRomSpline.controlPoints != null && catmullRomSpline.controlPoints.length > 0)
        vectorType = catmullRomSpline.controlPoints[0].getClass();
    kryo.writeClass(output, vectorType);
    output.writeInt(catmullRomSpline.controlPoints != null ? catmullRomSpline.controlPoints.length : -1); // -1 for null array
    if (catmullRomSpline.controlPoints != null){
        for (int i = 0; i < catmullRomSpline.controlPoints.length; i++) {
            kryo.writeObjectOrNull(output, catmullRomSpline.controlPoints[i], vectorType);
        }
    }
    output.writeBoolean(catmullRomSpline.continuous);
    output.writeInt(catmullRomSpline.spanCount, true);
}
 
Example 45
Project: athena   File: ProviderIdSerializer.java   Source Code and License 5 votes vote down vote up
@Override
public ProviderId read(Kryo kryo, Input input, Class<ProviderId> type) {
    String scheme = input.readString();
    String id = input.readString();
    boolean isAncillary = input.readBoolean();
    return new ProviderId(scheme, id, isAncillary);
}
 
Example 46
Project: gdx-cclibs   File: EllipseSerializer.java   Source Code and License 5 votes vote down vote up
@Override
public Ellipse read(Kryo kryo, Input input, Class<Ellipse> type) {
    float x = input.readFloat();
    float y = input.readFloat();
    float width = input.readFloat();
    float height = input.readFloat();
    return new Ellipse(x, y, width, height);
}
 
Example 47
Project: pac4j-plus   File: FormattedDateSerializer.java   Source Code and License 5 votes vote down vote up
@Override
public FormattedDate read(Kryo kryo, Input input, Class<FormattedDate> aClass) {
    final Long time = this.longSerializer.read(kryo, input, Long.class);
    final String format = this.stringSerializer.read(kryo, input, String.class);
    final Locale locale = this.localeSerializer.read(kryo, input, Locale.class);
    return new FormattedDate(new Date(time), format, locale);
}
 
Example 48
Project: gdx-cclibs   File: IntSetSerializer.java   Source Code and License 5 votes vote down vote up
@Override
public void write(Kryo kryo, Output output, IntSet set) {
    output.writeVarInt(set.size, true);
    IntSet.IntSetIterator iter = set.iterator();
    while (iter.hasNext) {
        output.writeInt(iter.next());
    }
}
 
Example 49
Project: ytk-mp4j   File: ProcessCommSlave.java   Source Code and License 5 votes vote down vote up
@Override
public List<T> read(Kryo kryo, Input input, Class<List<T>> type) {
    int size = input.readInt();
    List<T> list = new ArrayList<T>(size);
    for (int i = 0; i < size; i++) {
        list.add(valSerializer.read(kryo, input, this.valType));
    }
    return list;
}
 
Example 50
Project: gdx-cclibs   File: ObjectFloatMapSerializer.java   Source Code and License 5 votes vote down vote up
public void setGenerics (Kryo kryo, Class[] generics) {
    keyGenericType = null;

    if (generics != null && generics.length > 0) {
        if (generics[0] != null && kryo.isFinal(generics[0])) keyGenericType = generics[0];
    }
}
 
Example 51
Project: MMORPG_Prototype   File: PacketsRegisterer.java   Source Code and License 5 votes vote down vote up
public static Kryo registerAllAnnotated(Kryo destination, Class<? extends Annotation> annotationType,
          String sourcePackageName)
  {
Reflections reflections = new Reflections(getUsedPackageName());
      Set<Class<?>> registerableTypes = reflections.getTypesAnnotatedWith(annotationType);
      destination = registerCollection(destination, registerableTypes);
      Set<Class<?>> registerableBaseTypes = reflections.getTypesAnnotatedWith(defaultAnnotationBase);
      destination = registerSubTypesOf(destination, registerableBaseTypes);
      return destination;
  }
 
Example 52
Project: GabrielBot   File: KryoUtils.java   Source Code and License 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T> T unserialize(Kryo kryo, byte[] data) {
    Input in = new Input(new ByteArrayInputStream(checkNotNull(data, "data")));
    Object o = checkNotNull(kryo, "kryo").readClassAndObject(in);
    in.close();
    return (T)o;
}
 
Example 53
Project: gdx-cclibs   File: IntMapSerializer.java   Source Code and License 5 votes vote down vote up
public void setGenerics (Kryo kryo, Class[] generics) {
    valueGenericType = null;

    if (generics != null && generics.length > 0) {
        if (generics[0] != null && kryo.isFinal(generics[0])) valueGenericType = generics[0];
    }
}
 
Example 54
Project: athena   File: EncodedResourcesSerializer.java   Source Code and License 5 votes vote down vote up
@Override
public EncodedDiscreteResources read(Kryo kryo, Input input, Class<EncodedDiscreteResources> cls) {
    @SuppressWarnings("unchecked")
    List<ClosedOpenRange> ranges = kryo.readObject(input, ArrayList.class);
    DiscreteResourceCodec codec = (DiscreteResourceCodec) kryo.readClassAndObject(input);

    RangeSet<Integer> rangeSet = TreeRangeSet.create();
    ranges.stream()
            .map(x -> Range.closedOpen(x.lowerBound(), x.upperBound()))
            .forEach(rangeSet::add);
    return new EncodedDiscreteResources(rangeSet, codec);
}
 
Example 55
Project: gdx-cclibs   File: IntSetSerializer.java   Source Code and License 4 votes vote down vote up
@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;
}
 
Example 56
Project: athena   File: KryoNamespace.java   Source Code and License 4 votes vote down vote up
/**
 * 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);
    }
}
 
Example 57
Project: dremio-oss   File: RelTraitDefSerializers.java   Source Code and License 4 votes vote down vote up
@Override
public void write(final Kryo kryo, final Output output, final T object) {}
 
Example 58
Project: dremio-oss   File: RelOptTableImplSerializer.java   Source Code and License 4 votes vote down vote up
@Override
public void write(final Kryo kryo, final Output output, final RelOptTableImpl relOptTable) {
  final List<String> path = relOptTable.getQualifiedName();
  kryo.writeObject(output, path);
}
 
Example 59
Project: cas4.0.x-server-wechat   File: AbstractWebApplicationServiceSerializer.java   Source Code and License 4 votes vote down vote up
@Override
public void write(final Kryo kryo, final Output output, final T service) {
    kryo.writeObject(output, service.getId());
    kryo.writeObject(output, fieldHelper.getFieldValue(service, "originalUrl"));
    kryo.writeObject(output, service.getArtifactId());
}
 
Example 60
Project: gdx-cclibs   File: Vector3Serializer.java   Source Code and License 4 votes vote down vote up
@Override
public Vector3 copy (Kryo kryo, Vector3 original) {
    return new Vector3(original);
}