Clover Coverage Report - Pebble 2.5-SNAPSHOT
Coverage timestamp: Sat Jun 12 2010 09:39:29 EST
../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
36   153   15   7,2
16   71   0,42   5
5     3  
1    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  FileUtils       Line # 48 36 0% 15 3 94,7% 0.94736844
 
  (92)
 
1    /*
2    * Copyright (c) 2003-2006, 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    public final class FileUtils {
49   
50    /** the logger used by this class */
51    private static final Log log = LogFactory.getLog(FileUtils.class);
52   
53    /** the local content type map */
54    private static Properties localFileNameMap;
55   
 
56  2 toggle static {
57  2 try {
58  2 localFileNameMap = new Properties();
59  2 InputStream in = FileUtils.class.getClassLoader().getResourceAsStream("content-types.properties");
60  2 if (in != null) {
61  2 localFileNameMap.load(in);
62  2 in.close();
63    }
64    } catch (IOException ioe) {
65  0 log.error("Could not load content types.", ioe);
66    }
67    }
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  516 toggle public static boolean underneathRoot(File root, File file) {
79  516 try {
80    // first of all, find the root directory for this type of file
81  516 root = root.getCanonicalFile();
82  516 file = file.getCanonicalFile();
83  1250 while (file != null) {
84  1208 if (file.equals(root)) {
85  474 return true;
86    } else {
87  734 file = file.getParentFile();
88    }
89    }
90    } catch (IOException ioe) {
91  0 return false;
92    }
93   
94  42 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  15814 toggle public static void deleteFile(File directory) {
104  15814 File files[] = directory.listFiles();
105  15814 if (files != null) {
106  27144 for (int i = 0; i < files.length; i++) {
107  12674 if (files[i].isDirectory()) {
108  11888 deleteFile(files[i]);
109    } else {
110  786 files[i].delete();
111    }
112    }
113    }
114   
115  15814 directory.delete();
116    }
117   
118    /**
119    * Copies a file.
120    *
121    * @param source the source File
122    * @param destination the destination File
123    */
 
124  8 toggle public static void copyFile(File source, File destination) throws IOException {
125  8 FileChannel srcChannel = new FileInputStream(source).getChannel();
126  8 FileChannel dstChannel = new FileOutputStream(destination).getChannel();
127  8 dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
128  8 srcChannel.close();
129  8 dstChannel.close();
130    }
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  6 toggle public static String getContentType(String name) {
139  6 String contentType;
140  6 FileNameMap fileNameMap = URLConnection.getFileNameMap();
141  6 contentType = fileNameMap.getContentTypeFor(name);
142   
143  6 if (contentType == null) {
144  4 int index = name.lastIndexOf(".");
145  4 if (index > -1) {
146  2 contentType = localFileNameMap.getProperty(name.substring(index));
147    }
148    }
149   
150  6 return contentType;
151    }
152   
153    }