com.esotericsoftware.kryo.io.Input Java Examples

The following examples show how to use com.esotericsoftware.kryo.io.Input. 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: KryoRedisSerializer.java    From blog_demos with Apache License 2.0 6 votes vote down vote up
@Override
public T deserialize(byte[] bytes) throws SerializationException {
    if (bytes == null || bytes.length <= 0) {
        return null;
    }

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

    try (Input input = new Input(bytes)) {
        return (T) kryo.readClassAndObject(input);
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }

    return null;
}
 
Example #2
Source File: ByteOperand.java    From ytk-mp4j with MIT License 6 votes vote down vote up
public ArrayMetaData<byte[]> read(Kryo kryo, Input input, Class<ArrayMetaData<byte[]>> type) {
    try {
        byte[] arrData = arrayMetaData.getArrData();
        thatArrMetaData = arrayMetaData.recv(input);
        int arrSegNum = thatArrMetaData.getSegNum();
        for (int i = 0; i < arrSegNum; i++) {
            int from = thatArrMetaData.getFrom(i);
            int to = thatArrMetaData.getTo(i);
            for (int j = from; j < to; j++) {
                arrData[j] = input.readByte();
            }
        }
        thatArrMetaData.setArrData(arrData);
    } catch (IOException e) {
        LOG.error("double array read exception", e);
        System.exit(1);
    }
    return thatArrMetaData;

}
 
Example #3
Source File: JavaSerializer.java    From flink with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"unchecked", "rawtypes"})
@Override
public T read(Kryo kryo, Input input, Class aClass) {
	try {
		ObjectMap graphContext = kryo.getGraphContext();
		ObjectInputStream objectStream = (ObjectInputStream)graphContext.get(this);
		if (objectStream == null) {
			// make sure we use Kryo's classloader
			objectStream = new InstantiationUtil.ClassLoaderObjectInputStream(input, kryo.getClassLoader());
			graphContext.put(this, objectStream);
		}
		return (T) objectStream.readObject();
	} catch (Exception ex) {
		throw new KryoException("Error during Java deserialization.", ex);
	}
}
 
Example #4
Source File: EdgeValueWritable.java    From hgraphdb with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void readFields(final DataInput input) throws IOException {
    try {
        Kryo kryo = new Kryo();
        kryo.register(HBaseEdge.class, new HBaseEdgeSerializer());
        final ByteArrayInputStream bais = new ByteArrayInputStream(WritableUtils.readCompressedByteArray(input));
        this.edge = kryo.readObject(new Input(bais), HBaseEdge.class);
        Class<? extends Writable> cls = Class.forName(Text.readString(input)).asSubclass(Writable.class);
        Writable writable;
        if (cls.equals(NullWritable.class)) {
            writable = NullWritable.get();
        } else {
            writable = cls.newInstance();
        }
        writable.readFields(input);
        this.value = writable != NullWritable.get() ? (V) writable : null;
    } catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
        throw new IOException("Failed writable init", e);
    }
}
 
Example #5
Source File: DefaultLinkSerializer.java    From onos with Apache License 2.0 6 votes vote down vote up
@Override
public DefaultLink read(Kryo kryo, Input input, Class<DefaultLink> type) {
    ProviderId providerId = (ProviderId) kryo.readClassAndObject(input);
    ConnectPoint src = (ConnectPoint) kryo.readClassAndObject(input);
    ConnectPoint dst = (ConnectPoint) kryo.readClassAndObject(input);
    Type linkType = (Type) kryo.readClassAndObject(input);
    State state = (State) kryo.readClassAndObject(input);
    boolean isDurable = input.readBoolean();
    return DefaultLink.builder()
            .providerId(providerId)
            .src(src)
            .dst(dst)
            .type(linkType)
            .state(state)
            .isExpected(isDurable)
            .build();
}
 
Example #6
Source File: KryoByteArrayToObjectConverter.java    From boon with Apache License 2.0 6 votes vote down vote up
@Override
public T apply(byte[] value) {
    if (value == null || value.length == 0) {
        return null;
    }
    T v = null;
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(value);
    try {
        Input input = new Input(inputStream);
        v = kryo.readObject(input, type);
        input.close();
    } catch (Exception e) {
        Exceptions.handle(e);
    }
    return v;

}
 
