[Top] | [Contents] | [Index] | [ ? ] |
Version 0.5 – May 2013
This package provides some simple magic value features that simulate the Unix file(1) command to determine the type of a file
or of bytes
from the content. It has an internal set of magic number information or it can process the magic files from
local ~Unix system configuration
To get started quickly using SimpleMagic, see section Start Using Quickly. There is also a PDF version of this documentation.
Gray Watson http://256.com/gray/
1. Start Using Quickly | Start using SimpleMagic quickly. | |
2. Using SimpleMagic | How to use SimpleMagic. | |
3. Open Source License | Open Source license for the project. | |
Index of Concepts | Index of concepts in the manual. |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
To use SimpleMagic you need to do the following steps. For more information, see section Using SimpleMagic.
ContentTypeUtil
instance
with the default constructor, it will load the internal magic entries file. See section How To Load Magic Entries.
ContentTypeUtil
class to get content-types for files or byte[]
:
ContentTypeUtil util = new ContentTypeUtil(); ContentType contentType = util.findMatch("/tmp/upload.tmp"); // or ContentType contentType = util.findMatch(inputStream); // or ContentType contentType = util.findMatch(contentByteArray); System.out.println("Content-type is: " + contentType.getName()); |
For somewhat more extensive instructions, see section Using SimpleMagic.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
2.1 Downloading Jar | Downloading the SimpleMagic jar. | |
2.2 How To Load Magic Entries | Loading in the magic entries from files. |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
To get started with SimpleMagic, you will need to download the jar file. The SimpleMagic release page is the default repository but the jars are also available from the central maven repository.
The code works with Java 5 or later.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The library uses various magic byte information to be able to find and determine details about random blocks of bytes. By default, SimpleMagic has a built in version of a magic file that was copied from a CentOS Linux system. It contains, ~2400 magic file entries describing a number of different file types. It also has an additional ~6600 lines which provide more details about the detected content types.
The magic entries are relatively complex but in general look something like the following. The configuration line says to
look at the start of the file for the string "GIF8"
. If it is there then the file is "GIF image data".
0 string GIF8 GIF image data |
If you do not want to use the internal magic definitions, you can also construct the ContentTypeUtil
class with a file
or directory to have it parse and use another definition file.
ContentTypeUtil util = new ContentTypeUtil("/etc/magic"); ContentType contentType = util.findMatch(uploadedBytes); |
WARNING: although we’ve tried to support different types of magic entries, there are local per-OS variations that may not be supported.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This document is part of the SimpleMagic project.
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
The author may be contacted via http://256.com/sources/simplemagic/
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Jump to: | /
A C D G H I L M O Q S U W |
---|
Jump to: | /
A C D G H I L M O Q S U W |
---|
[Top] | [Contents] | [Index] | [ ? ] |
[Top] | [Contents] | [Index] | [ ? ] |
This document was generated by Gray Watson on May 13, 2013 using texi2html 1.82.
The buttons in the navigation panels have the following meaning:
Button | Name | Go to | From 1.2.3 go to |
---|---|---|---|
[ < ] | Back | Previous section in reading order | 1.2.2 |
[ > ] | Forward | Next section in reading order | 1.2.4 |
[ << ] | FastBack | Beginning of this chapter or previous chapter | 1 |
[ Up ] | Up | Up section | 1.2 |
[ >> ] | FastForward | Next chapter | 2 |
[Top] | Top | Cover (top) of document | |
[Contents] | Contents | Table of contents | |
[Index] | Index | Index | |
[ ? ] | About | About (help) |
where the Example assumes that the current position is at Subsubsection One-Two-Three of a document of the following structure:
This document was generated by Gray Watson on May 13, 2013 using texi2html 1.82.