com.j256.simplemagic
Class ContentTypeUtil

java.lang.Object
  extended by com.j256.simplemagic.ContentTypeUtil

public class ContentTypeUtil
extends Object

Class which reads in the magic files and determines the ContentType for files and byte arrays. You use the default constructor ContentTypeUtil() to use the internal rules file or load in a local file from the file-system using ContentTypeUtil(String). Once the rules are loaded, you use findMatch(String) or other such methods to get the content-type of a file or bytes.

Author:
graywatson

Nested Class Summary
static interface ContentTypeUtil.ErrorCallBack
          While we are parsing the magic configuration files, there are usually tons of badly formed lines and other errors.
 
Constructor Summary
ContentTypeUtil()
          Construct a magic utility using the internal magic file built into the package.
ContentTypeUtil(File fileOrDirectory)
          Construct a magic utility using the magic files from a file or a directory of files.
ContentTypeUtil(String fileOrDirectoryPath)
          Construct a magic utility using the magic files from a file or a directory of files.
 
Method Summary
 ContentType findMatch(byte[] bytes)
          Return the content type from the associated bytes or null if none of the magic entries matched.
 ContentType findMatch(File file)
          Return the content type for the file or null if none of the magic entries matched.
 ContentType findMatch(InputStream inputStream)
          Return the content type for the input-stream or null if none of the magic entries matched.
 ContentType findMatch(String filePath)
          Return the content type for the file or null if none of the magic entries matched.
 void setErrorCallBack(ContentTypeUtil.ErrorCallBack errorCallBack)
          Set our class which will get called whenever we get a configuration error.
 void setFileReadSize(int fileReadSize)
          Set the default size that will be read if we are getting the content from a file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ContentTypeUtil

public ContentTypeUtil()
Construct a magic utility using the internal magic file built into the package.

Throws:
IllegalStateException - If there was a problem reading the magic entries from the internal magic file.

ContentTypeUtil

public ContentTypeUtil(String fileOrDirectoryPath)
                throws IOException
Construct a magic utility using the magic files from a file or a directory of files.

Throws:
IOException - If there was a problem reading the magic entries from the internal magic file.

ContentTypeUtil

public ContentTypeUtil(File fileOrDirectory)
                throws IOException
Construct a magic utility using the magic files from a file or a directory of files.

Throws:
IOException - If there was a problem reading the magic entries from the internal magic file.
Method Detail

findMatch

public ContentType findMatch(String filePath)
                      throws IOException
Return the content type for the file or null if none of the magic entries matched.

Throws:
IOException - If there was a problem reading from the file.

findMatch

public ContentType findMatch(File file)
                      throws IOException
Return the content type for the file or null if none of the magic entries matched.

Throws:
IOException - If there was a problem reading from the file.

findMatch

public ContentType findMatch(InputStream inputStream)
                      throws IOException
Return the content type for the input-stream or null if none of the magic entries matched.

NOTE: The caller is responsible for closing the input-stream.

Throws:
IOException - If there was a problem reading from the input-stream.

findMatch

public ContentType findMatch(byte[] bytes)
Return the content type from the associated bytes or null if none of the magic entries matched.


setFileReadSize

public void setFileReadSize(int fileReadSize)
Set the default size that will be read if we are getting the content from a file. The default is most likely 10k.


setErrorCallBack

public void setErrorCallBack(ContentTypeUtil.ErrorCallBack errorCallBack)
Set our class which will get called whenever we get a configuration error.



This content is licensed by Gray Watson under the Creative Commons Attribution-Share Alike 3.0 License.