Example #7
Source File: TestDataFileSerialization.java    From iceberg with Apache License 2.0 6 votes vote down vote up
@Test
public void testDataFileKryoSerialization() throws Exception {
  File data = temp.newFile();
  Assert.assertTrue(data.delete());
  Kryo kryo = new KryoSerializer(new SparkConf()).newKryo();

  try (Output out = new Output(new FileOutputStream(data))) {
    kryo.writeClassAndObject(out, DATA_FILE);
    kryo.writeClassAndObject(out, DATA_FILE.copy());
  }

  try (Input in = new Input(new FileInputStream(data))) {
    for (int i = 0; i < 2; i += 1) {
      Object obj = kryo.readClassAndObject(in);
      Assert.assertTrue("Should be a DataFile", obj instanceof DataFile);
      checkDataFile(DATA_FILE, (DataFile) obj);
    }
  }
}
 
Example #8
Source File: StringOperand.java    From ytk-mp4j with MIT License 6 votes vote down vote up
public ArrayMetaData<String[]> read(Kryo kryo, Input input, Class<ArrayMetaData<String[]>> type) {
    try {
        String[] arrData = arrayMetaData.getArrData();
        thatArrMetaData = arrayMetaData.recv(input);
        int arrSegNum = thatArrMetaData.getSegNum();
        for (int i = 0; i < arrSegNum; i++) {
            int from = thatArrMetaData.getFrom(i);
            int to = thatArrMetaData.getTo(i);
            for (int j = from; j < to; j++) {
                arrData[j] = input.readString();
            }
        }
        thatArrMetaData.setArrData(arrData);
    } catch (IOException e) {
        LOG.error("double array read exception", e);
        System.exit(1);
    }
    return thatArrMetaData;

}
 
Example #9
Source File: KryoSerializer.java    From flink-htm with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Read an instance of the given class from the given input.
 *
 * @param kryo      instance of {@link Kryo} object
 * @param input     a Kryo {@link Input}
 * @param aClass    The class of the object to be read in.
 * @return  an instance of type &lt;T&gt;
 */
@Override
public T read(Kryo kryo, Input input, Class<T> aClass) {

    // read the serialized data
    byte[] data = new byte[input.readInt()];
    input.readBytes(data);

    try {
        try(ByteArrayInputStream stream = new ByteArrayInputStream(data)) {
            HTMObjectInput reader = serializer.getObjectInput(stream);
            T t = (T) reader.readObject(aClass);

            postDeSerialize(t);

            return t;
        }
    }
    catch(Exception e) {
        throw new KryoException(e);
    }
}
 
Example #10
Source File: SparkValueRowSerializerTest.java    From spliceengine with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void testEncodingDecodingSeveralRows() throws IOException, StandardException {

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

    for (int i = 0; i < 100; i++) {
        kryo.writeClassAndObject(output, getExecRow(i, 13));
    }
    output.close();

    InputStream in = new ByteArrayInputStream(out.toByteArray());
    Input input = new Input(in);
    for (int i = 0; i < 100; i++) {
        ExecRow row = (ExecRow) kryo.readClassAndObject(input);
        assertEquals(i, row.getRowArray()[0].getInt());
        assertEquals(""+i, row.getRowArray()[1].getString());
    }
    input.close();
}
 
Example #11
Source File: KryoTest.java    From spliceengine with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void testEmptyByteSlice() {
    ByteSlice byteSliceIn = ByteSlice.wrap(new byte[] {0, 1, 2,3,4,5,6,7,8,9}, 5, 0);

    Output output = new Output(new byte[20],20);
    kryo.writeObject(output, byteSliceIn);
    byte[] bytes = output.toBytes();
    assertNotNull(bytes);

    Input input = new Input(new ByteArrayInputStream(bytes), bytes.length);
    ByteSlice byteSliceOut = kryo.readObject(input, ByteSlice.class);

    assertNotNull(byteSliceOut);
    assertEquals(0, byteSliceOut.offset());
    assertEquals(0, byteSliceOut.length());
}
 
Example #12
Source File: KryoManager.java    From Mundus with Apache License 2.0 6 votes vote down vote up
/**
 * Loads the project context .pro.
 *
 * Does however not load the scenes (only the scene names as reference) or
 * meshes/textures (see ProjectManager).
 *
 * @param ref
 *            project to load
 * @return loaded project context without scenes
 * @throws FileNotFoundException
 */
