com.j256.simplemagic
Class ContentInfoUtil

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

public class ContentInfoUtil
extends Object

Class which reads in the magic files and determines the ContentInfo for files and byte arrays. You use the default constructor ContentInfoUtil() to use the internal rules file or load in a local file from the file-system using ContentInfoUtil(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 ContentInfoUtil.ErrorCallBack
          While we are parsing the magic configuration files, there are usually tons of badly formed lines and other errors.
 
Constructor Summary
ContentInfoUtil()
          Construct a magic utility using the internal magic file built into the package.
ContentInfoUtil(ContentInfoUtil.ErrorCallBack errorCallBack)
          Construct a magic utility using the internal magic file built into the package.
ContentInfoUtil(File fileOrDirectory)
          Construct a magic utility using the magic files from a file or a directory of files.
ContentInfoUtil(File fileOrDirectory, ContentInfoUtil.ErrorCallBack errorCallBack)
          Construct a magic utility using the magic files from a file or a directory of files.
ContentInfoUtil(String fileOrDirectoryPath)
          Construct a magic utility using the magic files from a file or a directory of files.
ContentInfoUtil(String fileOrDirectoryPath, ContentInfoUtil.ErrorCallBack errorCallBack)
          Construct a magic utility using the magic files from a file or a directory of files.
 
Method Summary
 ContentInfo findMatch(byte[] bytes)
          Return the content type from the associated bytes or null if none of the magic entries matched.
 ContentInfo findMatch(File file)
          Return the content type for the file or null if none of the magic entries matched.
 ContentInfo findMatch(InputStream inputStream)
          Return the content type for the input-stream or null if none of the magic entries matched.
 ContentInfo findMatch(String filePath)
          Return the content type for the file or null if none of the magic entries matched.
 void setErrorCallBack(ContentInfoUtil.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

ContentInfoUtil

public ContentInfoUtil()
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.

ContentInfoUtil

public ContentInfoUtil(ContentInfoUtil.ErrorCallBack errorCallBack)
Construct a magic utility using the internal magic file built into the package. This also allows the caller to log any errors discovered in the file(s).

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

ContentInfoUtil

public ContentInfoUtil(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.

ContentInfoUtil

public ContentInfoUtil(String fileOrDirectoryPath,
                       ContentInfoUtil.ErrorCallBack errorCallBack)
                throws IOException
Construct a magic utility using the magic files from a file or a directory of files. This also allows the caller to log any errors discovered in the file(s).

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

ContentInfoUtil

public ContentInfoUtil(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.

ContentInfoUtil

public ContentInfoUtil(File fileOrDirectory,
                       ContentInfoUtil.ErrorCallBack errorCallBack)
                throws IOException
Construct a magic utility using the magic files from a file or a directory of files. This also allows the caller to log any errors discovered in the file(s).

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

findMatch

public ContentInfo 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 ContentInfo 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 ContentInfo 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 ContentInfo 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(ContentInfoUtil.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.