byte[] readMagicHeader(InputStream stream) throws IOException {
        if (stream == null) {
            throw new IllegalArgumentException("InputStream is missing");
        }

        byte[] bytes = new byte[getMinLength()];
        int totalRead = 0;

        int lastRead = stream.read(bytes);
        while (lastRead != -1) {
            totalRead += lastRead;
            if (totalRead == bytes.length) {
                return bytes;
            }
            lastRead = stream.read(bytes, totalRead, bytes.length - totalRead);
        }

        byte[] shorter = new byte[totalRead];
        System.arraycopy(bytes, 0, shorter, 0, totalRead);
        return shorter;
    }
Reads the first {@link #getMinLength()} bytes from the given stream.
If the stream is shorter, then the entire content of the stream is
returned.
<p>
The given stream is never {@link InputStream#close() closed},
{@link InputStream#mark(int) marked}, or
{@link InputStream#reset() reset} by this method.

@param stream stream to be read
@return first {@link #getMinLength()} (or fewer) bytes of the stream
@throws IOException if the stream can not be read
tika-core/src/main/java/org/apache/tika/mime/MimeTypes.java