Class MimeTypes
- All Implemented Interfaces:
Serializable
,Detector
The MIME type detection methods that take an InputStream
as
an argument will never reads more than getMinLength()
bytes
from the stream. Also the given stream is never
closed
, marked
,
or reset
by the methods. Thus a client can
use the mark feature
of the stream
(if available) to restore the stream back to the state it was before type
detection if it wants to process the stream based on the detected type.
- See Also:
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addPattern
(MimeType type, String pattern) Adds a file name pattern for the given media type.void
addPattern
(MimeType type, String pattern, boolean isRegex) Adds a file name pattern for the given media type.detect
(InputStream input, Metadata metadata) Automatically detects the MIME type of a document based on magic markers in the stream prefix and any given metadata hints.Returns the registered media type with the given name (or alias).static MimeTypes
Get the default MimeTypes.static MimeTypes
getDefaultMimeTypes
(ClassLoader classLoader) Get the default MimeTypes.getMimeType
(File file) Deprecated.getMimeType
(String name) Deprecated.UseTika.detect(String)
insteadint
Return the minimum length of data to provide to analyzing methods based on the document's content in order to check all the known MimeTypes.getRegisteredMimeType
(String name) Returns the registered, normalised media type with the given name (or alias).void
setSuperType
(MimeType type, MediaType parent)
-
Field Details
-
OCTET_STREAM
Name of theroot
type, application/octet-stream.- See Also:
-
PLAIN_TEXT
Name of thetext
type, text/plain.- See Also:
-
XML
Name of thexml
type, application/xml.- See Also:
-
-
Constructor Details
-
MimeTypes
public MimeTypes()
-
-
Method Details
-
getDefaultMimeTypes
Get the default MimeTypes. This includes all the build in media types, and any custom override ones present.- Returns:
- MimeTypes default type registry
-
getDefaultMimeTypes
Get the default MimeTypes. This includes all the built-in media types, and any custom override ones present.- Parameters:
classLoader
- to use, if not the default- Returns:
- MimeTypes default type registry
-
getMimeType
Deprecated.UseTika.detect(String)
insteadFind the Mime Content Type of a document from its name. Returns application/octet-stream if no better match is found.- Parameters:
name
- of the document to analyze.- Returns:
- the Mime Content Type of the specified document name
-
getMimeType
Deprecated.UseTika.detect(File)
insteadFind the Mime Content Type of a document stored in the given file. Returns application/octet-stream if no better match is found.- Parameters:
file
- file to analyze- Returns:
- the Mime Content Type of the specified document
- Throws:
MimeTypeException
- if the type can't be detectedIOException
- if the file can't be read
-
forName
Returns the registered media type with the given name (or alias). The named media type is automatically registered (and returned) if it doesn't already exist.- Parameters:
name
- media type name (case-insensitive)- Returns:
- the registered media type with the given name or alias
- Throws:
MimeTypeException
- if the given media type name is invalid
-
getRegisteredMimeType
Returns the registered, normalised media type with the given name (or alias).Unlike
forName(String)
, this function will not create a new MimeType and register it. Instead,null
will be returned if there is no definition available for the given name.Also, unlike
forName(String)
, this function may return a mime type that has fewer parameters than were included in the supplied name. If the registered mime type has parameters (e.g.application/dita+xml;format=map
), then those will be maintained. However, if the supplied name has paramenters that the registered mime type does not (e.g.application/xml; charset=UTF-8
as a name, compared to justapplication/xml
for the type in the registry), then those parameters will not be included in the returned type.- Parameters:
name
- media type name (case-insensitive)- Returns:
- the registered media type with the given name or alias, or null if not found
- Throws:
MimeTypeException
- if the given media type name is invalid
-
setSuperType
-
addPattern
Adds a file name pattern for the given media type. Assumes that the pattern being added is not a JDK standard regular expression.- Parameters:
type
- media typepattern
- file name pattern- Throws:
MimeTypeException
- if the pattern conflicts with existing ones
-
addPattern
Adds a file name pattern for the given media type. The caller can specify whether the pattern being added is or is not a JDK standard regular expression via theisRegex
parameter. If the value is set to true, then a JDK standard regex is assumed, otherwise the freedesktop glob type is assumed.- Parameters:
type
- media typepattern
- file name patternisRegex
- set to true if JDK std regexs are desired, otherwise set to false.- Throws:
MimeTypeException
- if the pattern conflicts with existing ones.
-
getMediaTypeRegistry
-
getMinLength
public int getMinLength()Return the minimum length of data to provide to analyzing methods based on the document's content in order to check all the known MimeTypes.- Returns:
- the minimum length of data to provide.
- See Also:
-
getMimeType(byte[])
-
detect
Automatically detects the MIME type of a document based on magic markers in the stream prefix and any given metadata hints.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.
- Specified by:
detect
in interfaceDetector
- Parameters:
input
- document stream, ornull
metadata
- metadata hints- Returns:
- MIME type of the document
- Throws:
IOException
- if the document stream could not be read
-
Tika.detect(File)
instead