File Source: fileutils.java
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 */
/*
P/P * Method: void net.sourceforge.pebble.util.FileUtils()
*/
48 public final class FileUtils {
49
50 /** the logger used by this class */
/*
P/P * Method: net.sourceforge.pebble.util.FileUtils__static_init
*
* Presumptions:
* java.lang.Class:getClassLoader(...)@59 != null
* org.apache.commons.logging.LogFactory:getLog(...)@51 != null
*
* Postconditions:
* localFileNameMap in Addr_Set{null,&new Properties(FileUtils__static_init#1)}
* (soft) log != null
* new Properties(FileUtils__static_init#1) num objects <= 1
*
* Test Vectors:
* java.lang.ClassLoader:getResourceAsStream(...)@59: Addr_Set{null}, Inverse{null}
*/
51 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 localFileNameMap = new Properties();
59 InputStream in = FileUtils.class.getClassLoader().getResourceAsStream("content-types.properties");
60 if (in != null) {
61 localFileNameMap.load(in);
62 in.close();
63 }
64 } catch (IOException ioe) {
65 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 public static boolean underneathRoot(File root, File file) {
79 try {
80 // first of all, find the root directory for this type of file
/*
P/P * Method: bool underneathRoot(File, File)
*
* Preconditions:
* (soft) file != null
* (soft) root != null
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* java.io.File:equals(...)@84: {0}, {1}
*/
81 root = root.getCanonicalFile();
82 file = file.getCanonicalFile();
83 while (file != null) {
84 if (file.equals(root)) {
85 return true;
86 } else {
87 file = file.getParentFile();
88 }
89 }
90 } catch (IOException ioe) {
91 return false;
92 }
93
94 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) {
/*
P/P * Method: void deleteFile(File)
*
* Preconditions:
* directory != null
*
* Presumptions:
* files.length@104 <= 232-1
* files[i]@104 != null
*
* Test Vectors:
* java.io.File:isDirectory(...)@107: {0}, {1}
* java.io.File:listFiles(...)@104: Addr_Set{null}, Inverse{null}
*/
104 File files[] = directory.listFiles();
105 if (files != null) {
106 for (int i = 0; i < files.length; i++) {
107 if (files[i].isDirectory()) {
108 deleteFile(files[i]);
109 } else {
110 files[i].delete();
111 }
112 }
113 }
114
115 directory.delete();
116 }
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 {
/*
P/P * Method: void copyFile(File, File)
*
* Presumptions:
* java.io.FileInputStream:getChannel(...)@125 != null
* java.io.FileOutputStream:getChannel(...)@126 != null
*/
125 FileChannel srcChannel = new FileInputStream(source).getChannel();
126 FileChannel dstChannel = new FileOutputStream(destination).getChannel();
127 dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
128 srcChannel.close();
129 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 public static String getContentType(String name) {
139 String contentType;
/*
P/P * Method: String getContentType(String)
*
* Preconditions:
* (soft) localFileNameMap != null
* (soft) name != null
*
* Presumptions:
* java.net.URLConnection:getFileNameMap(...)@140 != null
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* java.lang.String:lastIndexOf(...)@144: {-231..-1}, {0..232-1}
* java.net.FileNameMap:getContentTypeFor(...)@141: Inverse{null}, Addr_Set{null}
*/
140 FileNameMap fileNameMap = URLConnection.getFileNameMap();
141 contentType = fileNameMap.getContentTypeFor(name);
142
143 if (contentType == null) {
144 int index = name.lastIndexOf(".");
145 if (index > -1) {
146 contentType = localFileNameMap.getProperty(name.substring(index));
147 }
148 }
149
150 return contentType;
151 }
152
153 }
SofCheck Inspector Build Version : 2.22510
| fileutils.java |
2010-Jun-25 19:40:32 |
| fileutils.class |
2010-Jul-19 20:23:38 |