| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
package com.drew.imaging; |
| 22 |
|
| 23 |
import com.drew.imaging.mp3.MpegAudioTypeChecker; |
| 24 |
import com.drew.imaging.quicktime.QuickTimeTypeChecker; |
| 25 |
import com.drew.imaging.riff.RiffTypeChecker; |
| 26 |
import com.drew.lang.ByteTrie; |
| 27 |
import com.drew.lang.annotations.NotNull; |
| 28 |
|
| 29 |
import java.io.InputStream; |
| 30 |
import java.io.IOException; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public class FileTypeDetector |
| 36 |
{ |
| 37 |
private final static ByteTrie<FileType> _root; |
| 38 |
private final static TypeChecker[] _fixedCheckers; |
| 39 |
private final static int _bytesNeeded; |
| 40 |
|
| 41 |
static |
| 42 |
{ |
| 43 |
_fixedCheckers = new TypeChecker[] { |
| 44 |
new QuickTimeTypeChecker(), |
| 45 |
new RiffTypeChecker(), |
| 46 |
new MpegAudioTypeChecker() |
| 47 |
}; |
| 48 |
|
| 49 |
_root = new ByteTrie<FileType>(); |
| 50 |
_root.setDefaultValue(FileType.Unknown); |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
_root.addPath(FileType.Jpeg, new byte[]{(byte)0xff, (byte)0xd8}); |
| 55 |
_root.addPath(FileType.Tiff, "II".getBytes(), new byte[]{0x2a, 0x00}); |
| 56 |
_root.addPath(FileType.Tiff, "MM".getBytes(), new byte[]{0x00, 0x2a}); |
| 57 |
_root.addPath(FileType.Psd, "8BPS".getBytes()); |
| 58 |
_root.addPath(FileType.Png, new byte[]{(byte)0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52}); |
| 59 |
_root.addPath(FileType.Bmp, "BM".getBytes()); |
| 60 |
_root.addPath(FileType.Bmp, "BA".getBytes()); |
| 61 |
_root.addPath(FileType.Bmp, "CI".getBytes()); |
| 62 |
_root.addPath(FileType.Bmp, "CP".getBytes()); |
| 63 |
_root.addPath(FileType.Bmp, "IC".getBytes()); |
| 64 |
_root.addPath(FileType.Bmp, "PT".getBytes()); |
| 65 |
_root.addPath(FileType.Gif, "GIF87a".getBytes()); |
| 66 |
_root.addPath(FileType.Gif, "GIF89a".getBytes()); |
| 67 |
_root.addPath(FileType.Ico, new byte[]{0x00, 0x00, 0x01, 0x00}); |
| 68 |
_root.addPath(FileType.Pcx, new byte[]{0x0A, 0x00, 0x01}); |
| 69 |
_root.addPath(FileType.Pcx, new byte[]{0x0A, 0x02, 0x01}); |
| 70 |
_root.addPath(FileType.Pcx, new byte[]{0x0A, 0x03, 0x01}); |
| 71 |
_root.addPath(FileType.Pcx, new byte[]{0x0A, 0x05, 0x01}); |
| 72 |
_root.addPath(FileType.Arw, "II".getBytes(), new byte[]{0x2a, 0x00, 0x08, 0x00}); |
| 73 |
_root.addPath(FileType.Crw, "II".getBytes(), new byte[]{0x1a, 0x00, 0x00, 0x00}, "HEAPCCDR".getBytes()); |
| 74 |
_root.addPath(FileType.Cr2, "II".getBytes(), new byte[]{0x2a, 0x00, 0x10, 0x00, 0x00, 0x00, 0x43, 0x52}); |
| 75 |
|
| 76 |
|
| 77 |
_root.addPath(FileType.Orf, "IIRO".getBytes(), new byte[]{(byte)0x08, 0x00}); |
| 78 |
_root.addPath(FileType.Orf, "MMOR".getBytes(), new byte[]{(byte)0x00, 0x00}); |
| 79 |
_root.addPath(FileType.Orf, "IIRS".getBytes(), new byte[]{(byte)0x08, 0x00}); |
| 80 |
_root.addPath(FileType.Raf, "FUJIFILMCCD-RAW".getBytes()); |
| 81 |
_root.addPath(FileType.Rw2, "II".getBytes(), new byte[]{0x55, 0x00}); |
| 82 |
_root.addPath(FileType.Eps, "%!PS".getBytes()); |
| 83 |
_root.addPath(FileType.Eps, new byte[]{(byte)0xC5, (byte)0xD0, (byte)0xD3, (byte)0xC6}); |
| 84 |
|
| 85 |
|
| 86 |
_root.addPath(FileType.Aac, new byte[]{(byte)0xFF, (byte)0xF1}); |
| 87 |
_root.addPath(FileType.Aac, new byte[]{(byte)0xFF, (byte)0xF9}); |
| 88 |
_root.addPath(FileType.Asf, new byte[]{0x30, 0x26, (byte)0xB2, 0x75, (byte)0x8E, 0x66, (byte)0xCF, 0x11, (byte)0xA6, (byte)0xD9, 0x00, (byte)0xAA, 0x00, 0x62, (byte)0xCE, 0x6C}); |
| 89 |
_root.addPath(FileType.Cfbf, new byte[]{(byte)0xD0, (byte)0xCF, 0x11, (byte)0xE0, (byte)0xA1, (byte)0xB1, 0x1A, (byte)0xE1, 0x00}); |
| 90 |
_root.addPath(FileType.Flv, new byte[]{0x46, 0x4C, 0x56}); |
| 91 |
_root.addPath(FileType.Indd, new byte[]{0x06, 0x06, (byte)0xED, (byte)0xF5, (byte)0xD8, 0x1D, 0x46, (byte)0xE5, (byte)0xBD, 0x31, (byte)0xEF, (byte)0xE7, (byte)0xFE, 0x74, (byte)0xB7, 0x1D}); |
| 92 |
_root.addPath(FileType.Mxf, new byte[]{0x06, 0x0e, 0x2b, 0x34, 0x02, 0x05, 0x01, 0x01, 0x0d, 0x01, 0x02, 0x01, 0x01, 0x02}); |
| 93 |
_root.addPath(FileType.Qxp, new byte[]{0x00, 0x00, 0x49, 0x49, 0x58, 0x50, 0x52, 0x33}); |
| 94 |
_root.addPath(FileType.Qxp, new byte[]{0x00, 0x00, 0x4D, 0x4D, 0x58, 0x50, 0x52, 0x33}); |
| 95 |
_root.addPath(FileType.Ram, new byte[]{0x72, 0x74, 0x73, 0x70, 0x3A, 0x2F, 0x2F}); |
| 96 |
_root.addPath(FileType.Rtf, new byte[]{0x7B, 0x5C, 0x72, 0x74, 0x66, 0x31}); |
| 97 |
_root.addPath(FileType.Sit, new byte[]{ 0x53, 0x49, 0x54, 0x21, 0x00 }); |
| 98 |
_root.addPath(FileType.Sit, new byte[]{ 0x53, 0x74, 0x75, 0x66, 0x66, 0x49, 0x74, 0x20, 0x28, 0x63, 0x29, 0x31, 0x39, 0x39, 0x37, 0x2D}); |
| 99 |
_root.addPath(FileType.Sitx, new byte[]{ 0x53, 0x74, 0x75, 0x66, 0x66, 0x49, 0x74, 0x21 }); |
| 100 |
_root.addPath(FileType.Swf, "CWS".getBytes()); |
| 101 |
_root.addPath(FileType.Swf, "FWS".getBytes()); |
| 102 |
_root.addPath(FileType.Swf, "ZWS".getBytes()); |
| 103 |
_root.addPath(FileType.Vob, new byte[]{0x00, 0x00, 0x01, (byte)0xBA}); |
| 104 |
_root.addPath(FileType.Zip, "PK".getBytes()); |
| 105 |
|
| 106 |
int bytesNeeded = _root.getMaxDepth(); |
| 107 |
for (TypeChecker fixedChecker : _fixedCheckers) { |
| 108 |
if (fixedChecker.getByteCount() > bytesNeeded) |
| 109 |
bytesNeeded = fixedChecker.getByteCount(); |
| 110 |
} |
| 111 |
_bytesNeeded = bytesNeeded; |
| 112 |
} |
| 113 |
|
| 114 |
private FileTypeDetector() throws Exception |
| 115 |
{ |
| 116 |
throw new Exception("Not intended for instantiation"); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
@NotNull |
| 131 |
public static FileType detectFileType(@NotNull final InputStream inputStream) throws IOException |
| 132 |
{ |
| 133 |
if (!inputStream.markSupported()) |
| 134 |
throw new IOException("Stream must support mark/reset"); |
| 135 |
|
| 136 |
inputStream.mark(_bytesNeeded); |
| 137 |
|
| 138 |
byte[] bytes = new byte[_bytesNeeded]; |
| 139 |
int offset = 0; |
| 140 |
int count = _bytesNeeded; |
| 141 |
while (count != 0) { |
| 142 |
int bytesRead = inputStream.read(bytes, offset, count); |
| 143 |
if (bytesRead == -1) |
| 144 |
break; |
| 145 |
count -= bytesRead; |
| 146 |
offset += bytesRead; |
| 147 |
} |
| 148 |
|
| 149 |
inputStream.reset(); |
| 150 |
|
| 151 |
FileType fileType = _root.find(bytes, 0, offset); |
| 152 |
|
| 153 |
assert(fileType != null); |
| 154 |
|
| 155 |
if (fileType == FileType.Unknown) { |
| 156 |
for (TypeChecker checker : _fixedCheckers) { |
| 157 |
fileType = checker.checkType(bytes); |
| 158 |
if (fileType != FileType.Unknown) |
| 159 |
return fileType; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
return fileType; |
| 164 |
} |
| 165 |
} |