Coverage Report - org.apache.commons.io.filefilter.MagicNumberFileFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
MagicNumberFileFilter
95%
43/45
90%
18/20
4.167
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.commons.io.filefilter;
 18  
 
 19  
 import java.io.File;
 20  
 import java.io.IOException;
 21  
 import java.io.RandomAccessFile;
 22  
 import java.io.Serializable;
 23  
 import java.nio.charset.Charset;
 24  
 import java.util.Arrays;
 25  
 
 26  
 import org.apache.commons.io.IOUtils;
 27  
 
 28  
 /**
 29  
  * <p>
 30  
  * File filter for matching files containing a "magic number". A magic number
 31  
  * is a unique series of bytes common to all files of a specific file format.
 32  
  * For instance, all Java class files begin with the bytes
 33  
  * <code>0xCAFEBABE</code>.
 34  
  * </p>
 35  
  *
 36  
  * <pre>
 37  
  * File dir = new File(".");
 38  
  * MagicNumberFileFilter javaClassFileFilter =
 39  
  *     MagicNumberFileFilter(new byte[] {(byte) 0xCA, (byte) 0xFE,
 40  
  *       (byte) 0xBA, (byte) 0xBE});
 41  
  * String[] javaClassFiles = dir.list(javaClassFileFilter);
 42  
  * for (String javaClassFile : javaClassFiles) {
 43  
  *     System.out.println(javaClassFile);
 44  
  * }
 45  
  * </pre>
 46  
  *
 47  
  * <p>
 48  
  * Sometimes, such as in the case of TAR files, the
 49  
  * magic number will be offset by a certain number of bytes in the file. In the
 50  
  * case of TAR archive files, this offset is 257 bytes.
 51  
  * </p>
 52  
  *
 53  
  * <pre>
 54  
  * File dir = new File(".");
 55  
  * MagicNumberFileFilter tarFileFilter =
 56  
  *     MagicNumberFileFilter("ustar", 257);
 57  
  * String[] tarFiles = dir.list(tarFileFilter);
 58  
  * for (String tarFile : tarFiles) {
 59  
  *     System.out.println(tarFile);
 60  
  * }
 61  
  * </pre>
 62  
  *
 63  
  * @since 2.0
 64  
  * @see FileFilterUtils#magicNumberFileFilter(byte[])
 65  
  * @see FileFilterUtils#magicNumberFileFilter(String)
 66  
  * @see FileFilterUtils#magicNumberFileFilter(byte[], long)
 67  
  * @see FileFilterUtils#magicNumberFileFilter(String, long)
 68  
  */
 69  
 public class MagicNumberFileFilter extends AbstractFileFilter implements
 70  
         Serializable {
 71  
 
 72  
     /**
 73  
      * The serialization version unique identifier.
 74  
      */
 75  
     private static final long serialVersionUID = -547733176983104172L;
 76  
 
 77  
     /**
 78  
      * The magic number to compare against the file's bytes at the provided
 79  
      * offset.
 80  
      */
 81  
     private final byte[] magicNumbers;
 82  
 
 83  
     /**
 84  
      * The offset (in bytes) within the files that the magic number's bytes
 85  
      * should appear.
 86  
      */
 87  
     private final long byteOffset;
 88  
 
 89  
     /**
 90  
      * <p>
 91  
      * Constructs a new MagicNumberFileFilter and associates it with the magic
 92  
      * number to test for in files. This constructor assumes a starting offset
 93  
      * of <code>0</code>.
 94  
      * </p>
 95  
      *
 96  
      * <p>
 97  
      * It is important to note that <em>the array is not cloned</em> and that
 98  
      * any changes to the magic number array after construction will affect the
 99  
      * behavior of this file filter.
 100  
      * </p>
 101  
      *
 102  
      * <pre>
 103  
      * MagicNumberFileFilter javaClassFileFilter =
 104  
      *     MagicNumberFileFilter(new byte[] {(byte) 0xCA, (byte) 0xFE,
 105  
      *       (byte) 0xBA, (byte) 0xBE});
 106  
      * </pre>
 107  
      *
 108  
      * @param magicNumber the magic number to look for in the file.
 109  
      *
 110  
      * @throws IllegalArgumentException if <code>magicNumber</code> is
 111  
      *         {@code null}, or contains no bytes.
 112  
      */
 113  
     public MagicNumberFileFilter(final byte[] magicNumber) {
 114  4
         this(magicNumber, 0);
 115  4
     }
 116  
 
 117  
     /**
 118  
      * <p>
 119  
      * Constructs a new MagicNumberFileFilter and associates it with the magic
 120  
      * number to test for in files. This constructor assumes a starting offset
 121  
      * of <code>0</code>.
 122  
      * </p>
 123  
      *
 124  
      * Example usage:
 125  
      * <pre>
 126  
      * {@code
 127  
      * MagicNumberFileFilter xmlFileFilter =
 128  
      *     MagicNumberFileFilter("<?xml");
 129  
      * }
 130  
      * </pre>
 131  
      *
 132  
      * @param magicNumber the magic number to look for in the file.
 133  
      *        The string is converted to bytes using the platform default charset.
 134  
      *
 135  
      * @throws IllegalArgumentException if <code>magicNumber</code> is
 136  
      *         {@code null} or the empty String.
 137  
      */
 138  
     public MagicNumberFileFilter(final String magicNumber) {
 139  4
         this(magicNumber, 0);
 140  4
     }
 141  
 
 142  
     /**
 143  
      * <p>
 144  
      * Constructs a new MagicNumberFileFilter and associates it with the magic
 145  
      * number to test for in files and the byte offset location in the file to
 146  
      * to look for that magic number.
 147  
      * </p>
 148  
      *
 149  
      * <pre>
 150  
      * MagicNumberFileFilter tarFileFilter =
 151  
      *     MagicNumberFileFilter("ustar", 257);
 152  
      * </pre>
 153  
      *
 154  
      * @param magicNumber the magic number to look for in the file.
 155  
      *        The string is converted to bytes using the platform default charset.
 156  
      * @param offset the byte offset in the file to start comparing bytes.
 157  
      *
 158  
      * @throws IllegalArgumentException if <code>magicNumber</code> is
 159  
      *         {@code null} or the empty String, or <code>offset</code> is
 160  
      *         a negative number.
 161  
      */
 162  14
     public MagicNumberFileFilter(final String magicNumber, final long offset) {
 163  14
         if (magicNumber == null) {
 164  2
             throw new IllegalArgumentException("The magic number cannot be null");
 165  
         }
 166  12
         if (magicNumber.length() == 0) {
 167  2
             throw new IllegalArgumentException("The magic number must contain at least one byte");
 168  
         }
 169  10
         if (offset < 0) {
 170  2
             throw new IllegalArgumentException("The offset cannot be negative");
 171  
         }
 172  
 
 173  8
         this.magicNumbers = magicNumber.getBytes(Charset.defaultCharset()); // explicitly uses the platform default charset
 174  8
         this.byteOffset = offset;
 175  8
     }
 176  
 
 177  
     /**
 178  
      * <p>
 179  
      * Constructs a new MagicNumberFileFilter and associates it with the magic
 180  
      * number to test for in files and the byte offset location in the file to
 181  
      * to look for that magic number.
 182  
      * </p>
 183  
      *
 184  
      * <pre>
 185  
      * MagicNumberFileFilter tarFileFilter =
 186  
      *     MagicNumberFileFilter(new byte[] {0x75, 0x73, 0x74, 0x61, 0x72}, 257);
 187  
      * </pre>
 188  
      *
 189  
      * <pre>
 190  
      * MagicNumberFileFilter javaClassFileFilter =
 191  
      *     MagicNumberFileFilter(new byte[] {0xCA, 0xFE, 0xBA, 0xBE}, 0);
 192  
      * </pre>
 193  
      *
 194  
      * @param magicNumber the magic number to look for in the file.
 195  
      * @param offset the byte offset in the file to start comparing bytes.
 196  
      *
 197  
      * @throws IllegalArgumentException if <code>magicNumber</code> is
 198  
      *         {@code null}, or contains no bytes, or <code>offset</code>
 199  
      *         is a negative number.
 200  
      */
 201  14
     public MagicNumberFileFilter(final byte[] magicNumber, final long offset) {
 202  14
         if (magicNumber == null) {
 203  2
             throw new IllegalArgumentException("The magic number cannot be null");
 204  
         }
 205  12
         if (magicNumber.length == 0) {
 206  2
             throw new IllegalArgumentException("The magic number must contain at least one byte");
 207  
         }
 208  10
         if (offset < 0) {
 209  2
             throw new IllegalArgumentException("The offset cannot be negative");
 210  
         }
 211  
 
 212  8
         this.magicNumbers = new byte[magicNumber.length];
 213  8
         System.arraycopy(magicNumber, 0, this.magicNumbers, 0, magicNumber.length);
 214  8
         this.byteOffset = offset;
 215  8
     }
 216  
 
 217  
     /**
 218  
      * <p>
 219  
      * Accepts the provided file if the file contains the file filter's magic
 220  
      * number at the specified offset.
 221  
      * </p>
 222  
      *
 223  
      * <p>
 224  
      * If any {@link IOException}s occur while reading the file, the file will
 225  
      * be rejected.
 226  
      * </p>
 227  
      *
 228  
      * @param file the file to accept or reject.
 229  
      *
 230  
      * @return {@code true} if the file contains the filter's magic number
 231  
      *         at the specified offset, {@code false} otherwise.
 232  
      */
 233  
     @Override
 234  
     public boolean accept(final File file) {
 235  104
         if (file != null && file.isFile() && file.canRead()) {
 236  72
             RandomAccessFile randomAccessFile = null;
 237  
             try {
 238  72
                 final byte[] fileBytes = new byte[this.magicNumbers.length];
 239  72
                 randomAccessFile = new RandomAccessFile(file, "r");
 240  72
                 randomAccessFile.seek(byteOffset);
 241  72
                 final int read = randomAccessFile.read(fileBytes);
 242  72
                 if (read != magicNumbers.length) {
 243  8
                     return false;
 244  
                 }
 245  64
                 return Arrays.equals(this.magicNumbers, fileBytes);
 246  0
             } catch (final IOException ioe) {
 247  
                 // Do nothing, fall through and do not accept file
 248  
             } finally {
 249  72
                 IOUtils.closeQuietly(randomAccessFile);
 250  0
             }
 251  
         }
 252  
 
 253  32
         return false;
 254  
     }
 255  
 
 256  
     /**
 257  
      * Returns a String representation of the file filter, which includes the
 258  
      * magic number bytes and byte offset.
 259  
      *
 260  
      * @return a String representation of the file filter.
 261  
      */
 262  
     @Override
 263  
     public String toString() {
 264  52
         final StringBuilder builder = new StringBuilder(super.toString());
 265  52
         builder.append("(");
 266  52
         builder.append(new String(magicNumbers, Charset.defaultCharset()));// TODO perhaps use hex if value is not printable
 267  52
         builder.append(",");
 268  52
         builder.append(this.byteOffset);
 269  52
         builder.append(")");
 270  52
         return builder.toString();
 271  
     }
 272  
 }