Class MagicNumberFileFilter
- All Implemented Interfaces:
- FileFilter,- FilenameFilter,- Serializable,- FileVisitor<Path>,- PathMatcher,- PathFilter,- PathVisitor,- IOFileFilter
 File filter for matching files containing a "magic number". A magic number
 is a unique series of bytes common to all files of a specific file format.
 For instance, all Java class files begin with the bytes
 0xCAFEBABE.
 
Using Classic IO
 File dir = FileUtils.current();
 MagicNumberFileFilter javaClassFileFilter =
     MagicNumberFileFilter(new byte[] {(byte) 0xCA, (byte) 0xFE,
       (byte) 0xBA, (byte) 0xBE});
 String[] javaClassFiles = dir.list(javaClassFileFilter);
 for (String javaClassFile : javaClassFiles) {
     System.out.println(javaClassFile);
 }
 
 Sometimes, such as in the case of TAR files, the magic number will be offset by a certain number of bytes in the file. In the case of TAR archive files, this offset is 257 bytes.
 File dir = FileUtils.current();
 MagicNumberFileFilter tarFileFilter =
     MagicNumberFileFilter("ustar", 257);
 String[] tarFiles = dir.list(tarFileFilter);
 for (String tarFile : tarFiles) {
     System.out.println(tarFile);
 }
 
 Using NIO
 final Path dir = PathUtils.current();
 final AccumulatorPathVisitor visitor = AccumulatorPathVisitor.withLongCounters(MagicNumberFileFilter("ustar", 257));
 //
 // Walk one directory
 Files.walkFileTree(dir, Collections.emptySet(), 1, visitor);
 System.out.println(visitor.getPathCounters());
 System.out.println(visitor.getFileList());
 //
 visitor.getPathCounters().reset();
 //
 // Walk directory tree
 Files.walkFileTree(dir, visitor);
 System.out.println(visitor.getPathCounters());
 System.out.println(visitor.getDirList());
 System.out.println(visitor.getFileList());
 
 Deprecating Serialization
Serialization is deprecated and will be removed in 3.0.
Deprecating Serialization
Serialization is deprecated and will be removed in 3.0.
- Since:
- 2.0
- See Also:
- 
Field SummaryFields inherited from interface org.apache.commons.io.filefilter.IOFileFilterEMPTY_STRING_ARRAY
- 
Constructor SummaryConstructorsConstructorDescriptionMagicNumberFileFilter(byte[] magicNumber) Constructs a new MagicNumberFileFilter and associates it with the magic number to test for in files.MagicNumberFileFilter(byte[] magicNumbers, 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.
- 
Method SummaryModifier and TypeMethodDescriptionbooleanAccepts the provided file if the file contains the file filter's magic number at the specified offset.accept(Path file, BasicFileAttributes attributes) Accepts the provided file if the file contains the file filter's magic number at the specified offset.toString()Returns a String representation of the file filter, which includes the magic number bytes and byte offset.Methods inherited from class org.apache.commons.io.filefilter.AbstractFileFilteraccept, handle, postVisitDirectory, preVisitDirectory, visitFile, visitFileFailedMethods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.apache.commons.io.filefilter.IOFileFilterand, matches, negate, or
- 
Constructor Details- 
MagicNumberFileFilterConstructs 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- magicNumberis- null, or contains no bytes.
 
- 
MagicNumberFileFilterConstructs 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:
- magicNumbers- the magic number to look for in the file.
- offset- the byte offset in the file to start comparing bytes.
- Throws:
- IllegalArgumentException- if- magicNumbercontains no bytes, or- offsetis a negative number.
 
- 
MagicNumberFileFilterConstructs a new MagicNumberFileFilter and associates it with the magic number to test for in files. This constructor assumes a starting offset of Example usage:0.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- magicNumberis- nullor the empty String.
 
- 
MagicNumberFileFilterConstructs 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);This method uses the virtual machine's default charset.- 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- magicNumberis the empty String, or- offsetis a negative number.
 
 
- 
- 
Method Details- 
acceptAccepts 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:
- acceptin interface- FileFilter
- Specified by:
- acceptin interface- IOFileFilter
- Overrides:
- acceptin class- AbstractFileFilter
- Parameters:
- file- the file to accept or reject.
- Returns:
- trueif the file contains the filter's magic number at the specified offset,- falseotherwise.
 
- 
acceptAccepts 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:
- acceptin interface- IOFileFilter
- Specified by:
- acceptin interface- PathFilter
- Parameters:
- file- the file to accept or reject.
- attributes- the path's basic attributes (may be null).
- Returns:
- trueif the file contains the filter's magic number at the specified offset,- falseotherwise.
- Since:
- 2.9.0
 
- 
toStringReturns a String representation of the file filter, which includes the magic number bytes and byte offset.- Overrides:
- toStringin class- AbstractFileFilter
- Returns:
- a String representation of the file filter.
 
 
-