Java Code Examples for com.esotericsoftware.kryo.Kryo.reference()

The following are Jave code examples for showing how to use reference() of the com.esotericsoftware.kryo.Kryo class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: gdx-cclibs   File: QueueSerializer.java   Source Code and License Vote up 6 votes
@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 2
Project: gdx-cclibs   File: SortedIntListSerializer.java   Source Code and License Vote up 6 votes
@Override
public SortedIntList read(Kryo kryo, Input input, Class<SortedIntList> type) {
    int length = input.readVarInt(true);
    SortedIntList list = new SortedIntList();
    kryo.reference(list);
    Class elementClass = null;
    Serializer serializer = null;
    if (genericType != null) {
        elementClass = genericType;
        serializer = kryo.getSerializer(genericType);
        genericType = null;
    }
    for (int i = 0; i < length; i++) {
        int index = input.readInt();
        Object value = serializer != null ?
                kryo.readObjectOrNull(input, elementClass, serializer) : kryo.readClassAndObject(input);
        list.insert(index, value);
    }
    return list;
}
 
Example 3
Project: gdx-cclibs   File: ArraySerializer.java   Source Code and License Vote up 6 votes
@Override
public T read(Kryo kryo, Input input, Class<T> type) {
    int length = input.readVarInt(true);
    boolean ordered = input.readBoolean();
    Class cls = kryo.readClass(input).getType();
    T array = create(ordered, length, cls);
    kryo.reference(array);
    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++)
            array.add(kryo.readObjectOrNull(input, elementClass, serializer));
    } else {
        for (int i = 0; i < length; i++)
            array.add(kryo.readClassAndObject(input));
    }
    return array;
}
 
Example 4
Project: gdx-cclibs   File: ObjectSetSerializer.java   Source Code and License Vote up 6 votes
@Override
public T read(Kryo kryo, Input input, Class<T> type) {
    int length = input.readVarInt(true);
    input.readBoolean(); // currently unused
    T objectSet = create(length);

    kryo.reference(objectSet);

    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++)
            objectSet.add(kryo.readObject(input, elementClass, serializer));
    } else {
        for (int i = 0; i < length; i++)
            objectSet.add(kryo.readClassAndObject(input));
    }
    return objectSet;
}
 
Example 5
Project: gdx-cclibs   File: ObjectMapSerializer.java   Source Code and License Vote up 5 votes
public T read (Kryo kryo, Input input, Class<T> type) {
    int length = input.readVarInt(true);
    input.readBoolean(); // currently unused
    T map = create(length);

    Class keyClass = null;
    Class valueClass = null;

    Serializer keySerializer = null;
    if (keyGenericType != null) {
        keyClass = keyGenericType;
        if (keySerializer == null) keySerializer = kryo.getSerializer(keyClass);
        keyGenericType = null;
    }
    Serializer valueSerializer = null;
    if (valueGenericType != null) {
        valueClass = valueGenericType;
        if (valueSerializer == null) valueSerializer = kryo.getSerializer(valueClass);
        valueGenericType = null;
    }

    kryo.reference(map);

    for (int i = 0; i < length; i++) {
        Object key;
        if (keySerializer != null) {
            key = kryo.readObject(input, keyClass, keySerializer);
        } else
            key = kryo.readClassAndObject(input);
        Object value;
        if (valueSerializer != null) {
            value = kryo.readObjectOrNull(input, valueClass, valueSerializer);
        } else
            value = kryo.readClassAndObject(input);
        map.put(key, value);
    }
    return map;
}
 
Example 6
Project: gdx-cclibs   File: ObjectFloatMapSerializer.java   Source Code and License Vote up 5 votes
public ObjectFloatMap read (Kryo kryo, Input input, Class<ObjectFloatMap> type) {
    int length = input.readVarInt(true);
    input.readBoolean(); // currently unused
    ObjectFloatMap map = create(length);

    Class keyClass = null;

    Serializer keySerializer = null;
    if (keyGenericType != null) {
        keyClass = keyGenericType;
        if (keySerializer == null) keySerializer = kryo.getSerializer(keyClass);
        keyGenericType = null;
    }

    kryo.reference(map);

    for (int i = 0; i < length; i++) {
        Object key;
        if (keySerializer != null) {
            key = kryo.readObject(input, keyClass, keySerializer);
        } else
            key = kryo.readClassAndObject(input);
        float value = input.readFloat();
        map.put(key, value);
    }
    return map;
}
 
