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

The following examples show how to use com.esotericsoftware.kryo.Kryo#readObject() . 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: PSTreeUnitTest.java    From gatk with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testSerialize() throws Exception {
    final PSTree tree = new PSTree(1);
    tree.addNode(2, "n2", 1, 0, "none");
    tree.addNode(3, "n3", 1, 0, "none");
    tree.addNode(4, "n4", 2, 0, "none");

    try {
        final File tempFile = createTempFile("test", ".dat");
        final Kryo kryo = new Kryo();
        kryo.setReferences(false);
        Output output = new Output(new FileOutputStream(tempFile));
        kryo.writeObject(output, tree);
        output.close();

        final Input input = new Input(new FileInputStream(tempFile));
        final PSTree treeIn = kryo.readObject(input, PSTree.class);
        Assert.assertEquals(treeIn, tree);
    } catch (FileNotFoundException e) {
        throw new IOException("Error with Kryo IO", e);
    }
}
 
Example 2
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 3
Source File: ProfileMeasurementTest.java    From metron with Apache License 2.0 6 votes vote down vote up
/**
 * Ensure that the {@link ProfileMeasurement} can undergo Kryo serialization which
 * occurs when the Profiler is running in Storm.
 */
@Test
public void testKryoSerialization() {
  assertNotNull(measurement);
  Kryo kryo = new Kryo();

  // serialize
  ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
  Output output = new Output(byteStream);
  kryo.writeObject(output, measurement);

  // validate serialization
  byte[] bits = output.toBytes();
  assertNotNull(bits);

  // deserialize
  Input input = new Input(new ByteArrayInputStream(bits));
  ProfileMeasurement actual = kryo.readObject(input, ProfileMeasurement.class);

  // validate deserialization
  assertNotNull(actual);
  assertEquals(measurement, actual);
}
 
Example 4
Source File: AbstractFileInputOperatorTest.java    From attic-apex-malhar with Apache License 2.0 6 votes vote down vote up
/**
 * This method checkpoints the given operator.
 * @param oper The operator to checkpoint.
 * @param bos The ByteArrayOutputStream which saves the checkpoint data temporarily.
 * @return new operator.
 */
public static <T> T checkpoint(T oper, ByteArrayOutputStream bos) throws Exception
{
  Kryo kryo = new Kryo();

  Output loutput = new Output(bos);
  kryo.writeObject(loutput, oper);
  loutput.close();

  Input lInput = new Input(bos.toByteArray());
  @SuppressWarnings("unchecked")
  T checkPointedOper = kryo.readObject(lInput, (Class<T>)oper.getClass());
  lInput.close();

  return checkPointedOper;
}
 
Example 5
Source File: IOTools.java    From phrasal with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Deserialize an object.
 * Only supports BIN and BIN_GZ SerializationMode.
 * 
 * If the specified file does not exist or is empty, then this call returns null.
 * 
 * @param inputStream
 * @param type
 * @param mode
 * @return
 * @throws IOException
 * @throws ClassNotFoundException
 */
public static <T> T deserialize(InputStream inStream, Class<T> type, SerializationMode mode) {
  try {
    T object;
    if (mode == SerializationMode.BIN || mode == SerializationMode.BIN_GZ) {
      Kryo kryo = new Kryo();
      kryo.setReferences(false);
      Input input = new Input(mode == SerializationMode.BIN_GZ ? 
          new GZIPInputStream(inStream) : inStream);
      object = kryo.readObject(input, type);
      
    } else {
      throw new UnsupportedOperationException();
    }

    return object;
    
  } catch (KryoException  | IOException e) {
    logger.error("Unable to deserialize {} (mode: {})", mode);
    logger.error("Deserialization exception", e);
    throw new RuntimeException(e);
  }
}
 
