|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object eu.medsea.mimeutil.MimeUtil2
public class MimeUtil2
The MimeUtil2
is a utility class that allows applications to detect, work with and manipulate MIME types.
A MIME or "Multipurpose Internet Mail Extension" type is an Internet standard that is important outside of just e-mail use. MIME is used extensively in other communications protocols such as HTTP for web communications. IANA "Internet Assigned Numbers Authority" is responsible for the standardisation and publication of MIME types. Basically any resource on any computer that can be located via a URL can be assigned a MIME type. So for instance, JPEG images have a MIME type of image/jpg. Some resources can have multiple MIME types associated with them such as files with an XML extension have the MIME types text/xml and application/xml and even specialised versions of xml such as image/svg+xml for SVG image files.
To do this MimeUtil2
uses registered MimeDetector
(s) that are delegated too in sequence to actually
perform the detection. There are several MimeDetector
implementations that come with the utility and
you can register and unregister them to perform detection based on file extensions, file globing and magic number detection.
Their is also a fourth MimeDetector that is registered by default that detects text files and encodings. Unlike the other
MimeDetector(s) or any MimeDetector(s) you may choose to implement, the TextMimeDetector cannot be registered or
unregistered by your code. It is advisable that you read the java doc for the TextMimeDetector as it can be modified in
several ways to make it perform better and or detect more specific types.
Please refer to the java doc for each of these MimeDetector
(s) for a description of how they
actually perform their particular detection process.
It is important to note that MIME matching is not an exact science, meaning that a positive match does not guarantee that the returned MIME type is actually correct. It is a best guess method of matching and the matched MIME types should be used with this in mind.
New MimeDetector
(s) can easily be created and registered with MimeUtil2
to extend it's
functionality beyond these initial detection strategies by extending the AbstractMimeDetector
class.
To see how to implement your own MimeDetector
take a look
at the java doc and source code for the ExtensionMimeDetector
, MagicMimeMimeDetector
and
OpendesktopMimeDetector
classes. To register and unregister MimeDetector(s) use the
[un]registerMimeDetector(...) methods of this class.
The order that the MimeDetector
(s) are executed is defined by the order each MimeDetector
is registered.
The resulting Collection
of mime types returned in response to a getMimeTypes(...) call is a normalised list of the
accumulation of MIME types returned by each of the registered MimeDetector
(s) that implement the specified getMimeTypesXXX(...)
methods.
All methods in this class that return a Collection object containing MimeType(s) actually return a MimeTypeHashSet
that implements both the Set
and Collection
interfaces.
Field Summary | |
---|---|
static MimeType |
DIRECTORY_MIME_TYPE
Mime type used to identify a directory |
static MimeType |
UNKNOWN_MIME_TYPE
Mime type used to identify an unknown MIME type |
Constructor Summary | |
---|---|
MimeUtil2()
|
Method Summary | |
---|---|
static void |
addKnownMimeType(MimeType mimeType)
While MimeType(s) are being loaded by the MimeDetector(s) they should be added to the list of known MIME types. |
static void |
addKnownMimeType(String mimeType)
While MimeType(s) are being loaded by the MimeDetector(s) they should be added to the list of known MIME types. |
static String |
getExtension(File file)
Get the extension part of a file name defined by the file parameter. |
static String |
getExtension(String fileName)
Get the extension part of a file name defined by the fileName parameter. |
static MimeType |
getFirstMimeType(String mimeTypes)
Get the first in a comma separated list of mime types. |
static InputStream |
getInputStreamForURL(URL url)
Utility method to get the InputStream from a URL. |
static Collection |
getKnownMimeTypes()
Returns a copy of the Collection of currently known MIME types as strings that have been registered either by the initialisation methods of the MimeDetector(s) or by the user. |
static String |
getMediaType(String mimeType)
Utility method to get the major or media part of a mime type i.e. |
MimeDetector |
getMimeDetector(String name)
Get a registered MimeDetector by name. |
static double |
getMimeQuality(String mimeType)
Utility method to get the quality part of a mime type. |
Collection |
getMimeTypes(byte[] data)
Get a Collection of possible MimeType(s) that this byte array could represent according to the registered MimeDetector(s). |
Collection |
getMimeTypes(byte[] data,
MimeType unknownMimeType)
Get a Collection of possible MimeType(s) that this byte array could represent according to the registered MimeDetector(s). |
Collection |
getMimeTypes(File file)
Get all of the matching mime types for this file object. |
Collection |
getMimeTypes(File file,
MimeType unknownMimeType)
Get all of the matching mime types for this file object. |
Collection |
getMimeTypes(InputStream in)
Get all of the matching mime types for this InputStream object. |
Collection |
getMimeTypes(InputStream in,
MimeType unknownMimeType)
Get all of the matching mime types for this InputStream object. |
Collection |
getMimeTypes(String fileName)
Get all of the matching mime types for this file name. |
Collection |
getMimeTypes(String fileName,
MimeType unknownMimeType)
Get all of the matching mime types for this file name . |
Collection |
getMimeTypes(URL url)
Get all of the matching mime types for this URL object. |
Collection |
getMimeTypes(URL url,
MimeType unknownMimeType)
|
static MimeType |
getMostSpecificMimeType(Collection mimeTypes)
Get the most specific match of the Collection of mime types passed in. |
static ByteOrder |
getNativeOrder()
Get the native byte order of the OS on which you are running. |
static MimeType |
getPreferedMimeType(String accept,
String canProvide)
Gives you the best match for your requirements. |
static double |
getQuality(String mimeType)
Get the quality parameter of this mime type i.e. |
static String |
getSubType(String mimeType)
Utility method to get the minor part of a mime type i.e. |
static boolean |
isMimeTypeKnown(MimeType mimeType)
Check to see if this mime type is one of the types seen during initialisation or has been added at some later stage using addKnownMimeType(...) |
static boolean |
isMimeTypeKnown(String mimeType)
Check to see if this mime type is one of the types seen during initialisation or has been added at some later stage using addKnownMimeType(...) |
static boolean |
isTextMimeType(MimeType mimeType)
Utility convenience method to check if a particular MimeType instance is actually a TextMimeType. |
MimeDetector |
registerMimeDetector(String mimeDetector)
Register a MimeDetector and add it to the MimeDetector registry. |
MimeDetector |
unregisterMimeDetector(MimeDetector mimeDetector)
Remove a previously registered MimeDetector |
MimeDetector |
unregisterMimeDetector(String mimeDetector)
Remove a previously registered MimeDetector |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final MimeType DIRECTORY_MIME_TYPE
public static final MimeType UNKNOWN_MIME_TYPE
Constructor Detail |
---|
public MimeUtil2()
Method Detail |
---|
public static void addKnownMimeType(MimeType mimeType)
This can be used to limit the mime types you work with i.e. if its not been loaded then don't bother using it as it won't match. This is no guarantee that a match will not be found as it is possible that a particular MimeDetector does not have an initialisation phase that loads all of the MIME types it will match.
For instance if you had a MIME type of abc/xyz and passed this to isMimeTypeKnown(...) it would return false unless you specifically add this to the know MIME types using this method.
mimeType
- a MIME type you want to add to the known MIME types.
Duplicates are ignored.isMimeTypeKnown(String mimeType)
,
isMimeTypeKnown(MimeType mimetType)
public static void addKnownMimeType(String mimeType)
This can be used to limit the mime types you work with i.e. if its not been loaded then don't bother using it as it won't match. This is no guarantee that a match will not be found as it is possible that a particular MimeDetector does not have an initialisation phase that loads all of the MIME types it will match.
For instance if you had a MIME type of abc/xyz and passed this to isMimeTypeKnown(...) it would return false unless you specifically add this to the know MIME types using this method.
mimeType
- a MIME type you want to add to the known MIME types.
Duplicates are ignored.isMimeTypeKnown(String mimetype)
,
isMimeTypeKnown(MimeType mimetType)
public static Collection getKnownMimeTypes()
public MimeDetector registerMimeDetector(String mimeDetector)
mimeDetector.
- This must be the fully qualified name of a concrete instance of an
AbstractMimeDetector class.
This enforces that all custom MimeDetector(s) extend the AbstractMimeDetector.MimeDetector
public static String getExtension(File file)
file
- a file object
public static String getExtension(String fileName)
fileName
- a relative or absolute path to a file
public static MimeType getFirstMimeType(String mimeTypes)
mimeTypes
- comma separated list of mime types
public static String getMediaType(String mimeType) throws MimeException
mimeType
- you want to get the media part from
MimeException
- if you pass in an invalid mime type structurepublic static double getMimeQuality(String mimeType) throws MimeException
Thanks to the Apache organisation for these settings.
mimeType
- a valid mime type string with or without a valid q parameter
MimeException
- this is thrown if the mime type pattern is invalid.public MimeDetector getMimeDetector(String name)
name
- the name of a registered MimeDetector. This is always the fully qualified
name of the class implementing the MimeDetector.
public final Collection getMimeTypes(byte[] data) throws MimeException
data
-
MimeException
public final Collection getMimeTypes(byte[] data, MimeType unknownMimeType) throws MimeException
data
- unknownMimeType
- used if the registered MimeDetector(s) fail to match any MimeType(s)
MimeException
public final Collection getMimeTypes(File file) throws MimeException
file
- the File object to detect.
MimeException
- if there are problems such as reading files generated when the MimeHandler(s)
executed.public final Collection getMimeTypes(File file, MimeType unknownMimeType) throws MimeException
file
- the File object to detect.unknownMimeType.
-
MimeException
- if there are problems such as reading files generated when the MimeHandler(s)
executed.public final Collection getMimeTypes(InputStream in) throws MimeException
in
- InputStream to detect.
MimeException
- if there are problems such as reading files generated when the MimeHandler(s)
executed.public final Collection getMimeTypes(InputStream in, MimeType unknownMimeType) throws MimeException
in
- the InputStream object to detect.unknownMimeType.
-
MimeException
- if there are problems such as reading files generated when the MimeHandler(s)
executed.public final Collection getMimeTypes(String fileName) throws MimeException
fileName
- the name of a file to detect.
MimeException
- if there are problems such as reading files generated when the MimeHandler(s)
executed.public final Collection getMimeTypes(String fileName, MimeType unknownMimeType) throws MimeException
fileName
- the name of a file to detect.unknownMimeType.
-
MimeException
- if there are problems such as reading files generated when the MimeHandler(s)
executed.public final Collection getMimeTypes(URL url) throws MimeException
url
- a URL to detect.
MimeException
- if there are problems such as reading files generated when the MimeHandler(s)
executed.public final Collection getMimeTypes(URL url, MimeType unknownMimeType) throws MimeException
MimeException
public static ByteOrder getNativeOrder()
public static MimeType getPreferedMimeType(String accept, String canProvide)
You can pass the accept header from a browser request to this method along with a comma separated list of possible mime types returned from say getExtensionMimeTypes(...) and the best match according to the accept header will be returned.
The following is typical of what may be specified in an HTTP Accept header:
Accept: text/xml, application/xml, application/xhtml+xml, text/html;q=0.9, text/plain;q=0.8, video/x-mng, image/png, image/jpeg, image/gif;q=0.2, text/css, */*;q=0.1
The quality parameter (q) indicates how well the user agent handles the MIME type. A value of 1 indicates the MIME type is understood perfectly, and a value of 0 indicates the MIME type isn't understood at all.
The reason the image/gif MIME type contains a quality parameter of 0.2, is to indicate that PNG & JPEG are preferred over GIF if the server is using content negotiation to deliver either a PNG or a GIF to user agents. Similarly, the text/html quality parameter has been lowered a little, to ensure that the XML MIME types are given in preference if content negotiation is being used to serve an XHTML document.
accept
- is a comma separated list of mime types you can accept
including QoS parameters. Can pass the Accept: header
directly.canProvide
- is a comma separated list of mime types that can be provided
such as that returned from a call to
getExtensionMimeTypes(...)
public static MimeType getMostSpecificMimeType(Collection mimeTypes)
mimeTypes
- this should be the Collection of mime types returned
from a getMimeTypes(...) call.
public static String getSubType(String mimeType) throws MimeException
mimeType
- you want to get the minor part from
MimeException
- if you pass in an invalid mime type structurepublic static boolean isMimeTypeKnown(MimeType mimeType)
mimeType
-
addKnownMimeType(String mimetype)
public static boolean isMimeTypeKnown(String mimeType)
mimeType
-
addKnownMimeType(String mimetype)
public static boolean isTextMimeType(MimeType mimeType)
mimeType
-
MimeType
,
TextMimeType
public MimeDetector unregisterMimeDetector(MimeDetector mimeDetector)
mimeDetector
-
public MimeDetector unregisterMimeDetector(String mimeDetector)
mimeDetector
-
public static double getQuality(String mimeType) throws MimeException
q=
property.
This method implements a value system similar to that used by the apache server i.e.
if the media type is a * then it's q
value is set to 0.01 and if the sub type is
a * then the q
value is set to 0.02 unless a specific q
value is specified. If a q
property is set it is limited to a max value of 1.0
mimeType
-
MimeException
public static InputStream getInputStreamForURL(URL url) throws Exception
url
-
Exception
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |