Class ExtensionMimeDetector
The extension mime mappings are loaded in the following way.
- Load the properties file from the mime utility jar named
eu.medsea.mimeutil.mime-types.properties
. - Locate and load a file named
.mime-types.properties
from the users home directory if one exists. - Locate and load a file named
mime-types.properties
from the classpath if one exists - locate and load a file named by the JVM property
mime-mappings
i.e.-Dmime-mappings=../my-mime-types.properties
Fortunately, we have compiled a relatively large list of mappings into a java properties file from information gleaned from many sites on the Internet. This file resides in the eu.medsea.util.mime-types.properties file and is not guaranteed to be correct or contain all the known mappings for a file extension type. This is not a complete or exhaustive list as that would have proven too difficult to compile for this project. So instead we give you the opportunity to extend and override these mappings for yourself as defined above. Obviously, to use this method you don't actually need a file object, you just need a file name with an extension. Also, if you have given or renamed a file using a different extension than the one that it would normally be associated with then this mapping will return the wrong mime-type and if the file has no extension at all, such as Make, then it's not going to be possible to determine a mime type using this technique
We acquired many mappings from many different sources on the net for the extension mappings. The internal list is quite large and there can be many associated mime types. These may not match what you are expecting so you can add the mapping you want to change to your own property file following the rules above. If you provide a mapping for an extension then any previously loaded mappings will be removed and only the mappings you define will be returned. This can be used to map certain extensions that are incorrectly returned for our environment defined in the internal property file.
If we have not provided a mapping for a file extension that you know the mime type for you can add this to your custom property files so that a correct mime type is returned for you.
We use the application/directory
mime type to identify
directories. Even though this is not an official mime type it seems to be
well accepted on the net as an unofficial mime type so we thought it was OK
for us to use as well.
This class is auto loaded by MimeUtil as it has an entry in the file called MimeDetectors. MimeUtil reads this file at startup and calls Class.forName() on each entry found. This mean the MimeDetector must have a no arg constructor.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAbstract method to be implement by concrete MimeDetector(s).getMimeTypesByteArray
(byte[] data) This method is required by the abstract MimeDetector class.getMimeTypesFile
(File file) Get the mime type of a file using extension mappings.getMimeTypesFileName
(String fileName) Get the mime type of a file name using file name extension mappings.This method is required by the abstract MimeDetector class.getMimeTypesURL
(URL url) Get the mime type of a URL using extension mappings.Methods inherited from class eu.medsea.mimeutil.detector.MimeDetector
closeStream, delete, getMimeTypes, getMimeTypes, getMimeTypes, getMimeTypes, getMimeTypes, getName, init
-
Constructor Details
-
ExtensionMimeDetector
public ExtensionMimeDetector()
-
-
Method Details
-
getDescription
Description copied from class:MimeDetector
Abstract method to be implement by concrete MimeDetector(s).- Specified by:
getDescription
in classMimeDetector
- Returns:
- description of this MimeDetector
-
getMimeTypesFile
Get the mime type of a file using extension mappings. The file path can be a relative or absolute path or can refer to a completely non-existent file as only the extension is important here.- Specified by:
getMimeTypesFile
in classMimeDetector
- Parameters:
file
- points to a file or directory. May not actually exist- Returns:
- collection of the matched mime types.
- Throws:
MimeException
- if errors occur.
-
getMimeTypesURL
Get the mime type of a URL using extension mappings. Only the extension is important here.- Specified by:
getMimeTypesURL
in classMimeDetector
- Parameters:
url
- is a valid URL- Returns:
- collection of the matched mime types.
- Throws:
MimeException
- if errors occur.
-
getMimeTypesFileName
Get the mime type of a file name using file name extension mappings. The file name path can be a relative or absolute path or can refer to a completely non-existent file as only the extension is important here.- Specified by:
getMimeTypesFileName
in classMimeDetector
- Parameters:
fileName
- points to a file or directory. May not actually exist- Returns:
- collection of the matched mime types.
- Throws:
MimeException
- if errors occur.
-
getMimeTypesInputStream
This method is required by the abstract MimeDetector class. As we do not support extension mapping of streams we just throw anUnsupportedOperationException
. This ensures that the getMimeTypes(...) methods ignore this method. We could also have just returned an empty collection.- Specified by:
getMimeTypesInputStream
in classMimeDetector
- Parameters:
in
- InputStream.- Returns:
- Collection of matched MimeType(s)
- Throws:
UnsupportedOperationException
-
getMimeTypesByteArray
This method is required by the abstract MimeDetector class. As we do not support extension mapping of byte arrays we just throw anUnsupportedOperationException
. This ensures that the getMimeTypes(...) methods ignore this method. We could also have just returned an empty collection.- Specified by:
getMimeTypesByteArray
in classMimeDetector
- Parameters:
data
- byte []. Is a byte array that you want to parse for matching mime types.- Returns:
- Collection of matched MimeType(s)
- Throws:
UnsupportedOperationException
-