BsPatch { static void transformBytes( final int diffLength, final InputStream patchData, final RandomAccessFile oldData, final OutputStream newData, final byte[] buffer1, final byte[] buffer2) throws IOException { int numBytesLeft = diffLength; while (numBytesLeft > 0) { final int numBytesThisRound = Math.min(numBytesLeft, buffer1.length); oldData.readFully(buffer1, 0, numBytesThisRound); readFully(patchData, buffer2, 0, numBytesThisRound); for (int i = 0; i < numBytesThisRound; i++) { buffer1[i] += buffer2[i]; } newData.write(buffer1, 0, numBytesThisRound); numBytesLeft -= numBytesThisRound; } }  static void applyPatch(
      RandomAccessFile oldData, OutputStream newData, InputStream patchData); static void applyPatch(
      RandomAccessFile oldData, OutputStream newData, InputStream patchData, Long expectedNewSize);  }
@Test public void testTransformBytes() throws IOException { final byte[] patchInput = "this is a sample string to read".getBytes("US-ASCII"); final ByteArrayInputStream patchInputStream = new ByteArrayInputStream(patchInput); copyToOldFile("bsdifftest_partial_a.txt"); RandomAccessFile oldData = new RandomAccessFile(oldFile, "r"); final byte[] expectedNewData = readTestData("bsdifftest_partial_b.bin"); ByteArrayOutputStream newData = new ByteArrayOutputStream(); BsPatch.transformBytes(patchInput.length, patchInputStream, oldData, newData, buffer1, buffer2); byte[] actual = newData.toByteArray(); Assert.assertArrayEquals(expectedNewData, actual); }
@Test public void testTransformBytes_Error_NotEnoughBytes() throws IOException { final InputStream patchIn = new ByteArrayInputStream(new byte[] {(byte) 0x00}); copyToOldFile("bsdifftest_partial_a.txt"); RandomAccessFile oldData = new RandomAccessFile(oldFile, "r"); try { BsPatch.transformBytes(2, patchIn, oldData, new ByteArrayOutputStream(), buffer1, buffer2); Assert.fail("Read past EOF"); } catch (IOException expected) { } }
@Test public void testTransformBytes_Error_JunkPatch() throws IOException { final byte[] patchInput = "this is a second sample string to read".getBytes("US-ASCII"); final ByteArrayInputStream patchInputStream = new ByteArrayInputStream(patchInput); copyToOldFile("bsdifftest_partial_a.txt"); RandomAccessFile oldData = new RandomAccessFile(oldFile, "r"); ByteArrayOutputStream newData = new ByteArrayOutputStream(); try { BsPatch.transformBytes( patchInput.length, patchInputStream, oldData, newData, buffer1, buffer2); Assert.fail("Should have thrown an IOException"); } catch (IOException expected) { } }
@Test public void testTransformBytes_Error_JunkPatch_Underflow() throws IOException { final byte[] patchInput = "this is a sample string".getBytes("US-ASCII"); final ByteArrayInputStream patchInputStream = new ByteArrayInputStream(patchInput); copyToOldFile("bsdifftest_partial_a.txt"); RandomAccessFile oldData = new RandomAccessFile(oldFile, "r"); final byte[] buffer1 = new byte[6]; final byte[] buffer2 = new byte[6]; ByteArrayOutputStream newData = new ByteArrayOutputStream(); try { BsPatch.transformBytes( patchInput.length + 1, patchInputStream, oldData, newData, buffer1, buffer2); Assert.fail("Should have thrown an IOException"); } catch (IOException expected) { } }