Example 7
Project: dremio-oss   File: WritableSerializer.java   Source Code and License Vote up 5 votes
@Override
public T read(final Kryo kryo, final Input input, final Class<T> type) {
  try {
    final T object = kryo.newInstance(type);
    kryo.reference(object);
    object.readFields(new DataInputStream(input));
    return object;
  } catch (final IOException e) {
    throw new RuntimeException("unable to deserialize Writable object", e);
  }
}
 
Example 8
Project: dremio-oss   File: ImmutableCollectionSerializers.java   Source Code and License Vote up 5 votes
@Override
public ImmutableMap<Object, Object> read(Kryo kryo, Input input, Class<ImmutableMap<Object, ? extends Object>> type) {
  Map map = kryo.readObject(input, HashMap.class);
  final ImmutableMap<Object, Object> result = ImmutableMap.copyOf(map);
  kryo.reference(result);
  return result;
}
 
Example 9
Project: gdx-cclibs   File: IdentityMapSerializer.java   Source Code and License Vote up 5 votes
public IdentityMap read (Kryo kryo, Input input, Class<IdentityMap> type) {
    int length = input.readVarInt(true);
    input.readBoolean(); // currently unused
    IdentityMap map = new IdentityMap(length);

    Class keyClass = null;
    Class valueClass = null;

    Serializer keySerializer = null;
    if (keyGenericType != null) {
        keyClass = keyGenericType;
        if (keySerializer == null) keySerializer = kryo.getSerializer(keyClass);
        keyGenericType = null;
    }
    Serializer valueSerializer = null;
    if (valueGenericType != null) {
        valueClass = valueGenericType;
        if (valueSerializer == null) valueSerializer = kryo.getSerializer(valueClass);
        valueGenericType = null;
    }

    kryo.reference(map);

    for (int i = 0; i < length; i++) {
        Object key;
        if (keySerializer != null) {
            key = kryo.readObject(input, keyClass, keySerializer);
        } else
            key = kryo.readClassAndObject(input);
        Object value;
        if (valueSerializer != null) {
            value = kryo.readObjectOrNull(input, valueClass, valueSerializer);
        } else
            value = kryo.readClassAndObject(input);
        map.put(key, value);
    }
    return map;
}
 
Example 10
Project: gdx-cclibs   File: LongMapSerializer.java   Source Code and License Vote up 5 votes
public LongMap read (Kryo kryo, Input input, Class<LongMap> type) {
    int length = input.readVarInt(true);
    input.readBoolean(); // currently unused
    LongMap map = new LongMap(length);

    Class valueClass = null;

    Serializer valueSerializer = null;
    if (valueGenericType != null) {
        valueClass = valueGenericType;
        if (valueSerializer == null) valueSerializer = kryo.getSerializer(valueClass);
        valueGenericType = null;
    }

    kryo.reference(map);

    for (int i = 0; i < length; i++) {
        long key = input.readLong();
        Object value;
        if (valueSerializer != null) {
            value = kryo.readObjectOrNull(input, valueClass, valueSerializer);
        } else
            value = kryo.readClassAndObject(input);
        map.put(key, value);
    }
    return map;
}
 
Example 11
Project: dremio-oss   File: ImmutableCollectionSerializers.java   Source Code and License Vote up 5 votes
@Override
public ImmutableList<Object> read(final Kryo kryo, final Input input, final Class<ImmutableList<Object>> type) {
  final int size = input.readInt(true);
  final Object[] values = new Object[size];

  for (int i = 0; i < size; ++i) {
    values[i] = kryo.readClassAndObject(input);
  }

  final ImmutableList<Object> result = ImmutableList.copyOf(values);
  kryo.reference(result);
  return result;
}
 
Example 12
Project: gdx-cclibs   File: ObjectIntMapSerializer.java   Source Code and License Vote up 5 votes
public ObjectIntMap read (Kryo kryo, Input input, Class<ObjectIntMap> type) {
    int length = input.readVarInt(true);
    input.readBoolean(); // currently unused
    ObjectIntMap map = create(length);

    Class keyClass = null;

    Serializer keySerializer = null;
    if (keyGenericType != null) {
        keyClass = keyGenericType;
        if (keySerializer == null) keySerializer = kryo.getSerializer(keyClass);
        keyGenericType = null;
    }

    kryo.reference(map);

    for (int i = 0; i < length; i++) {
        Object key;
        if (keySerializer != null) {
            key = kryo.readObject(input, keyClass, keySerializer);
        } else
            key = kryo.readClassAndObject(input);
        int value = input.readInt();
        map.put(key, value);
    }
    return map;
}
 
