private static long transfer(final InputStream inputStream, final OutputStream outputStream, final byte[] buffer) throws IOException {
        long totalLength = 0L;
        while (true) {
            final int lengthRead = inputStream.read(buffer);
            if (lengthRead < 0) {
                break;
            }
            outputStream.write(buffer, 0, lengthRead);
            totalLength += lengthRead;
        }
        return totalLength;
    }
Does the same with Java 9+'s {@code InputStream#transferTo}.
embulk-core/src/main/java/org/embulk/cli/SelfContainedJarFile.java