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 can use findMatch(String)
or other findMatch(...)
methods to get the content-type of a file or bytes.
// create a magic utility using the internal magic file ContentInfoUtil util = new ContentInfoUtil(); // get the content info for this file-path or null if no match ContentInfo info = util.findMatch("/tmp/upload.tmp"); // display content type information if (info == null) { System.out.println("Unknown content-type"); } else { // other information in ContentInfo type System.out.println("Content-type is: " + info.getName()); }
Modifier and Type | Class and Description |
---|---|
static interface |
ContentInfoUtil.ErrorCallBack
Optional call-back which will be made whenever we discover an error while parsing the magic configuration files.
|
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_READ_SIZE
Number of bytes that the utility class by default reads to determine the content type information.
|
Constructor and Description |
---|
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(Reader reader)
Construct a magic utility using the magic file entries from a reader.
|
ContentInfoUtil(Reader reader,
ContentInfoUtil.ErrorCallBack errorCallBack)
Construct a magic utility using the magic file entries from a reader.
|
ContentInfoUtil(String fileOrDirectoryPath)
Construct a magic utility using the magic files from a file or a directory of files.
|
ContentInfoUtil(String fileOrDirectoryOrResourcePath,
ContentInfoUtil.ErrorCallBack errorCallBack)
Construct a magic utility using the magic files from a file or a directory of files.
|
Modifier and Type | Method and Description |
---|---|
static ContentInfo |
findExtensionMatch(String name)
Return the content type if the extension from the file-name matches our internal list.
|
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-path or null if none of the magic entries matched.
|
static ContentInfo |
findMimeTypeMatch(String mimeType)
Return the content type if the mime-type matches our internal list.
|
void |
setErrorCallBack(ContentInfoUtil.ErrorCallBack errorCallBack)
Deprecated.
Not used since it is only passed into the constructor.
|
void |
setFileReadSize(int fileReadSize)
Set the default size that will be read if we are getting the content from a file.
|
public static final int DEFAULT_READ_SIZE
public ContentInfoUtil()
IllegalStateException
- If there was a problem reading the magic entries from the internal magic file.public ContentInfoUtil(ContentInfoUtil.ErrorCallBack errorCallBack)
errorCallBack
- Call back which shows any problems with the magic entries loaded.IllegalStateException
- If there was a problem reading the magic entries from the internal magic file.public ContentInfoUtil(String fileOrDirectoryPath) throws IOException
fileOrDirectoryPath
- A path which can be a magic file, or a directory of magic files, or a magic file in a resource path.IOException
- If there was a problem reading the magic entries from the internal magic file.public ContentInfoUtil(String fileOrDirectoryOrResourcePath, ContentInfoUtil.ErrorCallBack errorCallBack) throws IOException
fileOrDirectoryOrResourcePath
- A path which can be a magic file, or a directory of magic files, or a magic file in a resource path.errorCallBack
- Call back which shows any problems with the magic entries loaded.IOException
- If there was a problem reading the magic entries from the internal magic file.public ContentInfoUtil(File fileOrDirectory) throws IOException
fileOrDirectory
- A path which can be a magic file, or a directory of magic files.IOException
- If there was a problem reading the magic entries from the internal magic file.public ContentInfoUtil(File fileOrDirectory, ContentInfoUtil.ErrorCallBack errorCallBack) throws IOException
fileOrDirectory
- A path which can be a magic file, or a directory of magic files.errorCallBack
- Call back which shows any problems with the magic entries loaded.IOException
- If there was a problem reading the magic entries from the internal magic file.public ContentInfoUtil(Reader reader) throws IOException
reader
- A reader from which we will read the magic file entries.IOException
- If there was a problem reading the magic entries from the reader.public ContentInfoUtil(Reader reader, ContentInfoUtil.ErrorCallBack errorCallBack) throws IOException
reader
- A reader from which we will read the magic file entries.errorCallBack
- Call back which shows any problems with the magic entries loaded.IOException
- If there was a problem reading the magic entries from the reader.public ContentInfo findMatch(String filePath) throws IOException
IOException
- If there was a problem reading from the file.public ContentInfo findMatch(File file) throws IOException
IOException
- If there was a problem reading from the file.public ContentInfo findMatch(InputStream inputStream) throws IOException
ContentInfoInputStreamWrapper
class to delegate to an input-stream and determine content information
at the same time.
NOTE: The caller is responsible for closing the input-stream.
IOException
- If there was a problem reading from the input-stream.ContentInfoInputStreamWrapper
public ContentInfo findMatch(byte[] bytes)
public static ContentInfo findExtensionMatch(String name)
public static ContentInfo findMimeTypeMatch(String mimeType)
public void setFileReadSize(int fileReadSize)
DEFAULT_READ_SIZE
@Deprecated public void setErrorCallBack(ContentInfoUtil.ErrorCallBack errorCallBack)
This content is licensed by Gray Watson under the Creative Commons Attribution-Share Alike 3.0 License.