Class MagicByteCheck


  • public class MagicByteCheck
    extends Object
    This is an utility class to to check the type of file by probing the magic bytes.
    Author:
    Claus Stümke, aiticon GmbH, 2016
    • Constructor Detail

      • MagicByteCheck

        public MagicByteCheck()
    • Method Detail

      • compareFileExtensionWithMagicBytes

        public static boolean compareFileExtensionWithMagicBytes​(File sourceFile)
        It checks the magic bytes of the file and compares the extension by magic byte match with the extension in the file name. The magic byte match is not available for text files. Because here it is not possible to distinguish .csv from .txt files. The magic byte is the same. It is also not functioning reliable for MS Office or OpenOffice files. The method is tested for image files such as jpg, png and bmp. Please test this utility for other file types first.
        Parameters:
        sourceFile - the file to check
        Returns:
        true if the extension of the file is equal with the determined extension from magic bytes.
        Throws:
        IllegalArgumentException - if there is any issue reading the file
      • getExtensionByMagicBytes

        public static String getExtensionByMagicBytes​(File file)
        Retrieves the file's extension by magic byte detection.
        Parameters:
        file - the file to check
        Throws:
        IllegalArgumentException - if there is any issue reading the file