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

The following examples show how to use com.esotericsoftware.kryo.Kryo#readClassAndObject() . 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: KryoSerialize.java    From search-spring-boot-starter with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings({ "unchecked" })
public <T> T decode(byte[] bytes, Class<T> clazz) {
    Kryo kryo = new Kryo();
    kryo.setReferences(false);
    kryo.register(clazz, new JavaSerializer());

    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
    Input input = new Input(byteArrayInputStream);
    try {
        return (T) kryo.readClassAndObject(input);
    } finally {
        input.close();
        try {
            byteArrayInputStream.close();
        } catch (IOException e) {
        }
    }
}
 
Example 2
Source File: KryoSerialization.java    From api-mining with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Object deserializeFrom(final String filename)
		throws SerializationException {
	LOGGER.info("Deserializing object from " + filename);
	try {
		final Kryo kryo = new Kryo();
		final Input input = new Input(new FileInputStream(filename));

		kryo.setInstantiatorStrategy(new SerializingInstantiatorStrategy());

		final Object obj = kryo.readClassAndObject(input);
		input.close();
		return obj;
	} catch (final FileNotFoundException e) {
		throw new ISerializationStrategy.SerializationException(e);
	}
}
 
Example 3
Source File: ImmutableSetSerializer.java    From atomix with Apache License 2.0 6 votes vote down vote up
@Override
public ImmutableSet<?> read(Kryo kryo, Input input,
    Class<ImmutableSet<?>> type) {
  final int size = input.readInt();
  switch (size) {
    case 0:
      return ImmutableSet.of();
    case 1:
      return ImmutableSet.of(kryo.readClassAndObject(input));
    default:
      Object[] elms = new Object[size];
      for (int i = 0; i < size; ++i) {
        elms[i] = kryo.readClassAndObject(input);
      }
      return ImmutableSet.copyOf(elms);
  }
}
 
Example 4
Source File: ImmutableListSerializer.java    From atomix with Apache License 2.0 6 votes vote down vote up
@Override
public ImmutableList<?> read(Kryo kryo, Input input,
    Class<ImmutableList<?>> type) {
  final int size = input.readInt();
  switch (size) {
    case 0:
      return ImmutableList.of();
    case 1:
      return ImmutableList.of(kryo.readClassAndObject(input));
    default:
      Object[] elms = new Object[size];
      for (int i = 0; i < size; ++i) {
        elms[i] = kryo.readClassAndObject(input);
      }
      return ImmutableList.copyOf(elms);
  }
}
 
Example 5
Source File: TestDataFileSerialization.java    From iceberg with Apache License 2.0 6 votes vote down vote up
@Test
public void testParquetWriterSplitOffsets() throws IOException {
  Iterable<InternalRow> records = RandomData.generateSpark(DATE_SCHEMA, 1, 33L);
  File parquetFile = new File(
      temp.getRoot(),
      FileFormat.PARQUET.addExtension(UUID.randomUUID().toString()));
  FileAppender<InternalRow> writer =
      Parquet.write(Files.localOutput(parquetFile))
          .schema(DATE_SCHEMA)
          .createWriterFunc(msgType -> SparkParquetWriters.buildWriter(SparkSchemaUtil.convert(DATE_SCHEMA), msgType))
          .build();
  try {
    writer.addAll(records);
  } finally {
    writer.close();
  }

  Kryo kryo = new KryoSerializer(new SparkConf()).newKryo();
  File dataFile = temp.newFile();
  try (Output out = new Output(new FileOutputStream(dataFile))) {
    kryo.writeClassAndObject(out, writer.splitOffsets());
  }
  try (Input in = new Input(new FileInputStream(dataFile))) {
    kryo.readClassAndObject(in);
  }
}
 
