1 package com.atlassian.core.util;
2
3
4
5 import com.atlassian.core.util.zip.FolderArchiver;
6 import com.opensymphony.util.TextUtils;
7 import org.apache.log4j.Logger;
8
9 import javax.servlet.http.HttpServletRequest;
10 import java.io.*;
11 import java.util.ArrayList;
12 import java.util.List;
13
14
15
16
17 public class FileUtils
18 {
19 private static final Logger log = Logger.getLogger(FileUtils.class);
20 private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
21
22
23
24
25
26
27
28
29 public static int copy(final InputStream input, final OutputStream output)
30 throws IOException
31 {
32 return copy(input, output, DEFAULT_BUFFER_SIZE);
33 }
34
35
36
37
38
39
40
41
42
43 public static int copy(final InputStream input,
44 final OutputStream output,
45 final int bufferSize)
46 throws IOException
47 {
48 final byte[] buffer = new byte[bufferSize];
49 int count = 0;
50 int n = 0;
51 while (-1 != (n = input.read(buffer)))
52 {
53 output.write(buffer, 0, n);
54 count += n;
55 }
56 return count;
57 }
58
59
60
61
62
63
64 public static void shutdownStream(final OutputStream output)
65 {
66 if (output == null)
67 {
68 return;
69 }
70
71 try
72 {
73 output.close();
74 }
75 catch (final IOException ioe)
76 {
77 }
78 }
79
80
81
82
83
84
85 public static void shutdownStream(final InputStream input)
86 {
87 if (input == null)
88 {
89 return;
90 }
91
92 try
93 {
94 input.close();
95 }
96 catch (final IOException ioe)
97 {
98 }
99 }
100
101
102
103
104
105
106
107 public static boolean deleteDir(File dir)
108 {
109 if (dir == null)
110 {
111 return false;
112 }
113
114
115
116
117 File candir;
118 try
119 {
120 candir = dir.getCanonicalFile();
121 }
122 catch (IOException e)
123 {
124 return false;
125 }
126
127
128
129 if (!candir.equals(dir.getAbsoluteFile()))
130 {
131
132
133
134 return false;
135 }
136
137
138
139 File[] files = candir.listFiles();
140 if (files != null)
141 {
142 for (int i = 0; i < files.length; i++)
143 {
144 File file = files[i];
145
146
147
148
149 boolean deleted = !file.delete();
150 if (deleted)
151 {
152
153
154 if (file.isDirectory()) deleteDir(file);
155
156
157 }
158 }
159 }
160
161
162
163 return dir.delete();
164 }
165
166
167
168
169
170 public static void recursiveDelete(File file)
171 {
172 File[] files = null;
173
174 if (file.isDirectory())
175 files = file.listFiles();
176 else
177 {
178 file.delete();
179 return;
180 }
181
182 for (int i = 0; i < files.length; i++)
183 {
184 File next = files[i];
185 recursiveDelete(next);
186 }
187
188 file.delete();
189 }
190
191
192
193
194
195 public static String getResourceContent(String resource)
196 {
197 InputStream is = ClassLoaderUtils.getResourceAsStream(resource, FileUtils.class);
198 return getInputStreamTextContent(is);
199 }
200
201
202
203
204 public static String getResourceContent(HttpServletRequest req, String resource)
205 {
206 InputStream is = req.getSession().getServletContext().getResourceAsStream(resource);
207 String result = getInputStreamTextContent(is);
208
209 if (result == null)
210 {
211 result = "";
212 }
213
214 return result;
215 }
216
217
218
219
220 public static String getInputStreamTextContent(InputStream is)
221 {
222 if (is == null)
223 {
224 return null;
225 }
226
227 String result = null;
228
229 try
230 {
231 ByteArrayOutputStream baos = new ByteArrayOutputStream(is.available());
232
233 copy(is, baos);
234
235 result = new String(baos.toByteArray());
236
237 is.close();
238 }
239 catch (IOException e)
240 {
241 log.error("IOException reading stream: " + e, e);
242 }
243
244 return result;
245 }
246
247
248
249
250
251
252
253
254
255 public static void saveTextFile(String stringContent, File destFile) throws IOException
256 {
257 ensureFileAndPathExist(destFile);
258
259 FileWriter writer = new FileWriter(destFile);
260 writer.write(stringContent);
261 writer.close();
262 }
263
264
265
266
267 public static void ensureFileAndPathExist(File file) throws IOException
268 {
269 file.getParentFile().mkdirs();
270 file.createNewFile();
271 }
272
273
274
275
276
277
278
279
280
281
282 public static boolean moveDir(File dirName, File destDir)
283 {
284 File destParent = new File(destDir.getParent());
285
286
287 if (destDir.exists())
288 {
289 destDir.delete();
290 }
291
292
293 destParent.mkdirs();
294 return dirName.renameTo(destDir);
295 }
296
297
298
299
300 public static void createZipFile(File baseDir, File zipFile) throws Exception
301 {
302 FolderArchiver compressor = new FolderArchiver(baseDir, zipFile);
303 compressor.doArchive();
304 }
305
306
307
308
309
310
311 public static List readResourcesAsList(String resource)
312 {
313 List result = new ArrayList();
314
315 try
316 {
317 InputStream is = ClassLoaderUtils.getResourceAsStream(resource, FileUtils.class);
318 BufferedReader in = new BufferedReader(new InputStreamReader(is));
319 String s;
320
321 while ((s = in.readLine()) != null)
322 {
323 String niceS = TextUtils.noNull(s).trim();
324
325 if (TextUtils.stringSet(niceS) && !(niceS.charAt(0) == '#'))
326 {
327 result.add(s);
328 }
329 }
330
331 is.close();
332 }
333 catch (IOException e)
334 {
335 log.error("IOException reading stream: " + e, e);
336 }
337
338 return result;
339 }
340
341
342
343
344 public static void copyDirectory(File srcDir, File destDir) throws IOException
345 {
346 copyDirectory(srcDir, destDir, false);
347 }
348
349 public static void copyDirectory(File srcDir, File destDir, boolean overwrite) throws IOException
350 {
351 File[] files = srcDir.listFiles();
352
353 if (!destDir.exists())
354 destDir.mkdirs();
355 else
356 log.debug(destDir.getAbsolutePath() + " already exists");
357
358 if (files != null)
359 {
360 for (int i = 0; i < files.length; i++)
361 {
362 File file = files[i];
363 File dest = new File(destDir, file.getName());
364
365 if (file.isFile())
366 copyFile(new FileInputStream(file), dest, overwrite);
367 else
368 copyDirectory(file, dest, overwrite);
369 }
370 }
371 }
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386 public static void copyFile(File srcFile, File destFile) throws IOException
387 {
388 copyFile(srcFile, destFile, true);
389 }
390
391 public static void copyFile(File srcFile, File destFile, boolean overwrite) throws IOException
392 {
393
394 if (!srcFile.exists())
395 {
396 final String message = "File " + srcFile + " does not exist";
397 throw new IOException(message);
398 }
399
400 InputStream input = new FileInputStream(srcFile);
401 copyFile(input, destFile, overwrite);
402
403 if (srcFile.length() != srcFile.length())
404 {
405 final String message = "Failed to copy full contents from " + srcFile + " to " + destFile;
406 throw new IOException(message);
407 }
408 }
409
410 public static void copyFile(InputStream srcStream, File destFile) throws IOException
411 {
412 copyFile(srcStream, destFile, false);
413 }
414
415 public static void copyFile(InputStream srcStream, File destFile, boolean overwrite) throws IOException
416 {
417 File parentFile = destFile.getParentFile();
418 if (!parentFile.isDirectory())
419 {
420 parentFile.mkdirs();
421 }
422
423 if (destFile.exists())
424 {
425 if (!destFile.canWrite())
426 {
427 final String message = "Unable to open file " + destFile + " for writing.";
428 throw new IOException(message);
429 }
430
431 if (overwrite)
432 {
433 log.debug("Overwriting file at: " + destFile.getAbsolutePath());
434 writeStreamToFile(srcStream, destFile);
435 }
436 else
437 {
438 shutdownStream(srcStream);
439 log.warn(destFile.getAbsolutePath() + " already exists");
440 }
441 }
442 else
443 {
444 destFile.createNewFile();
445 writeStreamToFile(srcStream, destFile);
446 }
447 }
448
449 private static void writeStreamToFile(InputStream srcStream, File destFile) throws IOException
450 {
451 InputStream input = null;
452 OutputStream output = null;
453
454 try
455 {
456 input = new BufferedInputStream(srcStream);
457 output = new BufferedOutputStream(new FileOutputStream(destFile));
458 int ch;
459
460 while ((ch = input.read()) != -1)
461 output.write(ch);
462 }
463 catch (IOException e)
464 {
465 log.error("Error writing stream to file: " + destFile.getAbsolutePath());
466 throw e;
467 }
468 finally
469 {
470 shutdownStream(input);
471 shutdownStream(output);
472 }
473 }
474 }