Example 6
Source File: TestSerDeserPer.java    From eagle with Apache License 2.0 5 votes vote down vote up
@Test
public void testSerDeserPerf() throws Exception {
    Kryo kryo = new Kryo();
    String outputPath = temporaryFolder.newFile().toString();
    Output output = new Output(new FileOutputStream(outputPath));
    for (int i = 0; i < 1000; i++) {
        kryo.writeObject(output, constructPE());
    }
    output.close();
    Input input = new Input(new FileInputStream(outputPath));
    PartitionedEvent someObject = kryo.readObject(input, PartitionedEvent.class);
    input.close();
    Assert.assertTrue(someObject.getData().length == 1);
}
 
Example 7
Source File: KryoSerializer.java    From hmily with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T deSerialize(final byte[] param, final Class<T> clazz) throws HmilyException {
    T object;
    try (ByteArrayInputStream inputStream = new ByteArrayInputStream(param)) {
        Kryo kryo = new Kryo();
        Input input = new Input(inputStream);
        object = kryo.readObject(input, clazz);
        input.close();
    } catch (IOException e) {
        throw new HmilyException("kryo deSerialize error" + e.getMessage());
    }
    return object;
}
 
Example 8
Source File: EncodableDiscreteResourcesSerializer.java    From onos with Apache License 2.0 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 new EncodableDiscreteResources(parent, resources.stream()
            .collect(Collectors.toMap(
                    EncodedDiscreteResources::encodedClass,
                    Function.identity(),
                    (v1, v2) -> v1,
                    LinkedHashMap::new)));
}
 
Example 9
Source File: KryoSerializer.java    From Jupiter with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T readObject(InputBuf inputBuf, Class<T> clazz) {
    Input input = Inputs.getInput(inputBuf);
    Kryo kryo = kryoThreadLocal.get();
    try {
        return kryo.readObject(input, clazz);
    } finally {
        inputBuf.release();
    }
}
 
Example 10
Source File: URLSerializer.java    From springboot-shiro-cas-mybatis with MIT 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 new RuntimeException(e);
    }
}
 
Example 11
Source File: InternalPortStatusEventSerializer.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public InternalPortStatusEvent read(Kryo kryo, Input input,
                           Class<InternalPortStatusEvent> type) {
    ProviderId providerId = (ProviderId) kryo.readClassAndObject(input);
    DeviceId deviceId = kryo.readObject(input, DeviceId.class, deviceIdSerializer());
    @SuppressWarnings("unchecked")
    Timestamped<PortDescription> portDescription = (Timestamped<PortDescription>) kryo.readClassAndObject(input);

    return new InternalPortStatusEvent(providerId, deviceId, portDescription);
}
 
Example 12
Source File: RelTraitSerializers.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@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 13
Source File: KryoSerializer.java    From myth with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T deSerialize(final byte[] param, final Class<T> clazz) throws MythException {
    T object;
    try (ByteArrayInputStream inputStream = new ByteArrayInputStream(param)) {
        Kryo kryo = new Kryo();
        Input input = new Input(inputStream);
        object = kryo.readObject(input, clazz);
        input.close();
    } catch (IOException e) {
        throw new MythException("kryo deSerialize error" + e.getMessage());
    }
    return object;
}
 
Example 14
Source File: ObjectNameSerializer.java    From JPPF with Apache License 2.0 5 votes vote down vote up
@Override
public ObjectName read(final Kryo kryo, final Input input, final Class<ObjectName> clazz) {
  final String name = kryo.readObject(input, String.class);
  try {
    return ObjectNameCache.getObjectName(name);
  } catch (final Exception e) {
    e.printStackTrace();
  }
  return null;
}
 
Example 15
Source File: AbstractFileInputOperatorTest.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
/**
 * Restores the checkpointed operator.
 * @param checkPointOper The checkpointed operator.
 * @param bos The ByteArrayOutputStream which saves the checkpoint data temporarily.
 */
@SuppressWarnings({"unchecked", "rawtypes"})
public static <T> T restoreCheckPoint(T checkPointOper, ByteArrayOutputStream bos) throws Exception
{
  Kryo kryo = new Kryo();

  Input lInput = new Input(bos.toByteArray());
  T oper = kryo.readObject(lInput, (Class<T>)checkPointOper.getClass());
  lInput.close();

  return oper;
}
 