public ProjectContext loadProjectContext(ProjectRef ref) throws FileNotFoundException {
    // find .pro file
    FileHandle projectFile = null;
    for (FileHandle f : Gdx.files.absolute(ref.getPath()).list()) {
        if (f.extension().equals(ProjectManager.PROJECT_EXTENSION)) {
            projectFile = f;
            break;
        }
    }

    if (projectFile != null) {
        Input input = new Input(new FileInputStream(projectFile.path()));
        ProjectDescriptor projectDescriptor = kryo.readObjectOrNull(input, ProjectDescriptor.class);
        ProjectContext context = DescriptorConverter.convert(projectDescriptor);
        context.activeSceneName = projectDescriptor.getCurrentSceneName();
        return context;
    }

    return null;
}
 
Example #13
Source File: LongBloomFilterTest.java    From gatk with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
void serializationTest() {
    final Random rng = new Random(RAND_SEED);
    final LongBloomFilter bloomFilter = new LongBloomFilter(HHASH_NVALS, FPP);
    final HashSet<Long> hashSet = new HashSet<>(HHASH_NVALS);
    for (int valNo = 0; valNo != HHASH_NVALS; ++valNo) {
        final long randLong = randomLong(rng);
        bloomFilter.add(randLong);
        hashSet.add(randLong);
    }

    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    final Output out = new Output(bos);
    final Kryo kryo = new Kryo();
    kryo.writeObject(out, bloomFilter);
    out.flush();

    final ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
    final Input in = new Input(bis);
    final LongBloomFilter bloomFilter2 = kryo.readObject(in, LongBloomFilter.class);

    Assert.assertEquals(bloomFilter, bloomFilter2);
    for (Long val : hashSet) {
        Assert.assertTrue(bloomFilter2.contains(val));
    }
}
 
Example #14
Source File: PathSeqBuildKmersSparkIntegrationTest.java    From gatk with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testMaskedHopscotchSetFromFasta() throws Exception {
    final File expectedFile = getTestFile("hg19mini.mask_4_15.hss");
    final File ref = new File(hg19MiniReference);
    final File output = createTempFile("test", ".hss");
    if (!output.delete()) {
        Assert.fail();
    }
    final ArgumentsBuilder args = new ArgumentsBuilder();
    args.add(PathSeqBuildKmers.REFERENCE_LONG_NAME, ref);
    args.add(PathSeqBuildKmers.KMER_MASK_LONG_NAME, "4,15");
    args.addOutput(output);
    this.runCommandLine(args.getArgsArray());

    final Input inputExpected = new Input(FileUtils.openInputStream(expectedFile));
    final Input inputTest = new Input(FileUtils.openInputStream(output));

    final Kryo kryo = new Kryo();
    final PSKmerSet expectedKmerLib = kryo.readObject(inputExpected, PSKmerSet.class);
    final PSKmerSet testKmerLib = kryo.readObject(inputTest, PSKmerSet.class);

    Assert.assertEquals(testKmerLib, expectedKmerLib);
}
 
Example #15
Source File: PathSeqBuildKmersSparkIntegrationTest.java    From gatk with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testHopscotchSetFromFasta() throws Exception {

    final String libraryPath = publicTestDir + PathSeqBuildKmers.class.getPackage().getName().replace(".", "/") + "/hg19mini.hss";
    final File expectedFile = new File(libraryPath);
    final File ref = new File(hg19MiniReference);
    final File output = createTempFile("test", ".hss");
    if (!output.delete()) {
        Assert.fail();
    }
    final ArgumentsBuilder args = new ArgumentsBuilder();
    args.add(PathSeqBuildKmers.REFERENCE_LONG_NAME, ref);
    args.addOutput(output);
    this.runCommandLine(args.getArgsArray());

    final Input inputExpected = new Input(FileUtils.openInputStream(expectedFile));
    final Input inputTest = new Input(FileUtils.openInputStream(output));

    final Kryo kryo = new Kryo();
    final PSKmerSet expectedKmerLib = kryo.readObject(inputExpected, PSKmerSet.class);
    final PSKmerSet testKmerLib = kryo.readObject(inputTest, PSKmerSet.class);

    Assert.assertEquals(testKmerLib, expectedKmerLib);
}
 