Example 13
Project: dremio-oss   File: RelTraitSerializers.java   Source Code and License Vote up 5 votes
@Override
public T read(final Kryo kryo, final Input input, final Class<T> type) {
  final boolean isNone = kryo.readObject(input, Boolean.class);
  if (isNone) {
    return (T)Convention.NONE;
  }
  final T result = super.read(kryo, input, type);
  final T normalized = (T) result.getTraitDef().canonize(result);
  kryo.reference(normalized);
  return normalized;
}
 
Example 14
Project: dremio-oss   File: RelTraitSerializers.java   Source Code and License Vote up 5 votes
@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 15
Project: dremio-oss   File: RelTraitSerializers.java   Source Code and License Vote up 5 votes
@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 DistributionTrait.DistributionType kind = kryo.readObject(input, DistributionTrait.DistributionType.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 16
Project: dremio-oss   File: SqlOperatorSerializer.java   Source Code and License Vote up 5 votes
@Override
public T read(final Kryo kryo, final Input input, final Class<T> type) {
  final boolean isKnown = kryo.readObject(input, Boolean.class);
  if (isKnown) {
    final Integer ordinal = kryo.readObject(input, Integer.class);
    final SqlOperator operator = OperatorPopulator.BACKWARD.get(ordinal);
    if (operator != null) {
      kryo.reference(operator);
      return (T)operator;
    }
    throw new IllegalStateException(String.format("Unable to locate operator with ordinal [%s]", ordinal));
  }

  return super.read(kryo, input, type);
}
 
Example 17
Project: dremio-oss   File: RelTraitSerializers.java   Source Code and License Vote up 5 votes
@Override
public RelTraitSet read(final Kryo kryo, final Input input, final Class<RelTraitSet> type) {

  final int size = kryo.readObject(input, Integer.class);
  final RelTrait[] traits = new RelTrait[size];
  for (int i = 0; i < size; i++) {
    final RelTrait trait = (RelTrait) kryo.readClassAndObject(input);
    // normalize trait so that stupid calcite won't complain about == checks.
    traits[i] = trait.getTraitDef().canonize(trait);
  }

  final String digest = kryo.readObject(input, String.class);
  final Object cache = kryo.readClassAndObject(input);

  final RelTraitSet traitSet = kryo.newInstance(type);

  try {
    getField(NAME_CACHE).set(traitSet, cache);
    getField(NAME_TRAITS).set(traitSet, traits);
    getField(NAME_STRING).set(traitSet, digest);
  } catch (final NoSuchFieldException|IllegalAccessException e) {
    throw new RuntimeException("unable to deserialize TraitSet", e);
  }

  kryo.reference(traitSet);
  return traitSet;
}
 
Example 18
Project: dremio-oss   File: ImmutableCollectionSerializers.java   Source Code and License Vote up 5 votes
@Override
public ImmutableSet<Object> read(final Kryo kryo, final Input input, final Class<ImmutableSet<Object>> type) {
  final int size = input.readInt(true);

  final ImmutableSet.Builder builder = ImmutableSet.builder();

  for (int i = 0; i < size; ++i) {
    builder.add(kryo.readClassAndObject(input));
  }

  final ImmutableSet<Object> result = builder.build();
  kryo.reference(result);
  return result;
}
 
Example 19
Project: dremio-oss   File: RelDataTypeSerializer.java   Source Code and License Vote up 5 votes
@Override
public T read(final Kryo kryo, final Input input, final Class<T> type) {
  // do not use delegate.read because we do not want it to cache the object. Rather, we will cache the normalized type.
  final T dataType = kryo.newInstance(type);
  final FieldSerializer.CachedField[] fields = delegate.getFields();
  for (int i = 0, n = fields.length; i < n; i++) {
    fields[i].read(input, dataType);
  }

  // be gentle to calcite and normalize the returned data type. normalization here means to use same type instances.
  final T result = (T) typeFactory.copyType(dataType);
  kryo.reference(result);
  return result;
}
 
Example 20
Project: dremio-oss   File: RelTraitDefSerializers.java   Source Code and License Vote up 4 votes
@Override
public T read(final Kryo kryo, final Input input, final Class<T> type) {
  final T result = factory.get();
  kryo.reference(result);
  return result;
}