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

The following are Jave code examples for showing how to use writeClassAndObject() 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: jetcache   File: KryoValueEncoder.java   Source Code and License Vote up 6 votes
@Override
public byte[] apply(Object value) {
    try {
        Kryo kryo = kryoThreadLocal.get();
        ByteArrayOutputStream bos = new ByteArrayOutputStream(256);
        Output output = new Output(bos);
        output.writeInt(IDENTITY_NUMBER);
        kryo.writeClassAndObject(output, value);
        output.close();
        return bos.toByteArray();
    } catch (Exception e) {
        StringBuilder sb = new StringBuilder("Kryo Encode error. ");
        sb.append("msg=").append(e.getMessage());
        throw new CacheEncodeException(sb.toString(), e);
    }
}
 
Example 2
Project: SparkSeq   File: TestKryo.java   Source Code and License Vote up 6 votes
private byte[] serializationObject(Kryo kryo, byte[] obj) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Output output = new Output(baos);
    kryo.writeClassAndObject(output, obj);
    output.flush();
    output.close();

    byte[] b = baos.toByteArray();
    try {
        baos.flush();
        baos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return b;
}
 
Example 3
Project: gdx-cclibs   File: SortedIntListSerializer.java   Source Code and License Vote up 6 votes
@Override
public void write(Kryo kryo, Output output, SortedIntList list) {
    output.writeVarInt(list.size(), true);
    Serializer serializer = null;
    if (genericType != null) {
        if (serializer == null) serializer = kryo.getSerializer(genericType);
        genericType = null;
    }
    for (Iterator<SortedIntList.Node> iter = list.iterator(); iter.hasNext();){
        SortedIntList.Node node = iter.next();
        output.writeInt(node.index);
        if (serializer != null) {
            kryo.writeObjectOrNull(output, node.value, serializer);
        } else {
            kryo.writeClassAndObject(output, node.value);
        }
    }
}
 
Example 4
Project: gdx-cclibs   File: LongMapSerializer.java   Source Code and License Vote up 6 votes
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 5
Project: SparkSeq   File: TestMapDataSerialization.java   Source Code and License Vote up 6 votes
private <T extends Serializable> byte[] serializationObject(T obj) {
    Kryo kryo = new Kryo();
    kryo.setReferences(false);
    kryo.register(obj.getClass(), new JavaSerializer());

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Output output = new Output(baos);
    kryo.writeClassAndObject(output, obj);
    output.flush();
    output.close();

    byte[] b = baos.toByteArray();
    try {
        baos.flush();
        baos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return b;
}
 
Example 6
Project: gdx-cclibs   File: ObjectIntMapSerializer.java   Source Code and License Vote up 6 votes
public void write (Kryo kryo, Output output, ObjectIntMap map) {
    int length = map.size;
    output.writeVarInt(length, true);
    output.writeBoolean(false); // whether type is written (in case future version of ObjectIntMap 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();) {
        ObjectIntMap.Entry entry = (ObjectIntMap.Entry)iter.next();
        if (keySerializer != null) {
            kryo.writeObject(output, entry.key, keySerializer);
        } else
            kryo.writeClassAndObject(output, entry.key);
        output.writeInt(entry.value);
    }
}
 
Example 7
Project: athena   File: KryoNamespace.java   Source Code and License Vote up 6 votes
/**
 * Serializes given object to byte array using Kryo instance in pool.
 *
 * @param obj Object to serialize
 * @param bufferSize maximum size of serialized bytes
 * @return serialized bytes
 */
public byte[] serialize(final Object obj, final int bufferSize) {
    ByteBufferOutput out = new ByteBufferOutput(bufferSize, MAX_BUFFER_SIZE);
    try {
        Kryo kryo = borrow();
        try {
            kryo.writeClassAndObject(out, obj);
            out.flush();
            return out.toBytes();
        } finally {
            release(kryo);
        }
    } finally {
        out.release();
    }
}
 
Example 8
Project: happylifeplat-transaction   File: KryoSerialize.java   Source Code and License Vote up 5 votes
@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 9
Project: athena   File: ImmutableListSerializer.java   Source Code and License Vote up 5 votes
@Override
public void write(Kryo kryo, Output output, ImmutableList<?> object) {
    output.writeInt(object.size());
    for (Object e : object) {
        kryo.writeClassAndObject(output, e);
    }
}
 
Example 10
Project: athena   File: EncodedResourcesSerializer.java   Source Code and License Vote up 5 votes
@Override
public void write(Kryo kryo, Output output, EncodedDiscreteResources object) {
    List<ClosedOpenRange> ranges = object.rangeSet().asRanges().stream()
            .map(ClosedOpenRange::of)
            .collect(Collectors.toList());
    kryo.writeObject(output, ranges);
    kryo.writeClassAndObject(output, object.codec());
}
 