Example #16
Source File: DoubleOperand.java    From ytk-mp4j with MIT License 6 votes vote down vote up
public ArrayMetaData<double[]> read(Kryo kryo, Input input, Class<ArrayMetaData<double[]>> type) {
    try {
        double[] arrData = arrayMetaData.getArrData();
        thatArrMetaData = arrayMetaData.recv(input);
        int arrSegNum = thatArrMetaData.getSegNum();
        for (int i = 0; i < arrSegNum; i++) {
            int from = thatArrMetaData.getFrom(i);
            int to = thatArrMetaData.getTo(i);
            for (int j = from; j < to; j++) {
                arrData[j] = input.readDouble();
            }
        }
        thatArrMetaData.setArrData(arrData);
    } catch (IOException e) {
        LOG.error("double array read exception", e);
        System.exit(1);
    }
    return thatArrMetaData;

}
 
Example #17
Source File: KryoExample.java    From pragmatic-java-engineer with GNU General Public License v3.0 6 votes vote down vote up
public static void main(String[] args) {
    Kryo kryo = new Kryo();

    // 序列化
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    Output output = new Output(os);
    TestUser user = new TestUser();
    kryo.writeObject(output, user);
    output.close();
    byte[] bytes = os.toByteArray();

    // 反序列化
    Input input = new Input(new ByteArrayInputStream(bytes));
    user = kryo.readObject(input, TestUser.class);
    input.close();

}
 
Example #18
Source File: KryoTest.java    From spliceengine with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void testByteSlice() {
    ByteSlice byteSliceIn = ByteSlice.wrap(new byte[] {0, 1, 2,3,4,5,6,7,8,9}, 2, 4);

    Output output = new Output(new byte[20],20);
    kryo.writeObject(output, byteSliceIn);
    byte[] bytes = output.toBytes();
    assertNotNull(bytes);

    Input input = new Input(new ByteArrayInputStream(bytes), bytes.length);
    ByteSlice byteSliceOut = kryo.readObject(input, ByteSlice.class);

    assertNotNull(byteSliceOut);
    assertEquals(0, byteSliceOut.offset());
    assertEquals(4, byteSliceOut.length());
}
 
Example #19
Source File: FastClassResolver.java    From turbo-rpc with Apache License 2.0 5 votes vote down vote up
protected Registration readName(Input input) {
	int nameId = input.readVarInt(true);
	if (nameIdToClass == null)
		nameIdToClass = new IntToObjectArrayMap<>();
	Class type = nameIdToClass.get(nameId);
	if (type == null) {
		// Only read the class name the first time encountered in object graph.
		String className = input.readString();
		type = getTypeByName(className);
		if (type == null) {
			try {
				type = Class.forName(className, false, kryo.getClassLoader());
			} catch (ClassNotFoundException ex) {
				if (WARN)
					warn("kryo", "Unable to load class " + className
							+ " with kryo's ClassLoader. Retrying with current..");
				try {
					type = Class.forName(className);
				} catch (ClassNotFoundException e) {
					throw new KryoException("Unable to find class: " + className, ex);
				}
			}
			if (nameToClass == null)
				nameToClass = new FastMap<>(32, 0.5F);
			nameToClass.put(className, type);
		}
		nameIdToClass.put(nameId, type);
		if (TRACE)
			trace("kryo", "Read class name: " + className);
	} else {
		if (TRACE)
			trace("kryo", "Read class name reference " + nameId + ": " + className(type));
	}
	return kryo.getRegistration(type);
}
 
Example #20
Source File: CollectionSerdeTest.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
@Test
public void testSerdeList()
{
  CollectionSerde<String, List<String>> serdeList =
      new CollectionSerde<>(new StringSerde(), (Class)ArrayList.class);

  List<String> stringList = Lists.newArrayList("a", "b", "c");
  SerializationBuffer buffer = new SerializationBuffer(new WindowedBlockStream());
  serdeList.serialize(stringList, buffer);

  Slice slice = buffer.toSlice();
  List<String> deserializedList = serdeList.deserialize(new Input(slice.buffer, slice.offset, slice.length));

  Assert.assertEquals(stringList, deserializedList);
}
 
