|
Commons IO example source code file (MagicNumberFileFilter.java)
The Commons IO MagicNumberFileFilter.java source code/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.commons.io.filefilter; import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; import java.io.Serializable; import java.util.Arrays; import org.apache.commons.io.IOUtils; /** * <p> * 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 * <code>0xCAFEBABE. * </p> * * <code>* File dir = new File("."); * 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); * } * </pre> * * <p> * 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. * </p> * * <code>* File dir = new File("."); * MagicNumberFileFilter tarFileFilter = * MagicNumberFileFilter("ustar", 257); * String[] tarFiles = dir.list(tarFileFilter); * for (String tarFile : tarFiles) { * System.out.println(tarFile); * } * </pre> * @since Commons IO 2.0 * @see FileFilterUtils#magicNumberFileFilter(byte[]) * @see FileFilterUtils#magicNumberFileFilter(String) * @see FileFilterUtils#magicNumberFileFilter(byte[], long) * @see FileFilterUtils#magicNumberFileFilter(String, long) */ public class MagicNumberFileFilter extends AbstractFileFilter implements Serializable { /** * The serialization version unique identifier. */ private static final long serialVersionUID = -547733176983104172L; /** * The magic number to compare against the file's bytes at the provided * offset. */ private final byte[] magicNumbers; /** * The offset (in bytes) within the files that the magic number's bytes * should appear. */ private final long byteOffset; /** * <p> * Constructs a new MagicNumberFileFilter and associates it with the magic * number to test for in files. This constructor assumes a starting offset * of <code>0. * </p> * * <p> * It is important to note that <em>the array is not cloned and that * any changes to the magic number array after construction will affect the * behavior of this file filter. * </p> * * <code>* MagicNumberFileFilter javaClassFileFilter = * MagicNumberFileFilter(new byte[] {(byte) 0xCA, (byte) 0xFE, * (byte) 0xBA, (byte) 0xBE}); * </pre> * * @param magicNumber the magic number to look for in the file. * * @throws IllegalArgumentException if <code>magicNumber is * <code>null, or contains no bytes. */ public MagicNumberFileFilter(byte[] magicNumber) { this(magicNumber, 0); } /** * <p> * Constructs a new MagicNumberFileFilter and associates it with the magic * number to test for in files. This constructor assumes a starting offset * of <code>0. * </p> * * Example usage: * <pre> * {@code * MagicNumberFileFilter xmlFileFilter = * MagicNumberFileFilter("<?xml"); * } * </pre> * * @param magicNumber the magic number to look for in the file. * The string is converted to bytes using the platform default charset. * * @throws IllegalArgumentException if <code>magicNumber is * <code>null or the empty String. */ public MagicNumberFileFilter(String magicNumber) { this(magicNumber, 0); } /** * <p> * 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. * </p> * * <code>* MagicNumberFileFilter tarFileFilter = * MagicNumberFileFilter("ustar", 257); * </pre> * * @param magicNumber the magic number to look for in the file. * The string is converted to bytes using the platform default charset. * @param offset the byte offset in the file to start comparing bytes. * * @throws IllegalArgumentException if <code>magicNumber is * <code>null or the empty String, or |
... this post is sponsored by my books ... | |
![]() #1 New Release! |
![]() FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.