Coverage Report - net.sourceforge.pebble.util.FileUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
FileUtils
88%
37/42
100%
14/14
3.5
 
 1  
 /*
 2  
  * Copyright (c) 2003-2011, Simon Brown
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions are met:
 7  
  *
 8  
  *   - Redistributions of source code must retain the above copyright
 9  
  *     notice, this list of conditions and the following disclaimer.
 10  
  *
 11  
  *   - Redistributions in binary form must reproduce the above copyright
 12  
  *     notice, this list of conditions and the following disclaimer in
 13  
  *     the documentation and/or other materials provided with the
 14  
  *     distribution.
 15  
  *
 16  
  *   - Neither the name of Pebble nor the names of its contributors may
 17  
  *     be used to endorse or promote products derived from this software
 18  
  *     without specific prior written permission.
 19  
  *
 20  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 21  
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 22  
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 23  
  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 24  
  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 25  
  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 26  
  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 27  
  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 28  
  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 29  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 30  
  * POSSIBILITY OF SUCH DAMAGE.
 31  
  */
 32  
 package net.sourceforge.pebble.util;
 33  
 
 34  
 import org.apache.commons.logging.Log;
 35  
 import org.apache.commons.logging.LogFactory;
 36  
 
 37  
 import java.io.*;
 38  
 import java.net.FileNameMap;
 39  
 import java.net.URLConnection;
 40  
 import java.nio.channels.FileChannel;
 41  
 import java.util.Properties;
 42  
 
 43  
 /**
 44  
  * A collection of utility methods for manipulating files.
 45  
  *
 46  
  * @author    Simon Brown
 47  
  */
 48  0
 public final class FileUtils {
 49  
 
 50  
   /** the logger used by this class */
 51  4
   private static final Log log = LogFactory.getLog(FileUtils.class);
 52  
 
 53  
   /** the local content type map */
 54  
   private static Properties localFileNameMap;
 55  
 
 56  
   static {
 57  
     try {
 58  4
       localFileNameMap = new Properties();
 59  4
       InputStream in = FileUtils.class.getClassLoader().getResourceAsStream("content-types.properties");
 60  4
       if (in != null) {
 61  4
         localFileNameMap.load(in);
 62  4
         in.close();
 63  
       }
 64  0
     } catch (IOException ioe) {
 65  0
       log.error("Could not load content types.", ioe);
 66  4
     }
 67  4
   }
 68  
 
 69  
   /**
 70  
    * Determines whether a given file is underneath a given root.
 71  
    *
 72  
    * @param root    the root directory
 73  
    * @param file    the file to test
 74  
    * @return    true if the file is underneath the root,
 75  
    *            false otherwise or if this can not be determined because
 76  
    *            of security constraints in place
 77  
    */
 78  
   public static boolean underneathRoot(File root, File file) {
 79  
     try {
 80  
       // first of all, find the root directory for this type of file
 81  1032
       root = root.getCanonicalFile();
 82  1032
       file = file.getCanonicalFile();
 83  2500
       while (file != null) {
 84  2416
         if (file.equals(root)) {
 85  948
           return true;
 86  
         } else {
 87  1468
           file = file.getParentFile();
 88  
         }
 89  
       }
 90  0
     } catch (IOException ioe) {
 91  0
       return false;
 92  84
     }
 93  
 
 94  84
     return false;
 95  
   }
 96  
 
 97  
   /**
 98  
    * Deletes a file, including all files and sub-directories if the
 99  
    * specified file is a directory.
 100  
    *
 101  
    * @param directory   a File instance representing the directory to delete
 102  
    */
 103  
   public static void deleteFile(File directory) {
 104  32280
     File files[] = directory.listFiles();
 105  32280
     if (files != null) {
 106  55400
       for (int i = 0; i < files.length; i++) {
 107  25860
         if (files[i].isDirectory()) {
 108  24264
           deleteFile(files[i]);
 109  
         } else {
 110  1596
           files[i].delete();
 111  
         }
 112  
       }
 113  
     }
 114  
 
 115  32280
     directory.delete();
 116  32280
   }
 117  
 
 118  
   /**
 119  
    * Copies a file.
 120  
    *
 121  
    * @param source        the source File
 122  
    * @param destination   the destination File
 123  
    */
 124  
   public static void copyFile(File source, File destination) throws IOException {
 125  16
     FileChannel srcChannel = new FileInputStream(source).getChannel();
 126  16
     FileChannel dstChannel = new FileOutputStream(destination).getChannel();
 127  16
     dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
 128  16
     srcChannel.close();
 129  16
     dstChannel.close();
 130  16
   }
 131  
 
 132  
   /**
 133  
    * Gets the content type for the specified filename.
 134  
    *
 135  
    * @param name    the name of a file
 136  
    * @return  a MIME type, or application/octet-stream if one can't be found
 137  
    */
 138  
   public static String getContentType(String name) {
 139  
     String contentType;
 140  12
     FileNameMap fileNameMap = URLConnection.getFileNameMap();
 141  12
     contentType = fileNameMap.getContentTypeFor(name);
 142  
 
 143  12
     if (contentType == null) {
 144  8
       int index = name.lastIndexOf(".");
 145  8
       if (index > -1) {
 146  4
         contentType = localFileNameMap.getProperty(name.substring(index));
 147  
       }
 148  
     }
 149  
 
 150  12
     return contentType;
 151  
   }
 152  
 
 153  
 }