private void writeAndCloseStream(DataOutputStream output, InputStream stream)
            throws IOException {
        try {
            byte[] buffer = new byte[0x10000 - 1];
            int n;
            while ((n = stream.read(buffer)) != -1) {
                output.writeShort(n);
                output.write(buffer, 0, n);
            }
            output.writeShort(0);
        } finally {
            stream.close();
        }
    }
Sends the contents of the given input stream to the given output.
The stream is sent in chunks of less than 64kB, each preceded by
a 16-bit integer value that indicates the length of the following
chunk. A zero short value is sent at the end to signify the end of
the stream.
<p>
The stream is guaranteed to be closed by this method, regardless of
the way it returns.

@param stream the stream to be sent
@throws IOException if the stream could not be sent
tika-core/src/main/java/org/apache/tika/fork/ClassLoaderResource.java