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

The following examples show how to use com.esotericsoftware.kryo.Kryo#writeClassAndObject() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KryoSerializer.java    From journalkeeper with Apache License 2.0 6 votes vote down vote up
@Override
public byte[] serialize(Object entry) {
    if (entry == null) {
        return ArrayUtils.EMPTY_BYTE_ARRAY;
    }

    Kryo kryo = kryoPool.borrow();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(BUFFER_SIZE);
    Output output = new Output(outputStream);

    if (type == null) {
        kryo.writeClassAndObject(output, entry);
    } else {
        kryo.writeObject(output, entry);
    }
    kryoPool.release(kryo);
    output.flush();
    byte[] result = outputStream.toByteArray();
    output.close();
    return result;
}
 
Example 2
Source File: KryoRedisSerializer.java    From blog_demos with Apache License 2.0 6 votes vote down vote up
@Override
public byte[] serialize(T t) throws SerializationException {
    if (t == null) {
        return EMPTY_BYTE_ARRAY;
    }

    Kryo kryo = kryos.get();
    kryo.setReferences(false);
    kryo.register(clazz);

    try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
         Output output = new Output(baos)) {
        kryo.writeClassAndObject(output, t);
        output.flush();
        return baos.toByteArray();
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }

    return EMPTY_BYTE_ARRAY;
}
 
Example 3
Source File: NovelAdjacencyAndAltHaplotype.java    From gatk with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected void serialize(final Kryo kryo, final Output output) {
    output.writeString(leftJustifiedLeftRefLoc.getContig());
    output.writeInt(leftJustifiedLeftRefLoc.getStart());
    output.writeInt(leftJustifiedLeftRefLoc.getEnd());
    output.writeString(leftJustifiedRightRefLoc.getContig());
    output.writeInt(leftJustifiedRightRefLoc.getStart());
    output.writeInt(leftJustifiedRightRefLoc.getEnd());
    output.writeInt(strandSwitch.ordinal());

    kryo.writeClassAndObject(output, complication);

    output.writeInt(type.ordinal());

    if (altHaplotypeSequence==null) {
        output.writeBoolean(true);
    } else {
        output.writeBoolean(false);
        output.writeInt(altHaplotypeSequence.length);
        for (final byte b : altHaplotypeSequence) {
            output.writeByte(b);
        }
    }
}
 
Example 4
Source File: Kryo5Codec.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("IllegalCatch")
public ByteBuf encode(Object in) throws IOException {
    Kryo kryo = kryoPool.obtain();
    Output output = outputPool.obtain();
    ByteBuf out = ByteBufAllocator.DEFAULT.buffer();
    try {
        ByteBufOutputStream baos = new ByteBufOutputStream(out);
        output.setOutputStream(baos);
        kryo.writeClassAndObject(output, in);
        output.flush();
        return baos.buffer();
    } catch (RuntimeException e) {
        out.release();
        throw e;
    } finally {
        kryoPool.free(kryo);
        outputPool.free(output);
    }
}
 
Example 5
Source File: KryoSerializer.java    From cqengine with Apache License 2.0 6 votes vote down vote up
/**
 * Serializes the given object, using the given instance of Kryo serializer.
 *
 * @param object The object to serialize
 * @return The serialized form of the object as a byte array
 */
@Override
public byte[] serialize(O object) {
    if (object == null) {
        throw new NullPointerException("Object was null");
    }
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Output output = new Output(baos);
        Kryo kryo = kryoCache.get();
        if (polymorphic) {
            kryo.writeClassAndObject(output, object);
        }
        else {
            kryo.writeObject(output, object);
        }
        output.close();
        return baos.toByteArray();
    }
    catch (Throwable e) {
        throw new IllegalStateException("Failed to serialize object, object type: " + objectType + ". " +
                "Configure @PersistenceConfig.polymorphic if the collection will contain a mix of object types. " +
                "Use the KryoSerializer.validateObjectIsRoundTripSerializable() method " +
                "to test your object is compatible with CQEngine.", e);
    }
}
 
Example 6
Source File: KryoCodec.java    From redisson with Apache License 2.0 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 = 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) {
            yield(kryo);
        }
    }
}
 
Example 7
Source File: CpxVariantInducingAssemblyContigUnitTest.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test(groups = "sv", dataProvider = "forSerialization")
public void testSerialization(final CpxVariantInducingAssemblyContig cpxVariantInducingAssemblyContig) {
    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    final Output out = new Output(bos);
    final Kryo kryo = new Kryo();
    kryo.writeClassAndObject(out, cpxVariantInducingAssemblyContig);
    out.flush();

    final ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
    final Input in = new Input(bis);
    @SuppressWarnings("unchecked")
    final CpxVariantInducingAssemblyContig roundTrip = (CpxVariantInducingAssemblyContig) kryo.readClassAndObject(in);
    Assert.assertEquals(cpxVariantInducingAssemblyContig, roundTrip);
    Assert.assertEquals(cpxVariantInducingAssemblyContig.hashCode(), roundTrip.hashCode());
}
 
