Pipes with callbacks.

This is rather specialized, but can be used to avoid a thread creation if your pipe needs some kind of notification, and you’re willing not to be very precise, or possibly to loose some performance because of wrong buffer size. You can have the same effect by creating a filteroutputstream, but blah right?


        public static void writeInto(final InputStream input, boolean closeInput, final OutputStream output, boolean closeOutput, final int bufferSize, final Runnable continuation) throws IOException {
        final byte[] buffer = new byte[bufferSize];
        int n = 0;
        try {
            while (-1 != (n = input.read(buffer))) {
                output.write(buffer, 0, n);
                continuation.run();
            }
        } finally {
            if (closeInput && input != null) {
                input.close();
            }
            if (closeOutput && output != null) {
                output.close();
            }
        }
    }