James Moger
2012-10-31 40b07bca7d02438cd0d660f3b1713ffa86f6df76
commit | author | age
892570 1 /*
JM 2  * Copyright 2011 gitblit.com.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.gitblit.utils;
17
e36d4d 18 import java.io.BufferedInputStream;
892570 19 import java.io.BufferedReader;
a2709d 20 import java.io.BufferedWriter;
892570 21 import java.io.File;
JM 22 import java.io.FileInputStream;
e36d4d 23 import java.io.FileNotFoundException;
a2709d 24 import java.io.FileOutputStream;
e36d4d 25 import java.io.IOException;
892570 26 import java.io.InputStreamReader;
a2709d 27 import java.io.OutputStreamWriter;
892570 28 import java.nio.charset.Charset;
JM 29
30 /**
31  * Common file utilities.
32  * 
33  * @author James Moger
34  * 
35  */
36 public class FileUtils {
478678 37     
JM 38     /** 1024 (number of bytes in one kilobyte) */
39     public static final int KB = 1024;
40
41     /** 1024 {@link #KB} (number of bytes in one megabyte) */
42     public static final int MB = 1024 * KB;
43
44     /** 1024 {@link #MB} (number of bytes in one gigabyte) */
45     public static final int GB = 1024 * MB;
46
47     /**
48      * Returns an int from a string representation of a file size.
49      * e.g. 50m = 50 megabytes
50      * 
51      * @param aString
52      * @param defaultValue
53      * @return an int value or the defaultValue if aString can not be parsed
54      */
55     public static int convertSizeToInt(String aString, int defaultValue) {
56         return (int) convertSizeToLong(aString, defaultValue);
57     }
58     
59     /**
60      * Returns a long from a string representation of a file size.
61      * e.g. 50m = 50 megabytes
62      * 
63      * @param aString
64      * @param defaultValue
65      * @return a long value or the defaultValue if aString can not be parsed
66      */
67     public static long convertSizeToLong(String aString, long defaultValue) {
68         // trim string and remove all spaces 
69         aString = aString.toLowerCase().trim();
70         StringBuilder sb = new StringBuilder();
71         for (String a : aString.split(" ")) {
72             sb.append(a);
73         }
74         aString = sb.toString();
75         
76         // identify value and unit
77         int idx = 0;
78         int len = aString.length();
79         while (Character.isDigit(aString.charAt(idx))) {
80             idx++;
81             if (idx == len) {
82                 break;
83             }
84         }
85         long value = 0;
86         String unit = null;
87         try {
88             value = Long.parseLong(aString.substring(0, idx));
89             unit = aString.substring(idx);
90         } catch (Exception e) {
91             return defaultValue;
92         }
93         if (unit.equals("g") || unit.equals("gb")) {
94             return value * GB;
95         } else if (unit.equals("m") || unit.equals("mb")) {
96             return value * MB;
97         } else if (unit.equals("k") || unit.equals("kb")) {
98             return value * KB;
99         }
100         return defaultValue;
101     }
892570 102
JM 103     /**
104      * Returns the string content of the specified file.
105      * 
106      * @param file
107      * @param lineEnding
d9f687 108      * @return the string content of the file
892570 109      */
JM 110     public static String readContent(File file, String lineEnding) {
111         StringBuilder sb = new StringBuilder();
112         try {
113             InputStreamReader is = new InputStreamReader(new FileInputStream(file),
114                     Charset.forName("UTF-8"));
115             BufferedReader reader = new BufferedReader(is);
116             String line = null;
117             while ((line = reader.readLine()) != null) {
118                 sb.append(line);
119                 if (lineEnding != null) {
120                     sb.append(lineEnding);
121                 }
122             }
123             reader.close();
124         } catch (Throwable t) {
125             System.err.println("Failed to read content of " + file.getAbsolutePath());
126             t.printStackTrace();
127         }
128         return sb.toString();
129     }
5c2841 130
JM 131     /**
a2709d 132      * Writes the string content to the file.
JM 133      * 
134      * @param file
135      * @param content
136      */
137     public static void writeContent(File file, String content) {
138         try {
139             OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(file),
671c19 140                     Charset.forName("UTF-8"));
a2709d 141             BufferedWriter writer = new BufferedWriter(os);
JM 142             writer.append(content);
143             writer.close();
144         } catch (Throwable t) {
145             System.err.println("Failed to write content of " + file.getAbsolutePath());
146             t.printStackTrace();
147         }
148     }
149
150     /**
5c2841 151      * Recursively traverses a folder and its subfolders to calculate the total
JM 152      * size in bytes.
153      * 
154      * @param directory
155      * @return folder size in bytes
156      */
157     public static long folderSize(File directory) {
158         if (directory == null || !directory.exists()) {
159             return -1;
160         }
161         if (directory.isFile()) {
162             return directory.length();
163         }
164         long length = 0;
165         for (File file : directory.listFiles()) {
88598b 166             if (file.isFile()) {
5c2841 167                 length += file.length();
88598b 168             } else {
5c2841 169                 length += folderSize(file);
88598b 170             }
5c2841 171         }
JM 172         return length;
173     }
e36d4d 174
JM 175     /**
176      * Copies a file or folder (recursively) to a destination folder.
177      * 
178      * @param destinationFolder
179      * @param filesOrFolders
180      * @return
181      * @throws FileNotFoundException
182      * @throws IOException
183      */
184     public static void copy(File destinationFolder, File... filesOrFolders)
185             throws FileNotFoundException, IOException {
186         destinationFolder.mkdirs();
187         for (File file : filesOrFolders) {
188             if (file.isDirectory()) {
189                 copy(new File(destinationFolder, file.getName()), file.listFiles());
190             } else {
191                 File dFile = new File(destinationFolder, file.getName());
192                 BufferedInputStream bufin = null;
193                 FileOutputStream fos = null;
194                 try {
195                     bufin = new BufferedInputStream(new FileInputStream(file));
196                     fos = new FileOutputStream(dFile);
197                     int len = 8196;
198                     byte[] buff = new byte[len];
199                     int n = 0;
200                     while ((n = bufin.read(buff, 0, len)) != -1) {
201                         fos.write(buff, 0, n);
202                     }
203                 } finally {
204                     try {
205                         bufin.close();
206                     } catch (Throwable t) {
207                     }
208                     try {
209                         fos.close();
210                     } catch (Throwable t) {
211                     }
212                 }
213                 dFile.setLastModified(file.lastModified());
214             }
215         }
216     }
1aa6e0 217     
JM 218     /**
219      * Determine the relative path between two files.  Takes into account
220      * canonical paths, if possible.
221      * 
222      * @param basePath
223      * @param path
224      * @return a relative path from basePath to path
225      */
226     public static String getRelativePath(File basePath, File path) {
227         File exactBase = getExactFile(basePath);
228         File exactPath = getExactFile(path);
d65fb8 229         if (path.getAbsolutePath().startsWith(basePath.getAbsolutePath())) {
JM 230             // absolute base-path match
231             return StringUtils.getRelativePath(basePath.getAbsolutePath(), path.getAbsolutePath());
232         } else if (exactPath.getPath().startsWith(exactBase.getPath())) {
5e0107 233             // canonical base-path match
JM 234             return StringUtils.getRelativePath(exactBase.getPath(), exactPath.getPath());
235         } else if (exactPath.getPath().startsWith(basePath.getAbsolutePath())) {
236             // mixed path match
237             return StringUtils.getRelativePath(basePath.getAbsolutePath(), exactPath.getPath());
238         } else if (path.getAbsolutePath().startsWith(exactBase.getPath())) {
239             // mixed path match
240             return StringUtils.getRelativePath(exactBase.getPath(), path.getAbsolutePath());
241         }
242         // no relative relationship
243         return null;
1aa6e0 244     }
JM 245     
246     /**
247      * Returns the exact path for a file. This path will be the canonical path
248      * unless an exception is thrown in which case it will be the absolute path.
249      * 
250      * @param path
251      * @return the exact file
252      */
253     public static File getExactFile(File path) {
254         try {
255             return path.getCanonicalFile();
256         } catch (IOException e) {
257             return path.getAbsoluteFile();
258         }
259     }
892570 260 }