public class MagicFile extends Object
getMimeType(InputStream)) (or a file getMimeType(File)). It uses for that a bunch of Detector, which are all configured via a configuratin file
'magic.xml', that you can find as a resource the jar as org/mmbase/config/magic.xml (you could (temporary) override
it if you need an improvement in that XML).
Several algoritms are used to determin this mime-type. It can use 'magic' bytes sequences as also is done by the unix
'file' utility. Most recognized file types are determined in that manner BasicDetector).
XML based file types can also be determined by trying the parse the file as an XML and check for certain
XML-namespaces or public ids of the DTD (see XmlDetector).
To recognize some other formats (noticebly microsoft office formats) there is now also recognition based on the
contents of zip-files. So it wil be tried to treat the stream as a zip-file and if that succeeds, it is checked
wether certain 'magic' files are present in it (see ZipDetector).| Modifier and Type | Field and Description |
|---|---|
protected static int |
BUFSIZE |
protected DetectorProvider |
detectors |
static String |
FAILED
A constant that is used as a return value for several methods in this class
to indidate that determining a mime type failed.
|
| Modifier and Type | Method and Description |
|---|---|
String |
extensionToMimeType(String extension)
Given a file's extension returns its mimetype (e.g.
|
List<Detector> |
getDetectors()
Returns a list of detectors used by this MagicFile instance
|
static MagicFile |
getInstance()
Return the current instance of MagicFile.
|
String |
getMimeType(byte[] input) |
protected String |
getMimeType(byte[] input,
InputStream in)
Tests the byte[] array for the mime type.
|
String |
getMimeType(byte[] data,
String extension) |
String |
getMimeType(File file)
Determins what kind of file the given file represents.
|
String |
getMimeType(InputStream input)
Determins what kind of file the given input stream represents.
|
static void |
main(String[] argv)
e.g.: java -Dmmbase.config=/home/mmbase/mmbase-app/WEB-INF/config org.mmbase.util.MagicFile test.doc
|
String |
mimeTypeToExtension(String mimeType)
Given a mime-type string, this function tries to create a common extension for it.
|
protected String |
test(String path) |
protected static final int BUFSIZE
public static final String FAILED
protected final DetectorProvider detectors
public static MagicFile getInstance()
public List<Detector> getDetectors()
public String getMimeType(File file) throws IOException
getMimeType(InputStream). If that fails the extension of the file name is used
via extensionToMimeType(java.lang.String) to guess the file's type.file - Location of file to be checkedIOExceptionprotected String getMimeType(byte[] input, InputStream in) throws IOException
IOExceptionpublic String getMimeType(byte[] input)
public String getMimeType(InputStream input) throws IOException
IOExceptionpublic String extensionToMimeType(String extension)
FAILED.public String mimeTypeToExtension(String mimeType)
public static void main(String[] argv)
MMBase2 Utils 2.0.0 - 2019-08-08T23:20