@Override
    public MediaType detect(InputStream input, Metadata metadata) throws IOException {
        List<MimeType> possibleTypes = null;

        // Get type based on magic prefix
        if (input != null) {
            input.mark(getMinLength());
            try {
                byte[] prefix = readMagicHeader(input);
                possibleTypes = getMimeType(prefix);
            } finally {
                input.reset();
            }
        }

        // Get type based on resourceName hint (if available)
        String resourceName = metadata.get(TikaCoreProperties.RESOURCE_NAME_KEY);
        if (resourceName != null) {
            String name = null;
            boolean isHttp = false;

            // Deal with a URI or a path name in as the resource  name
            try {
                URI uri = new URI(resourceName);
                String scheme = uri.getScheme();
                isHttp = scheme != null && scheme.startsWith("http"); // http or https
                String path = uri.getPath();
                if (path != null) {
                    int slash = path.lastIndexOf('/');
                    if (slash + 1 < path.length()) {
                        name = path.substring(slash + 1);
                    }
                }
            } catch (URISyntaxException e) {
                name = resourceName;
            }

            if (name != null) {
                MimeType hint = getMimeType(name);

                // For server-side scripting languages, we cannot rely on the filename to detect
                // the mime type
                if (!(isHttp && hint.isInterpreted())) {
                    // If we have some types based on mime magic, try to specialise
                    //  and/or select the type based on that
                    // Otherwise, use the type identified from the name
                    possibleTypes = applyHint(possibleTypes, hint);
                }
            }
        }

        // Get type based on metadata hint (if available)
        String typeName = metadata.get(Metadata.CONTENT_TYPE);
        if (typeName != null) {
            try {
                MimeType hint = forName(typeName);
                possibleTypes = applyHint(possibleTypes, hint);
            } catch (MimeTypeException e) {
                // Malformed type name, ignore
            }
        }

        if (possibleTypes == null || possibleTypes.isEmpty()) {
            // Report that we don't know what it is
            return MediaType.OCTET_STREAM;
        } else {
            return possibleTypes.get(0).getType();
        }
    }
Automatically detects the MIME type of a document based on magic
markers in the stream prefix and any given metadata hints.
<p>
The given stream is expected to support marks, so that this method
can reset the stream to the position it was in before this method
was called.

@param input    document stream, or <code>null</code>
@param metadata metadata hints
@return MIME type of the document
@throws IOException if the document stream could not be read
tika-core/src/main/java/org/apache/tika/mime/MimeTypes.java