/** PDF document */
PDF("%PDF"),
/** Some different HTML documents */
- HTML("<!DOCTYP".getBytes(UTF_8), "<html".getBytes(UTF_8)),
+ HTML("<!DOCTYP".getBytes(UTF_8), "<html".getBytes(UTF_8), "<HTML".getBytes(UTF_8)),
WORD2(new byte[]{ (byte)0xdb, (byte)0xa5, 0x2d, 0x00}),
// keep UNKNOWN always as last enum!
/** UNKNOWN magic */
public static FileMagic valueOf(byte[] magic) {
for (FileMagic fm : values()) {
- int i=0;
- boolean found = true;
for (byte[] ma : fm.magic) {
- for (byte m : ma) {
- byte d = magic[i++];
- if (!(d == m || (m == 0x70 && (d == 0x10 || d == 0x20 || d == 0x40)))) {
- found = false;
- break;
- }
- }
- if (found) {
+ if (findMagic(ma, magic)) {
return fm;
}
}
return UNKNOWN;
}
+ private static boolean findMagic(byte[] cmp, byte[] actual) {
+ int i=0;
+ for (byte m : cmp) {
+ byte d = actual[i++];
+ if (!(d == m || (m == 0x70 && (d == 0x10 || d == 0x20 || d == 0x40)))) {
+ return false;
+ }
+ }
+ return true;
+ }
+
/**
* Get the file magic of the supplied {@link File}<p>
package org.apache.poi.poifs.filesystem;
+import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
private static InputStream openSampleStream(String sampleFileName) {
return HSSFTestDataSamples.openSampleFileStream(sampleFileName);
}
+
+ @Test
+ public void fileMagics() {
+ for (FileMagic fm : FileMagic.values()) {
+ if (fm == FileMagic.UNKNOWN) {
+ continue;
+ }
+ for (byte[] b : fm.magic) {
+ assertEquals(fm, FileMagic.valueOf(b));
+ }
+ }
+
+ assertEquals(FileMagic.UNKNOWN, FileMagic.valueOf("foobaa".getBytes(UTF_8)));
+ }
}