DiskStorage implements Storage { @Override public void saveArchive(Archive a, byte[] archiveData) throws IOException { Index index = a.getIndex(); IndexFile indexFile = getIndex(index.getId()); assert indexFile.getIndexFileId() == index.getId(); DataFileWriteResult res = data.write(index.getId(), a.getArchiveId(), archiveData); indexFile.write(new IndexEntry(indexFile, a.getArchiveId(), res.sector, res.compressedLength)); byte compression = archiveData[0]; int compressedSize = Ints.fromBytes(archiveData[1], archiveData[2], archiveData[3], archiveData[4]); int length = 1 + 4 + compressedSize + (compression != CompressionType.NONE ? 4 : 0); Crc32 crc = new Crc32(); crc.update(archiveData, 0, length); a.setCrc(crc.getHash()); logger.trace("Saved archive {}/{} at sector {}, compressed length {}", index.getId(), a.getArchiveId(), res.sector, res.compressedLength); } DiskStorage(File folder); @Override void init(Store store); @Override void close(); @Override void load(Store store); byte[] readIndex(int indexId); @Override byte[] loadArchive(Archive archive); @Override void save(Store store); @Override void saveArchive(Archive a, byte[] archiveData);  }
@Test public void testSaveArchive() throws Exception { File file = folder.newFolder(); DiskStorage storage = new DiskStorage(file); Archive archive; Archive archive2; try (Store store = new Store(storage)) { Index index = store.addIndex(0); archive = index.addArchive(0); archive2 = index.addArchive(1); FileData[] fileData = new FileData[1]; archive.setFileData(fileData); fileData[0] = new FileData(); FileData[] fileData2 = new FileData[1]; archive2.setFileData(fileData2); fileData2[0] = new FileData(); byte[] data = "test".getBytes(); Container container = new Container(archive.getCompression(), -1); container.compress(data, null); byte[] compressedData = container.data; storage.saveArchive(archive, compressedData); container = new Container(archive.getCompression(), 42); container.compress(data, null); compressedData = container.data; archive2.setRevision(42); storage.saveArchive(archive2, compressedData); store.save(); } storage = new DiskStorage(file); try (Store store = new Store(storage)) { store.load(); Index index = store.findIndex(0); Archive archive2_1 = index.getArchive(0); Archive archive2_2 = index.getArchive(1); byte[] comprsesedData = storage.loadArchive(archive2_1); byte[] data = archive2_1.decompress(comprsesedData); assertArrayEquals("test".getBytes(), data); assertEquals(archive.getCrc(), archive2_1.getCrc()); assertEquals(archive.getRevision(), archive2_1.getRevision()); comprsesedData = storage.loadArchive(archive2_2); data = archive2_2.decompress(comprsesedData); assertArrayEquals("test".getBytes(), data); assertEquals(archive2.getCrc(), archive2_2.getCrc()); assertEquals(archive2.getRevision(), archive2_2.getRevision()); } }