Example #21
Source File: PSTaxonomyDatabaseTest.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testSerializeDeserialize() {

    final Map<String,Integer> accessionToTaxMap = new HashMap<>();
    accessionToTaxMap.put("A",1);
    accessionToTaxMap.put("B",1);
    accessionToTaxMap.put("C",2);
    accessionToTaxMap.put("D",3);

    final PSTree tree = new PSTree(1);
    tree.addNode(2, "node2", 1, 0, "genus");
    tree.addNode(3, "node3", 1, 0, "genus");
    tree.addNode(4, "node4", 2, 200, "species");
    tree.addNode(5, "node5", 2, 300, "species");
    tree.addNode(6, "node6", 3, 100, "species");

    final PSTaxonomyDatabase taxonomyDatabase = new PSTaxonomyDatabase(tree, accessionToTaxMap);

    final Kryo kryo = new Kryo();
    final Output output = new Output(new ByteArrayOutputStream());
    kryo.writeObject(output, taxonomyDatabase);

    final Input input = new Input(new ByteArrayInputStream(output.getBuffer()));
    final PSTaxonomyDatabase taxonomyDatabaseTest = kryo.readObject(input, PSTaxonomyDatabase.class);

    Assert.assertEquals(taxonomyDatabaseTest.tree, taxonomyDatabase.tree);
    Assert.assertEquals(taxonomyDatabaseTest.accessionToTaxId, taxonomyDatabase.accessionToTaxId);
}
 
Example #22
Source File: KryoSerializer.java    From buddha with MIT License 5 votes vote down vote up
public <T> T deserialize(byte[] bytes, Class<T> clazz) {
    Kryo kryo = new Kryo();
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    Input input = new Input(bais);
    T result = kryo.readObject(input, clazz);
    input.close();
    return result;
}
 
Example #23
Source File: PersistableSerializer.java    From geowave with Apache License 2.0 5 votes vote down vote up
@Override
public Persistable read(final Kryo kryo, final Input input, final Class<Persistable> classTag) {

  // Read object byte count and allocate buffer to read object data
  final int byteCount = input.readInt();
  final byte[] bytes = new byte[byteCount];
  final int bytesRead = input.read(bytes);
  // TODO: This was only added for findbugs warning, not really necessary
  // check
  if (bytesRead < 0) {
    return null;
  }

  return PersistenceUtils.fromBinary(bytes);
}
 
Example #24
Source File: InternalDeviceEventSerializer.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public InternalDeviceEvent read(Kryo kryo, Input input,
                           Class<InternalDeviceEvent> type) {
    ProviderId providerId = (ProviderId) kryo.readClassAndObject(input);
    DeviceId deviceId = kryo.readObject(input, DeviceId.class, deviceIdSerializer());

    @SuppressWarnings("unchecked")
    Timestamped<DeviceDescription> deviceDescription
        = (Timestamped<DeviceDescription>) kryo.readClassAndObject(input);

    return new InternalDeviceEvent(providerId, deviceId, deviceDescription);
}
 
Example #25
Source File: BitSetSerializer.java    From kylin with Apache License 2.0 5 votes vote down vote up
@Override
public BitSet read(final Kryo kryo, final Input input, final Class<BitSet> bitSetClass) {
    final int len = input.readInt(true);
    final BitSet ret = new BitSet(len);

    for (int i = 0; i < len; i++) {
        ret.set(i, input.readBoolean());
    }

    return ret;
}
 
Example #26
Source File: InjectingSerializer.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public void read(final Input input, final Object object) {
  try {
    delegate.getField().set(object, value);
  } catch (IllegalAccessException e) {
    logger.error("unable to inject value", e);
  }
}
 
Example #27
Source File: MatcherList.java    From yauaa with Apache License 2.0 5 votes vote down vote up
@Override
public MatcherList read(Kryo kryo, Input input, Class<MatcherList> type) {
    MatcherList matcherList = new MatcherList();
    matcherList.maxSize = input.read();

    // Note: The matches list is always empty after deserialization.
    matcherList.initialize();

    return matcherList;
}
 
Example #28
Source File: HeronPluggableSerializerDelegate.java    From incubator-heron with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("rawtypes")
public void initialize(Map config) {
  kryo = SerializationFactory.getKryo(config);
  kryoOut = new Output(2000, 2000000000);
  kryoIn = new Input(1);
}
 
Example #29
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 #30
Source File: KryoSerialization.java    From craft-atom with MIT License 5 votes vote down vote up
@Override
public RpcBody deserialize(byte[] bytes, int off) {
	try {
	    Assert.notNull(bytes);
	    ByteArrayInputStream bais = new ByteArrayInputStream(bytes, off, bytes.length - off);
	    Input input = new Input(bais);
	    RpcBody rb = kryo().readObject(input, RpcBody.class);
        input.close();
	    return rb;
	} catch (Exception e) {
		throw new ProtocolException(e);
	}
}