BsPatch { static final long readBsdiffLong(InputStream in) throws PatchFormatException, IOException { long result = 0; for (int bitshift = 0; bitshift < 64; bitshift += 8) { result |= ((long) in.read()) << bitshift; } if (result == NEGATIVE_LONG_SIGN_MASK) { throw new PatchFormatException("read negative zero"); } if ((result & NEGATIVE_LONG_SIGN_MASK) != 0) { result = -(result & ~NEGATIVE_LONG_SIGN_MASK); } return result; }  static void applyPatch(
      RandomAccessFile oldData, OutputStream newData, InputStream patchData);  }
@Test public void testReadBsdiffLong() throws Exception { byte[] data = { (byte) 0x78, (byte) 0x56, (byte) 0x34, (byte) 0x12, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0xef, (byte) 0xbe, (byte) 0xad, (byte) 0x0e, (byte) 0, (byte) 0, (byte) 0, (byte) 0 }; ByteArrayInputStream inputStream = new ByteArrayInputStream(data); long actual = BsPatch.readBsdiffLong(inputStream); Assert.assertEquals(0x12345678, actual); actual = BsPatch.readBsdiffLong(inputStream); Assert.assertEquals(0x0eadbeef, actual); }
@Test public void testReadBsdiffLong_Zero() throws Exception { long expected = 0x00000000L; long actual = BsPatch.readBsdiffLong( new ByteArrayInputStream( new byte[] { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 })); Assert.assertEquals(expected, actual); }
@Test public void testReadBsdiffLong_IntegerMaxValue() throws Exception { long expected = 0x7fffffffL; long actual = BsPatch.readBsdiffLong( new ByteArrayInputStream( new byte[] { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x7f, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 })); Assert.assertEquals(expected, actual); }
@Test public void testReadBsdiffLong_IntegerMinValue() throws Exception { long expected = -0x80000000L; long actual = BsPatch.readBsdiffLong( new ByteArrayInputStream( new byte[] { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x80 })); Assert.assertEquals(expected, actual); }
@Test public void testReadBsdiffLong_LongMaxValue() throws Exception { long expected = 0x7fffffffffffffffL; long actual = BsPatch.readBsdiffLong( new ByteArrayInputStream( new byte[] { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x7f })); Assert.assertEquals(expected, actual); }
@Test public void testReadBsdiffLong_LongMinValueIsh() throws Exception { long expected = -0x7fffffffffffffffL; long actual = BsPatch.readBsdiffLong( new ByteArrayInputStream( new byte[] { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff })); Assert.assertEquals(expected, actual); }
@Test public void testReadBsdiffLong_NegativeZero() throws Exception { try { BsPatch.readBsdiffLong( new ByteArrayInputStream( new byte[] { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x80 })); Assert.fail("Tolerated negative zero"); } catch (PatchFormatException expected) { } }