public int peek(byte[] buffer) throws IOException {
        int n = 0;

        mark(buffer.length);

        int m = read(buffer);
        while (m != -1) {
            n += m;
            if (n < buffer.length) {
                m = read(buffer, n, buffer.length - n);
            } else {
                m = -1;
            }
        }

        reset();

        return n;
    }
Fills the given buffer with upcoming bytes from this stream without
advancing the current stream position. The buffer is filled up unless
the end of stream is encountered before that. This method will block
if not enough bytes are immediately available.

@param buffer byte buffer
@return number of bytes written to the buffer
@throws IOException if the stream can not be read
tika-core/src/main/java/org/apache/tika/io/TikaInputStream.java