/** * Writes the given chunk index data into the given file. * * @param file * The file to write the data into. Must not be <code>null</code>. Must refer to a file (not a directory) and must be writable. It will be fully overwritten! * @param chunkIndexData * The data to write into the file. Must not be <code>null</code>. * * @throws ChronosIOException * thrown if the writing process failed for any reason. */ public static void persistToFile(final File file, final DocumentChunkIndexData chunkIndexData) { try { if (file.exists()) { boolean deleted = file.delete(); if (!deleted) { throw new IOException("Could not delete existing index chunk file!"); } } boolean created = file.createNewFile(); if (!created) { throw new IOException("Could not create index chunk file!"); } ChunkIndexSeal seal = new ChunkIndexSeal(); KryoManager.serializeObjectsToFile(file, chunkIndexData, seal); } catch (IOException e) { throw new ChronosIOException("Failed to persist chunk index!", e); } }
@Test public void canSerializeAndDeserializeWithFiles() { try { File testFile = File.createTempFile("tempFile", "binary"); testFile.deleteOnExit(); Person johnDoe = new Person("John", "Doe"); KryoManager.serializeObjectsToFile(testFile, johnDoe); Person deserialized = KryoManager.deserializeObjectFromFile(testFile); assertNotNull(deserialized); assertEquals(johnDoe, deserialized); } catch (IOException ioe) { fail(ioe.toString()); } }
@Test public void canSerializeAndDeserializeMultipleObjectsWithFiles() { try { File testFile = File.createTempFile("tempFile", "binary"); testFile.deleteOnExit(); Person p1 = new Person("John", "Doe"); Person p2 = new Person("Jane", "Doe"); KryoManager.serializeObjectsToFile(testFile, p1, p2); List<Object> deserializedObjects = KryoManager.deserializeObjectsFromFile(testFile); assertNotNull(deserializedObjects); assertEquals(2, deserializedObjects.size()); assertEquals(p1, deserializedObjects.get(0)); assertEquals(p2, deserializedObjects.get(1)); } catch (IOException ioe) { fail(ioe.toString()); } }