StitchedManifestGenerator { public InputStream generate(String spaceId, ManifestFormat format) throws IOException { final File stitchedManifestFile = File.createTempFile("stitched-manifest-" + spaceId, "." + format.name().toLowerCase()); try (InputStream manifest = store.getManifest(spaceId, format); BufferedReader reader = new BufferedReader(new InputStreamReader(manifest)); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(stitchedManifestFile)));) { ManifestFormatter formatter = new ManifestFormatterFactory().create(format); String header = formatter.getHeader(); String line = null; try { while ((line = reader.readLine()) != null) { if (line.trim().length() == 0) { continue; } if (header != null && line.equals(header)) { writeLine(line, writer); continue; } processLine(line, formatter, writer); } } catch (IOException e) { log.error("failed to complete manifest stiching.", e); } } catch (ContentStoreException e) { log.error("failed to generate stitched manifest: " + e.getMessage(), e); throw new IOException(e); } return new AutoCloseInputStream(new FileInputStream(stitchedManifestFile) { @Override public void close() throws IOException { super.close(); stitchedManifestFile.delete(); } }); } StitchedManifestGenerator(ContentStore store); InputStream generate(String spaceId, ManifestFormat format);  }
@Test public void testGenerate() throws Exception { String sourceContentId = "content.dat"; String sourceMd5 = "checksum"; String chunkManifestContentId = sourceContentId + ChunksManifest.manifestSuffix; String chunkContentId = "content.dat" + ChunksManifest.chunkSuffix + "0000"; String unchunkedContentId = "content1.dat"; ContentStore store = createMock(ContentStore.class); Content content = createMock(Content.class); TsvManifestFormatter formatter = new TsvManifestFormatter(); File unstitchedManifest = File.createTempFile("unstitched", "tsv"); unstitchedManifest.deleteOnExit(); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(unstitchedManifest))); writer.write(formatter.getHeader() + "\n"); write(writer, formatter, chunkManifestContentId); write(writer, formatter, chunkContentId); write(writer, formatter, unchunkedContentId); writer.close(); expect(store.getManifest(spaceId, ManifestFormat.TSV)).andReturn(new FileInputStream(unstitchedManifest)); ChunksManifest manifest = new ChunksManifest(sourceContentId, "text/plain", 1000); manifest.setMD5OfSourceContent(sourceMd5); String xml = ManifestDocumentBinding.createDocumentFrom(manifest); InputStream is = new ByteArrayInputStream(xml.getBytes()); expect(content.getStream()).andReturn(is); expect(store.getContent(spaceId, chunkManifestContentId)).andReturn(content); replayAll(); StitchedManifestGenerator generator = new StitchedManifestGenerator(store); InputStream stitched = generator.generate(spaceId, ManifestFormat.TSV); BufferedReader reader = new BufferedReader(new InputStreamReader(stitched)); assertEquals(formatter.getHeader(), reader.readLine()); assertTrue(reader.readLine().contains(sourceContentId)); assertTrue(reader.readLine().contains(unchunkedContentId)); reader.close(); }