[Top] [Contents] [Index] [ ? ]

SimpleMagic

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/


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1. Start Using Quickly

To use SimpleMagic you need to do the following steps. For more information, see section Using SimpleMagic.

  1. Download SimpleMagic from the SimpleMagic release page. See section Downloading Jar.
  2. Optionally load in the magic entries from local file(s). By default, if you construct a ContentTypeUtil instance with the default constructor, it will load the internal magic entries file. See section How To Load Magic Entries.
  3. Use the 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. Using SimpleMagic


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.1 Downloading Jar

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] [ ? ]

2.2 How To Load Magic Entries

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] [ ? ]

3. Open Source License

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] [ ? ]

Index of Concepts

Jump to:   /  
A   C   D   G   H   I   L   M   O   Q   S   U   W  
Index Entry Section

/
/etc/magic2.2 How To Load Magic Entries

A
alternative magic files2.2 How To Load Magic Entries
authorSimpleMagic

C
ContentTypeUtil1. Start Using Quickly

D
default magic entries2.2 How To Load Magic Entries
downloading the jars2.1 Downloading Jar

G
getting started1. Start Using Quickly

H
how to download the jars2.1 Downloading Jar
how to get started1. Start Using Quickly
how to use2. Using SimpleMagic

I
introductionSimpleMagic

L
license3. Open Source License
loading magic entries2.2 How To Load Magic Entries

M
magic files2.2 How To Load Magic Entries

O
open source license3. Open Source License

Q
quick start1. Start Using Quickly

S
sample magic definition2.2 How To Load Magic Entries
simple magicSimpleMagic
system magic entries2.2 How To Load Magic Entries

U
using SimpleMagic2. Using SimpleMagic

W
where to get new jars2.1 Downloading Jar

Jump to:   /  
A   C   D   G   H   I   L   M   O   Q   S   U   W  

[Top] [Contents] [Index] [ ? ]

Table of Contents


[Top] [Contents] [Index] [ ? ]

About This Document

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.