public List<Object> deserializeObjectsFromFile(final File file) throws IOException { try (Input input = new Input(new FileInputStream(file))) { List<Object> resultList = Lists.newArrayList(); Kryo kryo = this.getKryo(); while (input.canReadInt()) { Object element = kryo.readClassAndObject(input); resultList.add(element); } this.usageCount++; this.destroyKryoIfNecessary(); return resultList; } }
@SuppressWarnings("unchecked") public <T> T deserializeObjectFromFile(final File file) throws IOException { try (Input input = new Input(new FileInputStream(file))) { Object object = this.getKryo().readClassAndObject(input); this.usageCount++; this.destroyKryoIfNecessary(); return (T) object; } }
public void serializeToFile(final File file, final Object... objects) throws IOException { try (Output out = new Output(new FileOutputStream(file))) { for (Object object : objects) { this.getKryo().writeClassAndObject(out, object); } out.flush(); this.serializedBytes += file.length(); this.usageCount++; this.destroyKryoIfNecessary(); } }
public <T> T deepCopy(final T element) { Kryo kryo = this.getKryo(); T copy = kryo.copy(element); // we have no idea how big the copied object is; // destroy our kryo instance as a precautionary measure this.destroyKryo(); return copy; }
public byte[] serialize(final Object object) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); Output output = new Output(baos); this.getKryo().writeClassAndObject(output, object); output.flush(); byte[] result = baos.toByteArray(); output.close(); this.serializedBytes += result.length; this.usageCount++; this.destroyKryoIfNecessary(); return result; }
@SuppressWarnings("unchecked") public <T> T deserialize(final byte[] serialForm) { ByteArrayInputStream bais = new ByteArrayInputStream(serialForm); Input input = new Input(bais); Object object = this.getKryo().readClassAndObject(input); input.close(); this.usageCount++; this.destroyKryoIfNecessary(); return (T) object; }