Example 8
Source File: Namespace.java    From atomix with Apache License 2.0 5 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 9
Source File: ImmutableListSerializer.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public void write(Kryo kryo, Output output, ImmutableList<?> object) {
    output.writeInt(object.size());
    for (Object e : object) {
        kryo.writeClassAndObject(output, e);
    }
}
 
Example 10
Source File: KryoSerialization.java    From tassal with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public byte[] serialize(final Object obj) throws SerializationException {
	LOGGER.info("Serializing object of type " + obj.getClass().getName()
			+ " to byte[]");
	final Kryo kryo = new Kryo();
	final ByteArrayOutputStream st = new ByteArrayOutputStream();
	final Output output = new Output(st);
	kryo.writeClassAndObject(output, obj);
	output.close();
	return st.toByteArray();
}
 
Example 11
Source File: ArraysAsListSerializer.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public void write(Kryo kryo, Output output, List<?> object) {
    output.writeInt(object.size(), true);
    for (Object elm : object) {
        kryo.writeClassAndObject(output, elm);
    }
}
 
Example 12
Source File: CubeImplTest.java    From cubedb with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void save(String saveFileName) throws IOException {
  Kryo kryo = CubeUtils.getKryoWithRegistrations();
  OutputStream zip = new BufferedOutputStream(new NullOutputStream());
  Output output = new Output(zip);
  kryo.writeClassAndObject(output, partitions);
  // zip.closeEntry();
  output.close();
  // zip.close();
}
 
Example 13
Source File: KryoSerialization.java    From tassal with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void serialize(final Object obj, final String filename)
		throws SerializationException {
	LOGGER.info("Serializing object of type " + obj.getClass().getName()
			+ " to " + filename);
	try {
		final Kryo kryo = new Kryo();
		final Output output = new Output(new FileOutputStream(filename));
		kryo.writeClassAndObject(output, obj);
		output.close();
	} catch (final FileNotFoundException e) {
		throw new ISerializationStrategy.SerializationException(e);
	}

}
 
Example 14
Source File: AlignedAssemblyUnitTest.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test(dataProvider="AlignedAssemblySerializationTest", groups = "sv")
public void testAlignedAssemblySerialization(final Integer assemblyID, final AlignedAssembly expectedAssembly) {

    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    final Output out = new Output(bos);
    final Kryo kryo = new Kryo();
    kryo.writeClassAndObject(out, expectedAssembly);
    out.flush();

    final ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
    final Input in = new Input(bis);
    @SuppressWarnings("unchecked")
    final AlignedAssembly roundTrip = (AlignedAssembly) kryo.readClassAndObject(in);
    Assert.assertEquals(roundTrip, expectedAssembly);
}
 
Example 15
Source File: PriorityQueueSerializer.java    From flink with Apache License 2.0 5 votes vote down vote up
public void write(Kryo k, Output o, PriorityQueue<?> q) {
	k.writeClassAndObject(o, getComparator(q));
	o.writeInt(q.size(), true);
	for (Object a : q) {
		k.writeClassAndObject(o, a);
		o.flush();
	}
}
 
Example 16
Source File: KryoDescriptorSerializer.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void encode(MultiFieldEncoder fieldEncoder, DataValueDescriptor dvd, boolean desc) throws StandardException {
		initializeForWrite();
		Object o = dvd.getObject();
              Kryo kryo = kryoPool.get();
              try {
                  kryo.writeClassAndObject(output, o);
                  fieldEncoder.encodeNextUnsorted(output.toBytes());
              } finally {
                  kryoPool.returnInstance(kryo);
              }
}
 
Example 17
Source File: InternalPortStatusEventSerializer.java    From onos with Apache License 2.0 4 votes vote down vote up
@Override
public void write(Kryo kryo, Output output, InternalPortStatusEvent event) {
    kryo.writeClassAndObject(output, event.providerId());
    kryo.writeObject(output, event.deviceId(), deviceIdSerializer());
    kryo.writeClassAndObject(output, event.portDescription());
}
 
Example 18
Source File: McastPathStoreKeySerializer.java    From onos with Apache License 2.0 4 votes vote down vote up
@Override
public void write(Kryo kryo, Output output, McastPathStoreKey object) {
    kryo.writeClassAndObject(output, object.mcastIp());
    kryo.writeClassAndObject(output, object.source());
}
 
Example 19
Source File: McastRoleStoreKeySerializer.java    From onos with Apache License 2.0 4 votes vote down vote up
@Override
public void write(Kryo kryo, Output output, McastRoleStoreKey object) {
    kryo.writeClassAndObject(output, object.mcastIp());
    output.writeString(object.deviceId().toString());
    kryo.writeClassAndObject(output, object.source());
}
 
Example 20
Source File: FilteredConnectPointSerializer.java    From onos with Apache License 2.0 4 votes vote down vote up
@Override
public void write(Kryo kryo, Output output, FilteredConnectPoint object) {
    kryo.writeClassAndObject(output, object.connectPoint());
    kryo.writeClassAndObject(output, object.trafficSelector());
}