Example 6
Source File: ImmutableListSerializer.java    From onos with Apache License 2.0 6 votes vote down vote up
@Override
public ImmutableList<?> read(Kryo kryo, Input input,
                             Class<ImmutableList<?>> type) {
    final int size = input.readInt();
    switch (size) {
    case 0:
        return ImmutableList.of();
    case 1:
        return ImmutableList.of(kryo.readClassAndObject(input));
    default:
        Object[] elms = new Object[size];
        for (int i = 0; i < size; ++i) {
            elms[i] = kryo.readClassAndObject(input);
        }
        return ImmutableList.copyOf(elms);
    }
}
 
Example 7
Source File: NovelAdjacencyAndAltHaplotype.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected NovelAdjacencyAndAltHaplotype(final Kryo kryo, final Input input) {
    final String contig1 = input.readString();
    final int start1 = input.readInt();
    final int end1 = input.readInt();
    this.leftJustifiedLeftRefLoc = new SimpleInterval(contig1, start1, end1);
    final String contig2 = input.readString();
    final int start2 = input.readInt();
    final int end2 = input.readInt();
    this.leftJustifiedRightRefLoc = new SimpleInterval(contig2, start2, end2);

    this.strandSwitch = StrandSwitch.values()[input.readInt()];

    this.complication = (BreakpointComplications) kryo.readClassAndObject(input);

    this.type = TypeInferredFromSimpleChimera.values()[ input.readInt() ];

    final boolean altSeqIsNull = input.readBoolean();
    if (altSeqIsNull) {
        altHaplotypeSequence = null;
    } else {
        final int arraySize = input.readInt();
        altHaplotypeSequence = new byte[arraySize];
        for (int i = 0 ; i < arraySize; ++i) {
            altHaplotypeSequence[i] = input.readByte();
        }
    }
}
 
Example 8
Source File: KryoDecoder.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
//        LOG.warn("Decoding");
        
        if (in.readableBytes() < 4)
            return;


        in.markReaderIndex();

        int len = in.readInt();
//        LOG.warn("Read lenght " + len);

        if (in.readableBytes() < len) {

//            LOG.warn("Not enough data ");
            in.resetReaderIndex();
            return;
        }

//        LOG.warn("Decoding object ");

        byte[] buf = new byte[len];
        in.readBytes(buf);
        Input input = new Input(buf);

        Kryo decoder = kp.get();
        try {
            Object object = decoder.readClassAndObject(input);
            out.add(object);
        }
        finally {
            kp.returnInstance(decoder);

        }


//        LOG.warn("Decoded " + object);
    }
 
Example 9
Source File: KryoNamespace.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Deserializes given InputStream to an Object using Kryo instance in pool.
 *
 * @param stream input stream
 * @param <T> deserialized Object type
 * @return deserialized Object
 * @param bufferSize size of the buffer in front of the stream
 */
public <T> T deserialize(final InputStream stream, final int bufferSize) {
    ByteBufferInput in = new ByteBufferInput(stream, bufferSize);
    Kryo kryo = borrow();
    try {
        @SuppressWarnings("unchecked")
        T obj = (T) kryo.readClassAndObject(in);
        return obj;
    } finally {
        release(kryo);
    }
}
 
Example 10
Source File: DefaultPortSerializer.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public DefaultPort read(Kryo kryo, Input input, Class<DefaultPort> aClass) {
    Element element = (Element) kryo.readClassAndObject(input);
    PortNumber number = kryo.readObject(input, PortNumber.class);
    boolean isEnabled = input.readBoolean();
    Port.Type type = kryo.readObject(input, Port.Type.class);
    long portSpeed = input.readLong();
    Annotations annotations = (Annotations) kryo.readClassAndObject(input);

    return new DefaultPort(element, number, isEnabled, type, portSpeed, annotations);
}
 
Example 11
Source File: RelTraitSerializers.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@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 12
Source File: KryoNamespace.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Deserializes given byte array to Object using Kryo instance in pool.
 *
 * @param bytes serialized bytes
 * @param <T> deserialized Object type
 * @return deserialized Object
 */
public <T> T deserialize(final byte[] bytes) {
    Input in = new Input(new ByteArrayInputStream(bytes));
    Kryo kryo = borrow();
    try {
        @SuppressWarnings("unchecked")
        T obj = (T) kryo.readClassAndObject(in);
        return obj;
    } finally {
        release(kryo);
    }
}
 
