public final class MimeTypes extends java.lang.Object implements Detector, java.io.Serializable
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.
Modifier and Type | Field and Description |
---|---|
private static java.util.Map<java.lang.ClassLoader,MimeTypes> |
CLASSLOADER_SPECIFIC_DEFAULT_TYPES |
private static MimeTypes |
DEFAULT_TYPES |
private java.util.List<Magic> |
magics
Sorted list of all registered magics
|
static java.lang.String |
OCTET_STREAM
Name of the
root type, application/octet-stream. |
private Patterns |
patterns
The patterns matcher
|
static java.lang.String |
PLAIN_TEXT
Name of the
text type, text/plain. |
private MediaTypeRegistry |
registry
Registered media types and their aliases.
|
private MimeType |
rootMimeType
Root type, application/octet-stream.
|
private java.util.List<MimeType> |
rootMimeTypeL |
private static long |
serialVersionUID
Serial version UID.
|
private MimeType |
textMimeType
Text type, text/plain.
|
private java.util.Map<MediaType,MimeType> |
types
All the registered MimeTypes indexed on their canonical names
|
static java.lang.String |
XML
Name of the
xml type, application/xml. |
private MimeType |
xmlMimeType |
private java.util.List<MimeType> |
xmls
Sorted list of all registered rootXML
|
Constructor and Description |
---|
MimeTypes() |
Modifier and Type | Method and Description |
---|---|
(package private) void |
add(MimeType type)
Add the specified mime-type in the repository.
|
(package private) void |
addAlias(MimeType type,
MediaType alias)
Adds an alias for the given media type.
|
void |
addPattern(MimeType type,
java.lang.String pattern)
Adds a file name pattern for the given media type.
|
void |
addPattern(MimeType type,
java.lang.String pattern,
boolean isRegex)
Adds a file name pattern for the given media type.
|
private java.util.List<MimeType> |
applyHint(java.util.List<MimeType> possibleTypes,
MimeType hint)
Use the MimeType hint to try to clarify or specialise the current
possible types list.
|
MediaType |
detect(java.io.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.
|
MimeType |
forName(java.lang.String name)
Returns the registered media type with the given name (or alias).
|
static MimeTypes |
getDefaultMimeTypes()
Get the default MimeTypes.
|
static MimeTypes |
getDefaultMimeTypes(java.lang.ClassLoader classLoader)
Get the default MimeTypes.
|
MediaTypeRegistry |
getMediaTypeRegistry() |
(package private) java.util.List<MimeType> |
getMimeType(byte[] data)
Returns the MIME type that best matches the given first few bytes
of a document stream.
|
MimeType |
getMimeType(java.io.File file)
Deprecated.
Use
Tika.detect(File) instead |
MimeType |
getMimeType(java.lang.String name)
Deprecated.
Use
Tika.detect(String) instead |
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.
|
MimeType |
getRegisteredMimeType(java.lang.String name)
Returns the registered, normalised media type with the given name (or alias).
|
(package private) void |
init()
Called after all configured types have been loaded.
|
(package private) byte[] |
readMagicHeader(java.io.InputStream stream)
Reads the first
getMinLength() bytes from the given stream. |
void |
setSuperType(MimeType type,
MediaType parent) |
private static final long serialVersionUID
public static final java.lang.String OCTET_STREAM
root
type, application/octet-stream.public static final java.lang.String PLAIN_TEXT
text
type, text/plain.public static final java.lang.String XML
xml
type, application/xml.private final MimeType rootMimeType
private final java.util.List<MimeType> rootMimeTypeL
private final MimeType textMimeType
private final MimeType xmlMimeType
private final MediaTypeRegistry registry
private final java.util.Map<MediaType,MimeType> types
private Patterns patterns
private final java.util.List<Magic> magics
private final java.util.List<MimeType> xmls
private static MimeTypes DEFAULT_TYPES
private static java.util.Map<java.lang.ClassLoader,MimeTypes> CLASSLOADER_SPECIFIC_DEFAULT_TYPES
public MimeType getMimeType(java.lang.String name)
Tika.detect(String)
insteadname
- of the document to analyze.public MimeType getMimeType(java.io.File file) throws MimeTypeException, java.io.IOException
Tika.detect(File)
insteadfile
- file to analyzeMimeTypeException
- if the type can't be detectedjava.io.IOException
- if the file can't be readjava.util.List<MimeType> getMimeType(byte[] data)
If multiple matches are found, the best (highest priority) matching type is returned. If multiple matches are found with the same priority, then all of these are returned.
The given byte array is expected to be at least getMinLength()
long, or shorter only if the document stream itself is shorter.
data
- first few bytes of a document streambyte[] readMagicHeader(java.io.InputStream stream) throws java.io.IOException
getMinLength()
bytes from the given stream.
If the stream is shorter, then the entire content of the stream is
returned.
The given stream is never closed
,
marked
, or
reset
by this method.
stream
- stream to be readgetMinLength()
(or fewer) bytes of the streamjava.io.IOException
- if the stream can not be readpublic MimeType forName(java.lang.String name) throws MimeTypeException
name
- media type name (case-insensitive)MimeTypeException
- if the given media type name is invalidpublic MimeType getRegisteredMimeType(java.lang.String name) throws MimeTypeException
Unlike Also, 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.
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 just application/xml
for the type in the registry),
then those parameters will not be included in the returned type.
name
- media type name (case-insensitive)MimeTypeException
- if the given media type name is invalidvoid addAlias(MimeType type, MediaType alias)
MimeType#addAlias(String)
.type
- media typealias
- media type alias (normalized to lower case)public void addPattern(MimeType type, java.lang.String pattern) throws MimeTypeException
type
- media typepattern
- file name patternMimeTypeException
- if the pattern conflicts with existing onespublic void addPattern(MimeType type, java.lang.String pattern, boolean isRegex) throws MimeTypeException
isRegex
parameter. If the value
is set to true, then a JDK standard regex is assumed, otherwise the
freedesktop glob type is assumed.type
- media typepattern
- file name patternisRegex
- set to true if JDK std regexs are desired, otherwise set to
false.MimeTypeException
- if the pattern conflicts with existing ones.public MediaTypeRegistry getMediaTypeRegistry()
public int getMinLength()
getMimeType(byte[])
,
#getMimeType(String, byte[])
void add(MimeType type)
type
- is the mime-type to add.void init()
public MediaType detect(java.io.InputStream input, Metadata metadata) throws java.io.IOException
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.
private java.util.List<MimeType> applyHint(java.util.List<MimeType> possibleTypes, MimeType hint)
public static MimeTypes getDefaultMimeTypes()
public static MimeTypes getDefaultMimeTypes(java.lang.ClassLoader classLoader)
classLoader
- to use, if not the default