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

The following examples show how to use com.esotericsoftware.kryo.Kryo#writeObject() . 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: SimplePipelineCompressor.java    From spliceengine with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public byte[] compress(Object o) throws IOException{
    if(o instanceof BulkWrites){
        BulkWrites bw = (BulkWrites)o;
        return PipelineEncoding.encode(txnOperationFactory,bw);
    }else {
        Output out = new Output(128,-1);
        Kryo kryo = kp.get();
        try{
            kryo.writeObject(out,o);
            out.flush();
            return out.getBuffer();
        }finally{
            kp.returnInstance(kryo);
        }
    }
}
 
Example 2
Source File: OperatorDiscoveryTest.java    From attic-apex-core with Apache License 2.0 6 votes vote down vote up
@Test
public void testTypeGraphSerializer() throws Exception
{
  String[] classFilePath = getClassFileInClasspath();
  OperatorDiscoverer operatorDiscoverer = new OperatorDiscoverer(classFilePath);
  operatorDiscoverer.buildTypeGraph();

  // make sure (de)serialization of type graph works without problem
  Kryo kryo = new Kryo();
  TypeGraph.TypeGraphSerializer tgs = new TypeGraph.TypeGraphSerializer();
  kryo.register(TypeGraph.class, tgs);
  ByteArrayOutputStream baos = new ByteArrayOutputStream(1024 * 1024 * 20);
  Output output = new Output(baos);
  kryo.writeObject(output, operatorDiscoverer.getTypeGraph());
  output.close();
  Input input = new Input(new ByteArrayInputStream(baos.toByteArray()));
  TypeGraph tg = kryo.readObject(input, TypeGraph.class);
}
 
Example 3
Source File: GenericSerdePerformanceTest.java    From attic-apex-malhar with Apache License 2.0 6 votes vote down vote up
@Test
public void testCompareSerdeForString()
{
  long beginTime = System.currentTimeMillis();
  testSerdeForString(new GenericSerde<String>(String.class));
  long genericSerdeCost = System.currentTimeMillis() - beginTime;
  logger.info("Generic Serde cost for String: {}", genericSerdeCost);

  beginTime = System.currentTimeMillis();
  testSerdeForString(new StringSerde());
  long stringSerdeCost = System.currentTimeMillis() - beginTime;
  logger.info("String Serde cost for String: {}", stringSerdeCost);

  beginTime = System.currentTimeMillis();
  Kryo kryo = new Kryo();
  for (int i = 0; i < serdeDataSize; ++i) {
    kryo.writeObject(buffer, "" + random.nextInt(1000));
    buffer.toSlice();
  }
  buffer.release();
  long kryoSerdeCost = System.currentTimeMillis() - beginTime;
  logger.info("Kryo Serde cost for String: {}", kryoSerdeCost);
}
 