Example 13
Source File: CpxVariantCanonicalRepresentationUnitTest.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 CpxVariantCanonicalRepresentation cpxVariantCanonicalRepresentation) {
    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    final Output out = new Output(bos);
    final Kryo kryo = new Kryo();
    kryo.writeClassAndObject(out, cpxVariantCanonicalRepresentation);
    out.flush();

    final ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
    final Input in = new Input(bis);
    @SuppressWarnings("unchecked")
    final CpxVariantCanonicalRepresentation roundTrip = (CpxVariantCanonicalRepresentation) kryo.readClassAndObject(in);
    Assert.assertEquals(roundTrip, cpxVariantCanonicalRepresentation);
    Assert.assertEquals(roundTrip.hashCode(), cpxVariantCanonicalRepresentation.hashCode());
}
 
Example 14
Source File: InternalDeviceStatusChangeEventSerializer.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public InternalDeviceStatusChangeEvent read(Kryo kryo, Input input,
                                            Class<InternalDeviceStatusChangeEvent> type) {
    DeviceId deviceId = kryo.readObject(input, DeviceId.class, deviceIdSerializer());
    Timestamp timestamp = (Timestamp) kryo.readClassAndObject(input);
    Boolean available = (Boolean) kryo.readClassAndObject(input);

    return new InternalDeviceStatusChangeEvent(deviceId, timestamp, available);
}
 
Example 15
Source File: Namespace.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * Deserializes given byte buffer to Object using Kryo instance in pool.
 *
 * @param buffer input with serialized bytes
 * @param <T>    deserialized Object type
 * @return deserialized Object
 */
public <T> T deserialize(final ByteBuffer buffer) {
  ByteBufferInput in = new ByteBufferInput(buffer);
  Kryo kryo = borrow();
  try {
    @SuppressWarnings("unchecked")
    T obj = (T) kryo.readClassAndObject(in);
    return obj;
  } finally {
    release(kryo);
  }
}
 
Example 16
Source File: Namespace.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * Deserializes given InputStream to an Object using Kryo instance in pool.
 *
 * @param stream     input stream
 * @param <T>        deserialized Object type
 * @param bufferSize size of the buffer in front of the stream
 * @return deserialized Object
 */
public <T> T deserialize(final InputStream stream, final int bufferSize) {
  ByteBufferInput in = new ByteBufferInput(stream, bufferSize);
  Kryo kryo = borrow();
  try {
    @SuppressWarnings("unchecked")
    T obj = (T) kryo.readClassAndObject(in);
    return obj;
  } finally {
    release(kryo);
  }
}
 
Example 17
Source File: DataSerializer.java    From chuidiang-ejemplos with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public Object read(ObjectDataInput objectDataInput) throws IOException {
    Kryo kryo = new Kryo();
    byte [] buffer = objectDataInput.readByteArray();

    Input input = new Input(buffer);

    Object data = kryo.readClassAndObject(input);

    return data;
}
 
Example 18
Source File: KryoSerialization.java    From eagle with Apache License 2.0 4 votes vote down vote up
@Override
public <T> T deserialize(byte[] bytes, Class<T> clz) throws IOException {
    Kryo kryo = kryos.get();
    Input in = new Input(new ByteArrayInputStream(bytes));
    return (T) kryo.readClassAndObject(in);
}
 
Example 19
Source File: ActivationSerializer.java    From spliceengine with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public void read (Kryo kryo, Input in) {
    dvd = (DataValueDescriptor) kryo.readClassAndObject(in);
}
 
Example 20
Source File: McastPathStoreKeySerializer.java    From onos with Apache License 2.0 4 votes vote down vote up
@Override
public McastPathStoreKey read(Kryo kryo, Input input, Class<McastPathStoreKey> type) {
    IpAddress mcastIp = (IpAddress) kryo.readClassAndObject(input);
    ConnectPoint source = (ConnectPoint) kryo.readClassAndObject(input);
    return new McastPathStoreKey(mcastIp, source);
}