Author: tilman Date: Tue Jan 5 17:16:43 2016 New Revision: 1723132 URL: http://svn.apache.org/viewvc?rev=1723132&view=rev Log: PDFBOX-3163: SonarQube fixes
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 URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/filetypedetector/FileTypeDetector.java?rev=1723132&r1=1723131&r2=1723132&view=diff ============================================================================== --- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/filetypedetector/FileTypeDetector.java (original) +++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/filetypedetector/FileTypeDetector.java Tue Jan 5 17:16:43 2016 @@ -19,6 +19,8 @@ package org.apache.pdfbox.util.filetyped import java.io.BufferedInputStream; import java.io.IOException; +import org.apache.pdfbox.util.Charsets; + /** * @author Drew Noakes * @@ -30,9 +32,9 @@ import java.io.IOException; * * Examines the a file's first bytes and estimates the file's type. */ -public class FileTypeDetector +public final class FileTypeDetector { - private final static ByteTrie<FileType> root; + private static final ByteTrie<FileType> root; static { @@ -42,35 +44,34 @@ public class FileTypeDetector // https://en.wikipedia.org/wiki/List_of_file_signatures root.addPath(FileType.JPEG, new byte[]{(byte)0xff, (byte)0xd8}); - root.addPath(FileType.TIFF, "II".getBytes(), new byte[]{0x2a, 0x00}); - root.addPath(FileType.TIFF, "MM".getBytes(), new byte[]{0x00, 0x2a}); - root.addPath(FileType.PSD, "8BPS".getBytes()); + root.addPath(FileType.TIFF, "II".getBytes(Charsets.ISO_8859_1), new byte[]{0x2a, 0x00}); + root.addPath(FileType.TIFF, "MM".getBytes(Charsets.ISO_8859_1), new byte[]{0x00, 0x2a}); + root.addPath(FileType.PSD, "8BPS".getBytes(Charsets.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... - root.addPath(FileType.BMP, "BM".getBytes()); - root.addPath(FileType.GIF, "GIF87a".getBytes()); - root.addPath(FileType.GIF, "GIF89a".getBytes()); + root.addPath(FileType.BMP, "BM".getBytes(Charsets.ISO_8859_1)); + root.addPath(FileType.GIF, "GIF87a".getBytes(Charsets.ISO_8859_1)); + root.addPath(FileType.GIF, "GIF89a".getBytes(Charsets.ISO_8859_1)); root.addPath(FileType.ICO, new byte[]{0x00, 0x00, 0x01, 0x00}); // multiple PCX versions, explicitly listed root.addPath(FileType.PCX, new byte[]{0x0A, 0x00, 0x01}); root.addPath(FileType.PCX, new byte[]{0x0A, 0x02, 0x01}); root.addPath(FileType.PCX, new byte[]{0x0A, 0x03, 0x01}); root.addPath(FileType.PCX, new byte[]{0x0A, 0x05, 0x01}); - root.addPath(FileType.RIFF, "RIFF".getBytes()); + root.addPath(FileType.RIFF, "RIFF".getBytes(Charsets.ISO_8859_1)); - root.addPath(FileType.ARW, "II".getBytes(), new byte[]{0x2a, 0x00, 0x08, 0x00}); - root.addPath(FileType.CRW, "II".getBytes(), new byte[]{0x1a, 0x00, 0x00, 0x00}, "HEAPCCDR".getBytes()); - root.addPath(FileType.CR2, "II".getBytes(), new byte[]{0x2a, 0x00, 0x10, 0x00, 0x00, 0x00, 0x43, 0x52}); - root.addPath(FileType.NEF, "MM".getBytes(), new byte[]{0x00, 0x2a, 0x00, 0x00, 0x00, (byte)0x80, 0x00}); - root.addPath(FileType.ORF, "IIRO".getBytes(), new byte[]{(byte)0x08, 0x00}); - root.addPath(FileType.ORF, "IIRS".getBytes(), new byte[]{(byte)0x08, 0x00}); - root.addPath(FileType.RAF, "FUJIFILMCCD-RAW".getBytes()); - root.addPath(FileType.RW2, "II".getBytes(), new byte[]{0x55, 0x00}); + root.addPath(FileType.ARW, "II".getBytes(Charsets.ISO_8859_1), new byte[]{0x2a, 0x00, 0x08, 0x00}); + root.addPath(FileType.CRW, "II".getBytes(Charsets.ISO_8859_1), new byte[]{0x1a, 0x00, 0x00, 0x00}, "HEAPCCDR".getBytes(Charsets.ISO_8859_1)); + root.addPath(FileType.CR2, "II".getBytes(Charsets.ISO_8859_1), new byte[]{0x2a, 0x00, 0x10, 0x00, 0x00, 0x00, 0x43, 0x52}); + root.addPath(FileType.NEF, "MM".getBytes(Charsets.ISO_8859_1), new byte[]{0x00, 0x2a, 0x00, 0x00, 0x00, (byte)0x80, 0x00}); + root.addPath(FileType.ORF, "IIRO".getBytes(Charsets.ISO_8859_1), new byte[]{(byte)0x08, 0x00}); + root.addPath(FileType.ORF, "IIRS".getBytes(Charsets.ISO_8859_1), new byte[]{(byte)0x08, 0x00}); + root.addPath(FileType.RAF, "FUJIFILMCCD-RAW".getBytes(Charsets.ISO_8859_1)); + root.addPath(FileType.RW2, "II".getBytes(Charsets.ISO_8859_1), new byte[]{0x55, 0x00}); } private FileTypeDetector() throws Exception { - throw new Exception("Not intended for instantiation"); } /** @@ -81,6 +82,8 @@ public class FileTypeDetector * <p> * Requires the stream to contain at least eight bytes. * + * @param inputStream a buffered input stream of the file to examine. + * @return the file type. * @throws IOException if an IO error occurred or the input stream ended unexpectedly. */ public static FileType detectFileType(final BufferedInputStream inputStream) throws IOException