Example 11
Project: dremio-oss   File: ImmutableCollectionSerializers.java   Source Code and License Vote up 5 votes
@Override
public void write(final Kryo kryo, final Output output, final ImmutableList<Object> object) {
  output.writeInt(object.size(), true);
  final UnmodifiableIterator iterator = object.iterator();

  while (iterator.hasNext()) {
    final Object value = iterator.next();
    kryo.writeClassAndObject(output, value);
  }
}
 
Example 12
Project: gdx-cclibs   File: ArrayMapSerializer.java   Source Code and License Vote up 5 votes
public void write (Kryo kryo, Output output, ArrayMap map) {
    output.writeBoolean(map.ordered);
    int length = map.size;
    output.writeVarInt(length, true);
    kryo.writeClass(output, map.keys.getClass().getComponentType());
    kryo.writeClass(output, map.values.getClass().getComponentType());

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

    for (Iterator iter = map.iterator(); iter.hasNext();) {
        ObjectMap.Entry entry = (ObjectMap.Entry)iter.next();
        if (keySerializer != null) {
            kryo.writeObject(output, entry.key, keySerializer);
        } else
            kryo.writeClassAndObject(output, entry.key);
        if (valueSerializer != null) {
            kryo.writeObjectOrNull(output, entry.value, valueSerializer);
        } else
            kryo.writeClassAndObject(output, entry.value);
    }
}
 
Example 13
Project: athena   File: DefaultLinkSerializer.java   Source Code and License Vote up 5 votes
@Override
public void write(Kryo kryo, Output output, DefaultLink object) {
    kryo.writeClassAndObject(output, object.providerId());
    kryo.writeClassAndObject(output, object.src());
    kryo.writeClassAndObject(output, object.dst());
    kryo.writeClassAndObject(output, object.type());
    kryo.writeClassAndObject(output, object.state());
    output.writeBoolean(object.isDurable());
}
 
Example 14
Project: dremio-oss   File: ImmutableCollectionSerializers.java   Source Code and License Vote up 5 votes
@Override
public void write(final Kryo kryo, final Output output, final ImmutableSet<Object> object) {
  output.writeInt(object.size(), true);
  final UnmodifiableIterator iterator = object.iterator();

  while (iterator.hasNext()) {
    final Object value = iterator.next();
    kryo.writeClassAndObject(output, value);
  }
}
 
Example 15
Project: athena   File: ConnectPointSerializer.java   Source Code and License Vote up 4 votes
@Override
public void write(Kryo kryo, Output output, ConnectPoint object) {
    kryo.writeClassAndObject(output, object.elementId());
    kryo.writeClassAndObject(output, object.port());
}
 
Example 16
Project: athena   File: InternalPortEventSerializer.java   Source Code and License Vote up 4 votes
@Override
public void write(Kryo kryo, Output output, InternalPortEvent event) {
    kryo.writeClassAndObject(output, event.providerId());
    kryo.writeClassAndObject(output, event.deviceId());
    kryo.writeClassAndObject(output, event.portDescriptions());
}
 
Example 17
Project: athena   File: ExtensionCriterionSerializer.java   Source Code and License Vote up 4 votes
@Override
public void write(Kryo kryo, Output output, ExtensionCriterion object) {
    kryo.writeClassAndObject(output, object.extensionSelector().type());
    kryo.writeClassAndObject(output, object.deviceId());
    kryo.writeClassAndObject(output, object.extensionSelector().serialize());
}
 
Example 18
Project: dremio-oss   File: StoragePluginSerializer.java   Source Code and License Vote up 4 votes
@Override
public void write(final Kryo kryo, final Output output, final StoragePlugin plugin) {
  kryo.writeClassAndObject(output, plugin.getConfig());
}
 
Example 19
Project: athena   File: HostLocationSerializer.java   Source Code and License Vote up 4 votes
@Override
public void write(Kryo kryo, Output output, HostLocation object) {
    kryo.writeClassAndObject(output, object.deviceId());
    kryo.writeClassAndObject(output, object.port());
    output.writeLong(object.time());
}
 
Example 20
Project: athena   File: DefaultOutboundPacketSerializer.java   Source Code and License Vote up 4 votes
@Override
public void write(Kryo kryo, Output output, DefaultOutboundPacket object) {
    kryo.writeClassAndObject(output, object.sendThrough());
    kryo.writeClassAndObject(output, object.treatment());
    kryo.writeClassAndObject(output, object.data().array());
}