Author: tilman
Date: Sat Apr 11 03:25:54 2026
New Revision: 1932977
Log:
PDFBOX-5660: optimize, as suggested by Valery Bokov; closes #439
Modified:
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/filetypedetector/FileTypeDetector.java
Modified:
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/filetypedetector/FileTypeDetector.java
==============================================================================
---
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/filetypedetector/FileTypeDetector.java
Sat Apr 11 03:25:49 2026 (r1932976)
+++
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/filetypedetector/FileTypeDetector.java
Sat Apr 11 03:25:54 2026 (r1932977)
@@ -42,9 +42,11 @@ public final class FileTypeDetector
// https://en.wikipedia.org/wiki/List_of_file_signatures
+ byte[] iiBytes = "II".getBytes(StandardCharsets.ISO_8859_1);
+ byte[] mmBytes = "MM".getBytes(StandardCharsets.ISO_8859_1);
root.addPath(FileType.JPEG, new byte[]{(byte)0xff, (byte)0xd8});
- root.addPath(FileType.TIFF,
"II".getBytes(StandardCharsets.ISO_8859_1), new byte[]{0x2a, 0x00});
- root.addPath(FileType.TIFF,
"MM".getBytes(StandardCharsets.ISO_8859_1), new byte[]{0x00, 0x2a});
+ root.addPath(FileType.TIFF, iiBytes, new byte[]{0x2a, 0x00});
+ root.addPath(FileType.TIFF, mmBytes, new byte[]{0x00, 0x2a});
root.addPath(FileType.PSD,
"8BPS".getBytes(StandardCharsets.ISO_8859_1));
root.addPath(FileType.PNG, new byte[]{(byte)0x89, 0x50, 0x4E, 0x47,
0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52});
// TODO technically there are other very rare magic numbers for OS/2
BMP files...
@@ -61,13 +63,13 @@ public final class FileTypeDetector
// https://github.com/drewnoakes/metadata-extractor/issues/217
// root.addPath(FileType.ARW,
"II".getBytes(StandardCharsets.ISO_8859_1), new byte[]{0x2a, 0x00, 0x08, 0x00})
- root.addPath(FileType.CRW, "II".getBytes(StandardCharsets.ISO_8859_1),
new byte[]{0x1a, 0x00, 0x00, 0x00},
"HEAPCCDR".getBytes(StandardCharsets.ISO_8859_1));
- root.addPath(FileType.CR2, "II".getBytes(StandardCharsets.ISO_8859_1),
new byte[]{0x2a, 0x00, 0x10, 0x00, 0x00, 0x00, 0x43, 0x52});
- root.addPath(FileType.NEF, "MM".getBytes(StandardCharsets.ISO_8859_1),
new byte[]{0x00, 0x2a, 0x00, 0x00, 0x00, (byte)0x80, 0x00});
+ root.addPath(FileType.CRW, iiBytes, new byte[]{0x1a, 0x00, 0x00,
0x00}, "HEAPCCDR".getBytes(StandardCharsets.ISO_8859_1));
+ root.addPath(FileType.CR2, iiBytes, new byte[]{0x2a, 0x00, 0x10, 0x00,
0x00, 0x00, 0x43, 0x52});
+ root.addPath(FileType.NEF, mmBytes, new byte[]{0x00, 0x2a, 0x00, 0x00,
0x00, (byte)0x80, 0x00});
root.addPath(FileType.ORF,
"IIRO".getBytes(StandardCharsets.ISO_8859_1), new byte[]{(byte)0x08, 0x00});
root.addPath(FileType.ORF,
"IIRS".getBytes(StandardCharsets.ISO_8859_1), new byte[]{(byte)0x08, 0x00});
root.addPath(FileType.RAF,
"FUJIFILMCCD-RAW".getBytes(StandardCharsets.ISO_8859_1));
- root.addPath(FileType.RW2, "II".getBytes(StandardCharsets.ISO_8859_1),
new byte[]{0x55, 0x00});
+ root.addPath(FileType.RW2, iiBytes, new byte[]{0x55, 0x00});
}
private FileTypeDetector()