| Overview | Package | Class | Tree | Deprecated | Index | Help | |||
| PREV CLASS | NEXT CLASS | FRAMES | NO FRAMES | ||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||
java.lang.Object | +--ncsa.horizon.util.MagicNumber
This class is based on the "magic number" scheme for identifying files commonly used by UNIX operating systems. This scheme is based on the idea that files certain standardized formats will contain a sequence of bytes near the beginning of the file. For example, the first three bytes of a GIF image file will be the string "GIF"; the second three bytes will identifies the version of the GIF standard, either "87a" or "89a". This class can encapsulate these criteria and provide a way to determine if a file of a particular type or subtype.
Tests are configured at construction typically via three arrays. Each
element from each array give an offset for applying the test, a standard
(either a byte array or a String) to be compared to, and a string
describing the test. The tests should be ordered from general to specific
as applicable. If the standard is given as a String, it will be converted
to a byte array using the local file string encoding (as given by the
file.encoding system property); this is expected to be ASCII
in most cases.
Tests are carried out by passing a byte array containing the first few bytes from the file in question along with a bit-field indicating which tests to apply to the test() method. The order of the bit-field corresponds to the order of the tests set at construction. Returned is a bit-field indicating which tests passed. This bit-field can then be handed to getFullTestID() to get a string describing what the file is (much like the output of the UNIX command file).
By default, the test() method applys all the tests that were requested. Subclasses of this class, however, are encouraged to override this method "short-circuit" the tests for efficiency; that is, later tests can be skipped if earlier tests fail.
| Field Summary | |
| static int | ALL_TESTS
a bit-field specifying all the available tests |
| java.util.Vector | ids
|
| java.util.Vector | offsets
|
| java.util.Vector | tests
|
| Constructor Summary | |
| MagicNumber()
|
|
| MagicNumber(int ntests,
int defaultTests)
|
|
| MagicNumber(java.util.Vector offsets,
java.util.Vector tests,
java.util.Vector ids,
int defs)
|
|
| MagicNumber(int[] offsets,
byte[][] tests,
java.lang.String[] ids,
int defaultTestSet)
initialize a MagicNumber object with a set of tests. |
|
| MagicNumber(int[] offsets,
java.lang.String[] tests,
java.lang.String[] ids,
int defaultTestSet)
initialize a MagicNumber object with a set of tests. |
|
| MagicNumber(int offset,
byte[] test,
java.lang.String id)
initialize a MagicNumber object with a single test. |
|
| MagicNumber(int offset,
java.lang.String test,
java.lang.String id)
initialize a MagicNumber object with a single test. |
|
| Method Summary | |
| MagicNumber | cloneWithDefaults(int defaultTests)
return a new instance that is a clone of this MagicNumber with a different set of default tests |
| static boolean | compare(byte[] b,
int off,
byte[] std)
test the input byte array against another byte array |
| int | getByteCount(int t)
return the number of bytes that needed from the beginning of the file for a given set of tests. |
| int | getDefaultTests()
return the default set of tests |
| java.lang.String | getFullTestID(int t)
return a concatonation of the descriptions for given a given set of tests. |
| int | getOffset(int i)
return the offset associated with a test of a given index (beginning with 0). |
| int | getTestCount()
return the number of tests that can be applied from this MagicNumber |
| java.lang.String | getTestID(int i)
return the short description of the file type associated with the test of a given index. |
| byte[] | getTest(int i)
return the array of bytes to match for a test of a given index (beginning with 0). |
| int | testFile(java.io.File file,
int which)
test the identity of the given input file |
| int | test(byte[] b,
int fileoffset,
int t)
test the input byte array against a given set of tests |
| int | test(byte[] b,
int fileoffset)
test the input byte array against the default set of tests |
| int | testStream(java.io.InputStream strm,
int which)
test the identity of the given input stream |
| int | testURL(java.net.URL url,
int which)
test the identity of the given URL |
| Methods inherited from class java.lang.Object | |
| clone, equals, finalize, getClass, hashCode, notifyAll, notify, toString, wait, wait, wait | |
| Field Detail |
protected java.util.Vector offsets
protected java.util.Vector tests
protected java.util.Vector ids
public static final int ALL_TESTS
| Constructor Detail |
protected MagicNumber()
protected MagicNumber(int ntests,
int defaultTests)
protected MagicNumber(java.util.Vector offsets,
java.util.Vector tests,
java.util.Vector ids,
int defs)
public MagicNumber(int[] offsets,
byte[][] tests,
java.lang.String[] ids,
int defaultTestSet)
offset
- the byte offsets from the beginning of the file to
to apply each test to.
test
- the byte arrays identifying the file with increasing
specificity
id
- short descriptions of a file that passes each test
defaultTestSet
- the default set tests to apply when none are
specified to the test() method; zero means use the
default (in this case, just the first test).
public MagicNumber(int[] offsets,
java.lang.String[] tests,
java.lang.String[] ids,
int defaultTestSet)
offset
- the byte offsets from the beginning of the file to
to apply each test to.
test
- the sub-strings identifying the file with increasing
specificity; the default character encoding is used
to convert the string to bytes.
id
- short descriptions of a file that passes each test
defaultTestSet
- the default set tests to apply when none are
specified to the test() method; zero means use the
default (in this case, just the first test).
public MagicNumber(int offset,
byte[] test,
java.lang.String id)
offset
- the byte offset from the beginning of the file to
to apply the test to.
test
- a byte array identifying the file
id
- a short description of a file that passes the test
public MagicNumber(int offset,
java.lang.String test,
java.lang.String id)
offset
- the byte offset from the beginning of the file to
to apply the test to.
test
- a string identifying the file; the default character
encoding is used to convert the string to bytes.
id
- a short description of a file that passes the test| Method Detail |
public int getTestCount()
public int getOffset(int i)
public byte[] getTest(int i)
public java.lang.String getTestID(int i)
public java.lang.String getFullTestID(int t)
t
- a bit-field identifying the testspublic int getByteCount(int t)
t
- a bit-field identifying the testspublic int getDefaultTests()
public int test(byte[] b,
int fileoffset,
int t)
throws java.lang.ArrayIndexOutOfBoundsException
b
- the byte array from the file to test
fileoffset
- the offset of the first byte in b from the
beginning of the file (may be negative).
t
- the set of tests to apply given as a bit-field.
public int test(byte[] b,
int fileoffset)
throws java.lang.ArrayIndexOutOfBoundsException
b
- the byte array from the file to test
fileoffset
- the offset of the first byte in b from the
beginning of the file (may be negative).
public static boolean compare(byte[] b,
int off,
byte[] std)
b
- a byte array
off
- the offset of the first byte in b to begin the
comparison
std
- a byte array to compare with bpublic MagicNumber cloneWithDefaults(int defaultTests)
public int testStream(java.io.InputStream strm,
int which)
throws java.io.IOException
strm
- the input stream, assuming it is set to the start of
the file
which
- the tests to apply
public int testFile(java.io.File file,
int which)
throws java.io.IOException,
java.io.FileNotFoundException
file
- the input file
which
- the tests to apply
public int testURL(java.net.URL url,
int which)
throws java.io.IOException
url
- the URL of the file
which
- the tests to apply| Overview | Package | Class | Tree | Deprecated | Index | Help | |||
| PREV CLASS | NEXT CLASS | FRAMES | NO FRAMES | ||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||