Class MagicNumberFileFilter

    • Constructor Summary

      Constructors 
      Constructor Description
      MagicNumberFileFilter​(byte[] magicNumber)
      Constructs a new MagicNumberFileFilter and associates it with the magic number to test for in files.
      MagicNumberFileFilter​(byte[] magicNumber, long offset)
      Constructs a new MagicNumberFileFilter and associates it with the magic number to test for in files and the byte offset location in the file to to look for that magic number.
      MagicNumberFileFilter​(String magicNumber)
      Constructs a new MagicNumberFileFilter and associates it with the magic number to test for in files.
      MagicNumberFileFilter​(String magicNumber, long offset)
      Constructs a new MagicNumberFileFilter and associates it with the magic number to test for in files and the byte offset location in the file to to look for that magic number.
    • Constructor Detail

      • MagicNumberFileFilter

        public MagicNumberFileFilter​(byte[] magicNumber)

        Constructs a new MagicNumberFileFilter and associates it with the magic number to test for in files. This constructor assumes a starting offset of 0.

        It is important to note that the array is not cloned and that any changes to the magic number array after construction will affect the behavior of this file filter.

         MagicNumberFileFilter javaClassFileFilter =
             MagicNumberFileFilter(new byte[] {(byte) 0xCA, (byte) 0xFE,
               (byte) 0xBA, (byte) 0xBE});
         
        Parameters:
        magicNumber - the magic number to look for in the file.
        Throws:
        IllegalArgumentException - if magicNumber is null, or contains no bytes.
      • MagicNumberFileFilter

        public MagicNumberFileFilter​(String magicNumber)

        Constructs a new MagicNumberFileFilter and associates it with the magic number to test for in files. This constructor assumes a starting offset of 0.

        Example usage:
         
         MagicNumberFileFilter xmlFileFilter =
             MagicNumberFileFilter("<?xml");
         
         
        Parameters:
        magicNumber - the magic number to look for in the file. The string is converted to bytes using the platform default charset.
        Throws:
        IllegalArgumentException - if magicNumber is null or the empty String.
      • MagicNumberFileFilter

        public MagicNumberFileFilter​(String magicNumber,
                                     long offset)

        Constructs a new MagicNumberFileFilter and associates it with the magic number to test for in files and the byte offset location in the file to to look for that magic number.

         MagicNumberFileFilter tarFileFilter =
             MagicNumberFileFilter("ustar", 257);
         
        Parameters:
        magicNumber - the magic number to look for in the file. The string is converted to bytes using the platform default charset.
        offset - the byte offset in the file to start comparing bytes.
        Throws:
        IllegalArgumentException - if magicNumber is null or the empty String, or offset is a negative number.
      • MagicNumberFileFilter

        public MagicNumberFileFilter​(byte[] magicNumber,
                                     long offset)

        Constructs a new MagicNumberFileFilter and associates it with the magic number to test for in files and the byte offset location in the file to to look for that magic number.

         MagicNumberFileFilter tarFileFilter =
             MagicNumberFileFilter(new byte[] {0x75, 0x73, 0x74, 0x61, 0x72}, 257);
         
         MagicNumberFileFilter javaClassFileFilter =
             MagicNumberFileFilter(new byte[] {0xCA, 0xFE, 0xBA, 0xBE}, 0);
         
        Parameters:
        magicNumber - the magic number to look for in the file.
        offset - the byte offset in the file to start comparing bytes.
        Throws:
        IllegalArgumentException - if magicNumber is null, or contains no bytes, or offset is a negative number.
    • Method Detail

      • accept

        public boolean accept​(File file)

        Accepts the provided file if the file contains the file filter's magic number at the specified offset.

        If any IOExceptions occur while reading the file, the file will be rejected.

        Specified by:
        accept in interface FileFilter
        Specified by:
        accept in interface IOFileFilter
        Overrides:
        accept in class AbstractFileFilter
        Parameters:
        file - the file to accept or reject.
        Returns:
        true if the file contains the filter's magic number at the specified offset, false otherwise.
      • toString

        public String toString()
        Returns a String representation of the file filter, which includes the magic number bytes and byte offset.
        Overrides:
        toString in class AbstractFileFilter
        Returns:
        a String representation of the file filter.