Example usage for org.apache.commons.io.filefilter MagicNumberFileFilter accept

List of usage examples for org.apache.commons.io.filefilter MagicNumberFileFilter accept

Introduction

In this page you can find the example usage for org.apache.commons.io.filefilter MagicNumberFileFilter accept.

Prototype

@Override
public boolean accept(File file) 

Source Link

Document

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

Usage

From source file:org.openbel.framework.core.df.cache.CacheUtil.java

/**
 * Copies the <tt>resource</tt> to <tt>copy</tt>.  Decompression is
 * performed if the resource file is identified as a GZIP-encoded file.
 *
 * @param resource {@link File}, the resource to copy
 * @param resourceLocation {@link String}, the resource location url
 * @param copy {@link File}, the file to copy to
 * @return {@link File}, the copied file
 * @throws ResourceDownloadError Thrown if an IO error copying the resource
 *//*from  w  w w  . ja v  a2s . c om*/
private static File copyWithDecompression(final File resource, final String resourceLocation, final File copy)
        throws ResourceDownloadError {
    GZIPInputStream gzipis = null;
    FileOutputStream fout = null;
    try {
        MagicNumberFileFilter mnff = new MagicNumberFileFilter(GZIP_MAGIC_NUMBER);

        if (mnff.accept(resource)) {
            gzipis = new GZIPInputStream(new FileInputStream(resource));
            byte[] buffer = new byte[8192];

            fout = new FileOutputStream(copy);

            int length;
            while ((length = gzipis.read(buffer, 0, 8192)) != -1) {
                fout.write(buffer, 0, length);
            }
        } else {
            copyFile(resource, copy);
        }
    } catch (IOException e) {
        String msg = e.getMessage();
        ResourceDownloadError r = new ResourceDownloadError(resourceLocation, msg);
        r.initCause(e);
        throw r;
    } finally {
        // clean up all I/O resources
        closeQuietly(fout);
        closeQuietly(gzipis);
    }

    return copy;
}