Example 16
Source File: FastSerializer.java    From turbo-rpc with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static Object slowRead(Kryo kryo, Input input) {
	Registration registration = kryo.readClass(input);
	if (registration == null) {
		return null;
	} else {
		Serializer<?> serializer = registration.getSerializer();
		return kryo.readObject(input, registration.getType(), serializer);
	}
}
 
Example 17
Source File: DiskCachingIngester.java    From macrobase with Apache License 2.0 5 votes vote down vote up
private List<Datum> readInData() throws IOException {
    File f = new File(fileDir + "/" + convertFileName(timeColumn,
                                                      attributes,
                                                      metrics,
                                                      conf.getString(MacroBaseConf.BASE_QUERY,
                                                                     conf.getString(MacroBaseConf.QUERY_NAME,
                                                                                    "cachedQuery"))));
    if (!f.exists()) {
        log.info("Data did not exist; going to read from SQL.");
        return null;
    }

    log.info("On-disk cache exists; loading...");
    InputStream inputStream = new SnappyInputStream(new BufferedInputStream(new FileInputStream(f), 16384));

    Kryo kryo = new Kryo();
    Input input = new Input(inputStream);

    DatumEncoder cachedEncoder = kryo.readObject(input, DatumEncoder.class);

    Integer numBatches = kryo.readObject(input, Integer.class);
    List<Datum> output = null;

    for (int i = 0; i < numBatches; ++i) {
        List<Datum> fromDisk = (List<Datum>) kryo.readClassAndObject(input);
        if (output == null) {
            output = fromDisk;
        } else {
            output.addAll(fromDisk);
        }
    }

    log.info("...loaded!");

    conf.getEncoder().copy(cachedEncoder);
    return output;
}
 
Example 18
Source File: KryoByteBufTest.java    From turbo-rpc with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {
		Kryo kryo = new Kryo();
		// kryo.setWarnUnregisteredClasses(true);
		// kryo.setReferences(false);

		kryo.register(IntObj.class);
		kryo.register(StringUTF8Obj.class);
		kryo.register(LocalDateTimeObj.class);
		kryo.register(ComplexObj.class);
		kryo.register(int[].class);
		kryo.register(ArrayList.class);
		kryo.register(ArrayListObj.class);

		ByteBufAllocator allocator = new PooledByteBufAllocator(true);
		ByteBuf buffer = allocator.directBuffer(2, 1024 * 1024 * 8);
		System.out.println("buffer.nioBufferCount: " + buffer.nioBufferCount());
		System.out.println(buffer.nioBuffer(0, buffer.capacity()));

		ByteBufOutput output = new ByteBufOutput(buffer);

		UserService userService = new UserServiceServerImpl();
		User user = userService.getUser(Long.MAX_VALUE).join();
		
		while(true) {
			buffer.clear();
			output.setBuffer(buffer);
			kryo.writeObject(output, user);

			ByteBufInput input = new ByteBufInput(buffer);
			User u = kryo.readObject(input, User.class);
			System.out.println(u);
		}

//		buffer.clear();
//		output.setBuffer(buffer);
//		kryo.writeObject(output, user);
//		System.out.println("user writeObject: " + output.total());
//
//		ByteBufInput input = new ByteBufInput(buffer);
//		System.out.println("user readableBytes: " + buffer.readableBytes());
//		System.out.println("user readerIndex: " + buffer.readerIndex());
//		System.out.println(kryo.readObject(input, User.class));
	}
 
Example 19
Source File: PercentileSerializerTest.java    From kylin with Apache License 2.0 4 votes vote down vote up
public static <T> T deserialize(final Kryo kryo, final byte[] in, final Class<T> clazz) {
    final Input input = new Input(in);
    return kryo.readObject(input, clazz);
}
 
Example 20
Source File: KryoUtils.java    From kylin-on-parquet-v2 with Apache License 2.0 4 votes vote down vote up
public static <T> T deserialize(byte[] bytes, Class<T> clazz) {
    Kryo kryo = getKryo();
    Input input = new Input(bytes);
    return kryo.readObject(input, clazz);
}