Example 4
Source File: KryoSerializer.java    From tx-lcn with Apache License 2.0 6 votes vote down vote up
@Override
public byte[] serialize(Object obj) throws SerializerException {
    byte[] bytes;
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {
        //获取kryo对象
        Kryo kryo = new Kryo();
        Output output = new Output(outputStream);
        kryo.writeObject(output, obj);
        bytes = output.toBytes();
        output.flush();
    } catch (Exception ex) {
        throw new SerializerException("kryo serialize error" + ex.getMessage());
    } finally {
        try {
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {

        }
    }
    return bytes;
}
 
Example 5
Source File: EncoderDecoderKryo.java    From ambiverse-nlu with Apache License 2.0 6 votes vote down vote up
@Override public byte[] encode(T element) {
  logger.debug("Encoding using kryo.");
  Kryo kryo = kryos.get();
  int size = 0;
  Output output = new Output(0, 500000000);
  try {
    kryo.writeObject(output, element);
    //logger.info("Object size: " + output.toBytes().length);
    logger.debug("Encoding finished.");
    byte [] result = output.toBytes();
    size = result.length;
    return result;
  } finally {
    output.close();
    if(size < MAX_OBJECT_SIZE) { //Small objects will be put back into the pull, larger ones will be thrown away
      kryos.remove();
    }
  }

}
 
Example 6
Source File: SerializationTest.java    From rtree-3d with Apache License 2.0 6 votes vote down vote up
@Test
@Ignore
public void testSerializationRoundTrip() throws FileNotFoundException {
    // this test to see if can use kryo to serialize RTree instance
    List<Entry<Object, Point>> entries = GreekEarthquakes.entriesList();
    int maxChildren = 8;
    RTree<Object, Point> tree = RTree.maxChildren(maxChildren).<Object, Point> create()
            .add(entries);

    File file = new File("target/greek-serialized.kryo");
    file.delete();

    Kryo kryo = new Kryo();
    Output output = new Output(new FileOutputStream(file));
    kryo.writeObject(output, tree);
    output.close();

    Input input = new Input(new FileInputStream(file));
    kryo.setInstantiatorStrategy(new StdInstantiatorStrategy());
    @SuppressWarnings("unchecked")
    RTree<Object, Point> tree2 = kryo.readObject(input, RTree.class);
    assertNotNull(tree2);

}
 
Example 7
Source File: CVParticleSerializer.java    From StormCV with Apache License 2.0 5 votes vote down vote up
@Override
public void write(Kryo kryo, Output output, Type type) {
	output.writeLong(type.getRequestId());
	output.writeString(type.getStreamId());
	output.writeLong(type.getSequenceNr());
	kryo.writeObject(output, type.getMetadata());
	try {
		this.writeObject(kryo, output, type);
	} catch (Exception e) {
		//TODO
	}
	output.flush();
	//output.close();
}
 
Example 8
Source File: KryoSerializer.java    From javabase with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    Kryo kryo = new Kryo();
    SerializerBean serializerBean = new SerializerBean();
    serializerBean.setName("高广金");
    serializerBean.setAge(18);
    Output output = new Output(new FileOutputStream("/tmp/test.bin"));
    kryo.writeObject(output, serializerBean);
    output.flush();
    output.close();
    log.info("序列化前:{}", serializerBean.toString());
    SerializerBean deseriable = kryo.readObject(new Input(new FileInputStream("/tmp/test.bin")),
            SerializerBean.class);
    log.info("反序列化:{}", deseriable.toString());
}
 
Example 9
Source File: EncodedResourcesSerializer.java    From onos with Apache License 2.0 5 votes vote down vote up
@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 10
Source File: TestSiteToSiteClient.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testSerializationWithStateManager() {
    final StateManager stateManager = Mockito.mock(StateManager.class);
    final SiteToSiteClientConfig clientConfig = new SiteToSiteClient.Builder()
        .url("http://localhost:8080/nifi")
        .portName("input")
        .stateManager(stateManager)
        .buildConfig();

    final Kryo kryo = new Kryo();

    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final Output output = new Output(out);

    try {
        kryo.writeObject(output, clientConfig);
    } finally {
        output.close();
    }

    final ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    final Input input = new Input(in);

    try {
        SiteToSiteClientConfig clientConfig2 = kryo.readObject(input, SiteToSiteClient.StandardSiteToSiteClientConfig.class);
        Assert.assertEquals(clientConfig.getUrls(), clientConfig2.getUrls());
        // Serialization works, but the state manager is not serialized.
        Assert.assertNotNull(clientConfig.getStateManager());
        Assert.assertNull(clientConfig2.getStateManager());
    } finally {
        input.close();
    }
}
 
Example 11
Source File: RelTraitSerializers.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public void write(final Kryo kryo, final Output output, final T object) {
  final boolean isKnown = collationSet.contains(object);
  kryo.writeObject(output, isKnown);
  if (isKnown) {
    kryo.writeObject(output, object.equals(RelCollations.EMPTY) ? 0 : 1);
    return;
  }

  super.write(kryo, output, object);
}
 
Example 12
Source File: AvroCoderTest.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Confirm that we can serialize and deserialize an AvroCoder object using Kryo. (BEAM-626).
 *
 * @throws Exception
 */
