public static InputStream ensureStreamReReadable(InputStream stream, TemporaryResources tmp,
                                                     Metadata metadata)
            throws IOException {
        // If it's re-readable, we're done
        if (stream instanceof RereadableInputStream) {
            return stream;
        }

        // Make sure it's a TikaInputStream
        TikaInputStream tstream = TikaInputStream.cast(stream);
        if (tstream == null) {
            tstream = TikaInputStream.get(stream, tmp, metadata);
        }

        // If it's factory based, it's ok
        if (tstream.getInputStreamFactory() != null) {
            return tstream;
        }

        // Ensure it's file based
        tstream.getFile();
        // Prepare for future re-reads
        tstream.mark(-1);
        return tstream;
    }
Ensures that the Stream will be able to be re-read, by buffering to
a temporary file if required.
Streams that are automatically OK include {@link TikaInputStream}s
created from Files or InputStreamFactories, and {@link RereadableInputStream}.
tika-core/src/main/java/org/apache/tika/utils/ParserUtils.java