PatchWriter { public void writeV1Patch(OutputStream out) throws IOException { @SuppressWarnings("resource") DataOutputStream dataOut = new DataOutputStream(out); dataOut.write(PatchConstants.IDENTIFIER.getBytes("US-ASCII")); dataOut.writeInt(0); dataOut.writeLong(deltaFriendlyOldFileSize); dataOut.writeInt(plan.getOldFileUncompressionPlan().size()); for (TypedRange<Void> range : plan.getOldFileUncompressionPlan()) { dataOut.writeLong(range.getOffset()); dataOut.writeLong(range.getLength()); } dataOut.writeInt(plan.getDeltaFriendlyNewFileRecompressionPlan().size()); for (TypedRange<JreDeflateParameters> range : plan.getDeltaFriendlyNewFileRecompressionPlan()) { dataOut.writeLong(range.getOffset()); dataOut.writeLong(range.getLength()); dataOut.write(PatchConstants.CompatibilityWindowId.DEFAULT_DEFLATE.patchValue); dataOut.write(range.getMetadata().level); dataOut.write(range.getMetadata().strategy); dataOut.write(range.getMetadata().nowrap ? 1 : 0); } dataOut.writeInt(1); dataOut.write(PatchConstants.DeltaFormat.BSDIFF.patchValue); dataOut.writeLong(0); dataOut.writeLong(deltaFriendlyOldFileSize); dataOut.writeLong(0); dataOut.writeLong(deltaFriendlyNewFileSize); dataOut.writeLong(deltaFile.length()); try (FileInputStream deltaFileIn = new FileInputStream(deltaFile); BufferedInputStream deltaIn = new BufferedInputStream(deltaFileIn)) { byte[] buffer = new byte[32768]; int numRead = 0; while ((numRead = deltaIn.read(buffer)) >= 0) { dataOut.write(buffer, 0, numRead); } } dataOut.flush(); } PatchWriter(
      PreDiffPlan plan,
      long deltaFriendlyOldFileSize,
      long deltaFriendlyNewFileSize,
      File deltaFile); void writeV1Patch(OutputStream out);  }
@Test public void testWriteV1Patch() throws IOException { PatchWriter writer = new PatchWriter( PLAN, DELTA_FRIENDLY_OLD_FILE_SIZE, DELTA_FRIENDLY_NEW_FILE_SIZE, deltaFile); writer.writeV1Patch(buffer); DataInputStream patchIn = new DataInputStream(new ByteArrayInputStream(buffer.toByteArray())); byte[] eightBytes = new byte[8]; patchIn.readFully(eightBytes); Assert.assertArrayEquals(PatchConstants.IDENTIFIER.getBytes("US-ASCII"), eightBytes); Assert.assertEquals(0, patchIn.readInt()); Assert.assertEquals(DELTA_FRIENDLY_OLD_FILE_SIZE, patchIn.readLong()); Assert.assertEquals(1, patchIn.readInt()); Assert.assertEquals(OLD_DELTA_FRIENDLY_UNCOMPRESS_RANGE.getOffset(), patchIn.readLong()); Assert.assertEquals(OLD_DELTA_FRIENDLY_UNCOMPRESS_RANGE.getLength(), patchIn.readLong()); Assert.assertEquals(1, patchIn.readInt()); Assert.assertEquals(NEW_DELTA_FRIENDLY_RECOMPRESS_RANGE.getOffset(), patchIn.readLong()); Assert.assertEquals(NEW_DELTA_FRIENDLY_RECOMPRESS_RANGE.getLength(), patchIn.readLong()); Assert.assertEquals( PatchConstants.CompatibilityWindowId.DEFAULT_DEFLATE.patchValue, patchIn.read()); Assert.assertEquals(DEFLATE_PARAMS.level, patchIn.read()); Assert.assertEquals(DEFLATE_PARAMS.strategy, patchIn.read()); Assert.assertEquals(DEFLATE_PARAMS.nowrap ? 1 : 0, patchIn.read()); Assert.assertEquals(1, patchIn.readInt()); Assert.assertEquals(PatchConstants.DeltaFormat.BSDIFF.patchValue, patchIn.read()); Assert.assertEquals(0, patchIn.readLong()); Assert.assertEquals(DELTA_FRIENDLY_OLD_FILE_SIZE, patchIn.readLong()); Assert.assertEquals(0, patchIn.readLong()); Assert.assertEquals(DELTA_FRIENDLY_NEW_FILE_SIZE, patchIn.readLong()); byte[] expectedDeltaContent = DELTA_CONTENT.getBytes("US-ASCII"); Assert.assertEquals(expectedDeltaContent.length, patchIn.readLong()); byte[] actualDeltaContent = new byte[expectedDeltaContent.length]; patchIn.readFully(actualDeltaContent); Assert.assertArrayEquals(expectedDeltaContent, actualDeltaContent); }