check jpeg file signature in genericImageParser

to fail early without further object allocation if source is not a jpeg.
This commit is contained in:
reger 2015-10-05 01:58:31 +02:00
parent fb75fea446
commit 851e8f6c8a

View File

@ -129,6 +129,12 @@ public class genericImageParser extends AbstractParser implements Parser {
byte[] b; byte[] b;
try { try {
b = FileUtils.read(source); b = FileUtils.read(source);
// check jpeg file signature (magic number FF D8 FF)
if ((b[0] != (byte) 0xFF) // cast to signed byte (-1)
|| (b[1] != (byte) 0xD8) //cast to signed byte (-40)
|| (b[2] != (byte) 0xFF)) {
throw new Parser.Failure("File has no jpeg signature", location);
}
} catch (final IOException e) { } catch (final IOException e) {
ConcurrentLog.logException(e); ConcurrentLog.logException(e);
throw new Parser.Failure(e.getMessage(), location); throw new Parser.Failure(e.getMessage(), location);