@Test
public void testKryoSerialization() throws Exception {
  Pojo value = new Pojo("Hello", 42, DATETIME_A);
  AvroCoder<Pojo> coder = AvroCoder.of(Pojo.class);

  // Kryo instantiation
  Kryo kryo = new Kryo();
  kryo.setInstantiatorStrategy(new StdInstantiatorStrategy());

  // Serialization of object without any memoization
  ByteArrayOutputStream coderWithoutMemoizationBos = new ByteArrayOutputStream();
  try (Output output = new Output(coderWithoutMemoizationBos)) {
    kryo.writeObject(output, coder);
  }

  // Force thread local memoization to store values.
  CoderProperties.coderDecodeEncodeEqual(coder, value);

  // Serialization of object with memoized fields
  ByteArrayOutputStream coderWithMemoizationBos = new ByteArrayOutputStream();
  try (Output output = new Output(coderWithMemoizationBos)) {
    kryo.writeObject(output, coder);
  }

  // Copy empty and memoized variants of the Coder
  ByteArrayInputStream bisWithoutMemoization =
      new ByteArrayInputStream(coderWithoutMemoizationBos.toByteArray());
  AvroCoder<Pojo> copiedWithoutMemoization =
      (AvroCoder<Pojo>) kryo.readObject(new Input(bisWithoutMemoization), AvroCoder.class);
  ByteArrayInputStream bisWithMemoization =
      new ByteArrayInputStream(coderWithMemoizationBos.toByteArray());
  AvroCoder<Pojo> copiedWithMemoization =
      (AvroCoder<Pojo>) kryo.readObject(new Input(bisWithMemoization), AvroCoder.class);

  CoderProperties.coderDecodeEncodeEqual(copiedWithoutMemoization, value);
  CoderProperties.coderDecodeEncodeEqual(copiedWithMemoization, value);
}
 
Example 13
Source File: EdgeValueWritable.java    From hgraphdb with Apache License 2.0 5 votes vote down vote up
@Override
public void write(final DataOutput output) throws IOException {
    Kryo kryo = new Kryo();
    kryo.register(HBaseEdge.class, new HBaseEdgeSerializer());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Output out = new Output(baos);
    kryo.writeObject(out, this.edge);
    out.close();
    final byte[] serialized = baos.toByteArray();
    WritableUtils.writeCompressedByteArray(output, serialized);
    Writable writable = value != null ? value : NullWritable.get();
    Text.writeString(output, writable.getClass().getName());
    writable.write(output);
}
 
Example 14
Source File: GraphIO.java    From ContactMerger with Apache License 2.0 5 votes vote down vote up
public static <N,E> void store(UndirectedGraph<N,E> g, File file)
        throws FileNotFoundException, IOException
{
    Kryo kryo = new Kryo();
    register(kryo);
    Output output = new Output(
        new GZIPOutputStream(new FileOutputStream(file)));
    kryo.writeObject(output, g);
    output.flush();
    output.close();
}
 
Example 15
Source File: EncodableDiscreteResourcesSerializer.java    From onos with Apache License 2.0 4 votes vote down vote up
@Override
public void write(Kryo kryo, Output output, EncodableDiscreteResources object) {
    kryo.writeObject(output, object.parent());
    kryo.writeObject(output, new LinkedHashSet<>(object.rawValues().values()));
}
 
Example 16
Source File: InternalPortEventSerializer.java    From onos with Apache License 2.0 4 votes vote down vote up
@Override
public void write(Kryo kryo, Output output, InternalPortEvent event) {
    kryo.writeClassAndObject(output, event.providerId());
    kryo.writeObject(output, event.deviceId(), deviceIdSerializer());
    kryo.writeClassAndObject(output, event.portDescriptions());
}
 
Example 17
Source File: GoogleAccountsServiceSerializer.java    From springboot-shiro-cas-mybatis with MIT License 4 votes vote down vote up
@Override
public void write(final Kryo kryo, final Output output, final GoogleAccountsService service) {
    super.write(kryo, output, service);
    kryo.writeObject(output, fieldHelper.getFieldValue(service, "requestId"));
    kryo.writeObject(output, fieldHelper.getFieldValue(service, "relayState"));
}
 
Example 18
Source File: SimpleWebApplicationServiceSerializer.java    From cas4.0.x-server-wechat with Apache License 2.0 4 votes vote down vote up
@Override
public void write(final Kryo kryo, final Output output, final SimpleWebApplicationServiceImpl service) {
    kryo.writeObject(output, service.getId());
}
 
Example 19
Source File: SetPropertyValueSerializer.java    From SynchronizeFX with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void write(final Kryo kryo, final Output output, final SetPropertyValue object) {
    kryo.writeObject(output, object.getCommandId());
    kryo.writeObject(output, object.getPropertyId());
    kryo.writeObject(output, object.getValue());
}
 
Example 20
Source File: SamlServiceSerializer.java    From cas4.0.x-server-wechat with Apache License 2.0 4 votes vote down vote up
@Override
public void write(final Kryo kryo, final Output output, final SamlService service) {
    super.write(kryo, output, service);
    kryo.writeObject(output, service.getRequestID());
}