BsPatch { static void pipe( final InputStream in, final OutputStream out, final byte[] buffer, int copyLength) throws IOException { while (copyLength > 0) { int maxCopy = Math.min(buffer.length, copyLength); readFully(in, buffer, 0, maxCopy); out.write(buffer, 0, maxCopy); copyLength -= maxCopy; } }  static void applyPatch(
      RandomAccessFile oldData, OutputStream newData, InputStream patchData); static void applyPatch(
      RandomAccessFile oldData, OutputStream newData, InputStream patchData, Long expectedNewSize);  }
@Test public void testPipe() throws IOException { final String inputString = "this is a sample string to read"; final byte[] input = inputString.getBytes("US-ASCII"); final ByteArrayInputStream inputStream = new ByteArrayInputStream(input); final byte[] buffer = new byte[5]; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); BsPatch.pipe(inputStream, outputStream, buffer, 0); int actualLength = outputStream.toByteArray().length; Assert.assertEquals(0, actualLength); inputStream.reset(); BsPatch.pipe(inputStream, outputStream, buffer, 1); actualLength = outputStream.toByteArray().length; Assert.assertEquals(1, actualLength); byte actualByte = outputStream.toByteArray()[0]; Assert.assertEquals((byte) 't', actualByte); outputStream = new ByteArrayOutputStream(); inputStream.reset(); BsPatch.pipe(inputStream, outputStream, buffer, 5); actualLength = outputStream.toByteArray().length; Assert.assertEquals(5, actualLength); String actualOutput = outputStream.toString(); String expectedOutput = inputString.substring(0, 5); Assert.assertEquals(expectedOutput, actualOutput); outputStream = new ByteArrayOutputStream(); inputStream.reset(); BsPatch.pipe(inputStream, outputStream, buffer, input.length); actualLength = outputStream.toByteArray().length; Assert.assertEquals(input.length, actualLength); expectedOutput = outputStream.toString(); Assert.assertEquals(inputString, expectedOutput); }
@Test public void testPipe_Underrun() { int dataLength = 10; ByteArrayInputStream in = new ByteArrayInputStream(new byte[dataLength]); try { BsPatch.pipe(in, new ByteArrayOutputStream(), new byte[dataLength], dataLength + 1); Assert.fail("Should've thrown an IOException"); } catch (IOException expected) { } }
@Test public void testPipe_CopyZeroBytes() throws IOException { int dataLength = 0; ByteArrayInputStream in = new ByteArrayInputStream(new byte[dataLength]); ByteArrayOutputStream out = new ByteArrayOutputStream(); BsPatch.pipe(in, out, new byte[100], dataLength); int actualLength = out.toByteArray().length; Assert.assertEquals(0, actualLength); }