1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.util; |
12 | |
|
13 | |
import org.mule.MuleServer; |
14 | |
import org.mule.api.MuleRuntimeException; |
15 | |
import org.mule.config.i18n.MessageFactory; |
16 | |
|
17 | |
import java.io.BufferedOutputStream; |
18 | |
import java.io.BufferedWriter; |
19 | |
import java.io.File; |
20 | |
import java.io.FileInputStream; |
21 | |
import java.io.FileOutputStream; |
22 | |
import java.io.FileWriter; |
23 | |
import java.io.IOException; |
24 | |
import java.io.InputStream; |
25 | |
import java.io.OutputStream; |
26 | |
import java.io.UnsupportedEncodingException; |
27 | |
import java.net.JarURLConnection; |
28 | |
import java.net.URI; |
29 | |
import java.net.URL; |
30 | |
import java.net.URLConnection; |
31 | |
import java.net.URLDecoder; |
32 | |
import java.nio.channels.FileChannel; |
33 | |
import java.util.Enumeration; |
34 | |
import java.util.jar.JarEntry; |
35 | |
import java.util.jar.JarFile; |
36 | |
import java.util.zip.ZipEntry; |
37 | |
import java.util.zip.ZipFile; |
38 | |
|
39 | |
import org.apache.commons.lang.StringUtils; |
40 | |
import org.apache.commons.logging.Log; |
41 | |
import org.apache.commons.logging.LogFactory; |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | 0 | public class FileUtils extends org.apache.commons.io.FileUtils |
49 | |
{ |
50 | 2 | private static final Log logger = LogFactory.getLog(FileUtils.class); |
51 | |
public static final String DEFAULT_ENCODING = "UTF-8"; |
52 | |
|
53 | |
public static synchronized void copyStreamToFile(InputStream input, File destination) throws IOException |
54 | |
{ |
55 | 0 | if (destination.exists() && !destination.canWrite()) |
56 | |
{ |
57 | 0 | throw new IOException("Destination file does not exist or is not writeable"); |
58 | |
} |
59 | |
|
60 | |
try |
61 | |
{ |
62 | 0 | FileOutputStream output = new FileOutputStream(destination); |
63 | |
try |
64 | |
{ |
65 | 0 | IOUtils.copy(input, output); |
66 | |
} |
67 | |
finally |
68 | |
{ |
69 | 0 | IOUtils.closeQuietly(output); |
70 | 0 | } |
71 | |
} |
72 | |
finally |
73 | |
{ |
74 | 0 | IOUtils.closeQuietly(input); |
75 | 0 | } |
76 | 0 | } |
77 | |
|
78 | |
|
79 | |
public static File createFile(String filename) throws IOException |
80 | |
{ |
81 | 8 | File file = FileUtils.newFile(filename); |
82 | 8 | if (!file.canWrite()) |
83 | |
{ |
84 | 4 | String dirName = file.getPath(); |
85 | 4 | int i = dirName.lastIndexOf(File.separator); |
86 | 4 | if (i > -1) |
87 | |
{ |
88 | 4 | dirName = dirName.substring(0, i); |
89 | 4 | File dir = FileUtils.newFile(dirName); |
90 | 4 | dir.mkdirs(); |
91 | |
} |
92 | 4 | file.createNewFile(); |
93 | |
} |
94 | 8 | return file; |
95 | |
} |
96 | |
|
97 | |
|
98 | |
public static String prepareWinFilename(String filename) |
99 | |
{ |
100 | 6 | filename = filename.replaceAll("<", "("); |
101 | 6 | filename = filename.replaceAll(">", ")"); |
102 | 6 | filename = filename.replaceAll("[/\\*?|:;\\]\\[\"]", "-"); |
103 | 6 | return filename; |
104 | |
} |
105 | |
|
106 | |
|
107 | |
public static File openDirectory(String directory) throws IOException |
108 | |
{ |
109 | 20 | File dir = FileUtils.newFile(directory); |
110 | 20 | if (!dir.exists()) |
111 | |
{ |
112 | 12 | dir.mkdirs(); |
113 | |
} |
114 | 20 | if (!dir.isDirectory() || !dir.canRead()) |
115 | |
{ |
116 | 0 | throw new IOException("Path: " + directory + " exists but isn't a directory"); |
117 | |
} |
118 | 20 | return dir; |
119 | |
} |
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
public static File stringToFile(String filename, String data) throws IOException |
130 | |
{ |
131 | 2 | return stringToFile(filename, data, false); |
132 | |
} |
133 | |
|
134 | |
|
135 | |
public static synchronized File stringToFile(String filename, String data, boolean append) |
136 | |
throws IOException |
137 | |
{ |
138 | 4 | return stringToFile(filename, data, append, false); |
139 | |
} |
140 | |
|
141 | |
|
142 | |
public static synchronized File stringToFile(String filename, String data, boolean append, boolean newLine) |
143 | |
throws IOException |
144 | |
{ |
145 | 4 | File f = createFile(filename); |
146 | 4 | BufferedWriter writer = null; |
147 | |
try |
148 | |
{ |
149 | 4 | writer = new BufferedWriter(new FileWriter(f, append)); |
150 | 4 | writer.write(data); |
151 | 4 | if (newLine) |
152 | |
{ |
153 | 0 | writer.newLine(); |
154 | |
} |
155 | |
} |
156 | |
finally |
157 | |
{ |
158 | 4 | if (writer != null) |
159 | |
{ |
160 | 4 | writer.close(); |
161 | |
} |
162 | |
} |
163 | 4 | return f; |
164 | |
} |
165 | |
|
166 | |
|
167 | |
public static String getResourcePath(String resourceName, Class callingClass) throws IOException |
168 | |
{ |
169 | 8 | return getResourcePath(resourceName, callingClass, DEFAULT_ENCODING); |
170 | |
} |
171 | |
|
172 | |
|
173 | |
public static String getResourcePath(String resourceName, Class callingClass, String encoding) |
174 | |
throws IOException |
175 | |
{ |
176 | 8 | if (resourceName == null) |
177 | |
{ |
178 | |
|
179 | 0 | return null; |
180 | |
} |
181 | |
|
182 | 8 | URL url = IOUtils.getResourceAsUrl(resourceName, callingClass); |
183 | 8 | if (url == null) |
184 | |
{ |
185 | |
|
186 | 2 | return null; |
187 | |
} |
188 | 6 | return normalizeFilePath(url, encoding); |
189 | |
} |
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
public static String normalizeFilePath(URL url, String encoding) throws UnsupportedEncodingException |
201 | |
{ |
202 | 16 | String resource = URLDecoder.decode(url.toExternalForm(), encoding); |
203 | 16 | if (resource != null) |
204 | |
{ |
205 | 16 | if (resource.startsWith("file:/")) |
206 | |
{ |
207 | 16 | resource = resource.substring(6); |
208 | |
} |
209 | 16 | if (!resource.startsWith(File.separator)) |
210 | |
{ |
211 | 16 | resource = File.separator + resource; |
212 | |
} |
213 | |
} |
214 | 16 | return resource; |
215 | |
} |
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
public static boolean deleteTree(File dir) |
224 | |
{ |
225 | 2880 | return deleteTree(dir, null); |
226 | |
} |
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
public static boolean deleteTree(File dir, final String[] topLevelDirsToIgnore) |
239 | |
{ |
240 | 4024 | if (dir == null || !dir.exists()) |
241 | |
{ |
242 | 3712 | return true; |
243 | |
} |
244 | 312 | File[] files = dir.listFiles(); |
245 | 312 | if (files != null) |
246 | |
{ |
247 | 2016 | for (int i = 0; i < files.length; i++) |
248 | |
{ |
249 | |
OUTER: |
250 | 1704 | if (files[i].isDirectory()) |
251 | |
{ |
252 | 252 | if (topLevelDirsToIgnore != null) |
253 | |
{ |
254 | 58 | for (int j = 0; j < topLevelDirsToIgnore.length; j++) |
255 | |
{ |
256 | 30 | String ignored = topLevelDirsToIgnore[j]; |
257 | 30 | if (ignored.equals(FilenameUtils.getBaseName(files[i].getName()))) |
258 | |
{ |
259 | 2 | break OUTER; |
260 | |
} |
261 | |
} |
262 | |
} |
263 | 250 | if (!deleteTree(files[i])) |
264 | |
{ |
265 | 0 | return false; |
266 | |
} |
267 | |
} |
268 | |
else |
269 | |
{ |
270 | 1452 | if (!files[i].delete()) |
271 | |
{ |
272 | 0 | return false; |
273 | |
} |
274 | |
} |
275 | |
} |
276 | |
} |
277 | 312 | return dir.delete(); |
278 | |
} |
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
public static void unzip(File archive, File directory) throws IOException |
284 | |
{ |
285 | 0 | ZipFile zip = null; |
286 | |
|
287 | 0 | if (directory.exists()) |
288 | |
{ |
289 | 0 | if (!directory.isDirectory()) |
290 | |
{ |
291 | 0 | throw new IOException("Directory is not a directory: " + directory); |
292 | |
} |
293 | |
} |
294 | |
else |
295 | |
{ |
296 | 0 | if (!directory.mkdirs()) |
297 | |
{ |
298 | 0 | throw new IOException("Could not create directory: " + directory); |
299 | |
} |
300 | |
} |
301 | |
try |
302 | |
{ |
303 | 0 | zip = new ZipFile(archive); |
304 | 0 | for (Enumeration entries = zip.entries(); entries.hasMoreElements();) |
305 | |
{ |
306 | 0 | ZipEntry entry = (ZipEntry) entries.nextElement(); |
307 | 0 | File f = FileUtils.newFile(directory, entry.getName()); |
308 | 0 | if (entry.isDirectory()) |
309 | |
{ |
310 | 0 | if (!f.mkdirs()) |
311 | |
{ |
312 | 0 | throw new IOException("Could not create directory: " + f); |
313 | |
} |
314 | |
} |
315 | |
else |
316 | |
{ |
317 | 0 | InputStream is = zip.getInputStream(entry); |
318 | 0 | OutputStream os = new BufferedOutputStream(new FileOutputStream(f)); |
319 | 0 | IOUtils.copy(is, os); |
320 | 0 | IOUtils.closeQuietly(is); |
321 | 0 | IOUtils.closeQuietly(os); |
322 | |
} |
323 | 0 | } |
324 | |
} |
325 | |
finally |
326 | |
{ |
327 | 0 | if (zip != null) |
328 | |
{ |
329 | 0 | zip.close(); |
330 | |
} |
331 | |
} |
332 | 0 | } |
333 | |
|
334 | |
|
335 | |
|
336 | |
|
337 | |
|
338 | |
|
339 | |
|
340 | |
|
341 | |
|
342 | |
|
343 | |
|
344 | |
public static File newFile(String pathName) |
345 | |
{ |
346 | |
try |
347 | |
{ |
348 | 3900 | return new File(pathName).getCanonicalFile(); |
349 | |
} |
350 | 0 | catch (IOException e) |
351 | |
{ |
352 | 0 | throw new MuleRuntimeException( |
353 | |
MessageFactory.createStaticMessage("Unable to create a canonical file for " + pathName), |
354 | |
e); |
355 | |
} |
356 | |
} |
357 | |
|
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | |
|
364 | |
|
365 | |
|
366 | |
|
367 | |
|
368 | |
public static File newFile(URI uri) |
369 | |
{ |
370 | |
try |
371 | |
{ |
372 | 278 | return new File(uri).getCanonicalFile(); |
373 | |
} |
374 | 0 | catch (IOException e) |
375 | |
{ |
376 | 0 | throw new MuleRuntimeException( |
377 | |
MessageFactory.createStaticMessage("Unable to create a canonical file for " + uri), |
378 | |
e); |
379 | |
} |
380 | |
} |
381 | |
|
382 | |
|
383 | |
|
384 | |
|
385 | |
|
386 | |
|
387 | |
|
388 | |
|
389 | |
|
390 | |
|
391 | |
|
392 | |
public static File newFile(File parent, String child) |
393 | |
{ |
394 | |
try |
395 | |
{ |
396 | 3074 | return new File(parent, child).getCanonicalFile(); |
397 | |
} |
398 | 0 | catch (IOException e) |
399 | |
{ |
400 | 0 | throw new MuleRuntimeException( |
401 | |
MessageFactory.createStaticMessage("Unable to create a canonical file for parent: " |
402 | |
+ parent + " and child: " + child), |
403 | |
e); |
404 | |
} |
405 | |
} |
406 | |
|
407 | |
|
408 | |
|
409 | |
|
410 | |
|
411 | |
|
412 | |
|
413 | |
|
414 | |
|
415 | |
|
416 | |
|
417 | |
public static File newFile(String parent, String child) |
418 | |
{ |
419 | |
try |
420 | |
{ |
421 | 16 | return new File(parent, child).getCanonicalFile(); |
422 | |
} |
423 | 0 | catch (IOException e) |
424 | |
{ |
425 | 0 | throw new MuleRuntimeException( |
426 | |
MessageFactory.createStaticMessage("Unable to create a canonical file for parent: " |
427 | |
+ parent + " and child: " + child), |
428 | |
e); |
429 | |
} |
430 | |
} |
431 | |
|
432 | |
|
433 | |
|
434 | |
|
435 | |
|
436 | |
|
437 | |
|
438 | |
|
439 | |
|
440 | |
|
441 | |
|
442 | |
public static void extractResources(String resourceName, Class callingClass, File outputDir, boolean keepParentDirectory) throws IOException |
443 | |
{ |
444 | 16 | URL url = callingClass.getClassLoader().getResource(resourceName); |
445 | 16 | URLConnection connection = url.openConnection(); |
446 | 16 | if (connection instanceof JarURLConnection) |
447 | |
{ |
448 | 6 | extractJarResources((JarURLConnection) connection, outputDir, keepParentDirectory); |
449 | |
} |
450 | |
else |
451 | |
{ |
452 | 10 | extractFileResources(normalizeFilePath(url, |
453 | |
MuleServer.getMuleContext().getConfiguration().getDefaultEncoding()), |
454 | |
outputDir, resourceName, keepParentDirectory); |
455 | |
} |
456 | 16 | } |
457 | |
|
458 | |
|
459 | |
|
460 | |
|
461 | |
|
462 | |
|
463 | |
|
464 | |
|
465 | |
|
466 | |
|
467 | |
private static void extractFileResources(String path, File outputDir, String resourceName, boolean keepParentDirectory) throws IOException |
468 | |
{ |
469 | 10 | File file = FileUtils.newFile(path); |
470 | 10 | if (!file.exists()) |
471 | |
{ |
472 | 0 | throw new IOException("The resource by path " + path + " "); |
473 | |
} |
474 | 10 | if (file.isDirectory()) |
475 | |
{ |
476 | 6 | if (keepParentDirectory) |
477 | |
{ |
478 | 2 | outputDir = FileUtils.newFile(outputDir.getPath() + File.separator + resourceName); |
479 | 2 | if (!outputDir.exists()) |
480 | |
{ |
481 | 2 | outputDir.mkdirs(); |
482 | |
} |
483 | |
} |
484 | |
else |
485 | |
{ |
486 | 4 | outputDir = FileUtils.newFile(outputDir.getPath()); |
487 | |
} |
488 | 6 | copyDirectory(file, outputDir); |
489 | |
} |
490 | |
else |
491 | |
{ |
492 | |
|
493 | 4 | if (keepParentDirectory) |
494 | |
{ |
495 | 2 | outputDir = FileUtils.newFile(outputDir.getPath() + File.separator + resourceName); |
496 | |
} |
497 | |
else |
498 | |
{ |
499 | 2 | outputDir = FileUtils.newFile(outputDir.getPath() + File.separator + file.getName()); |
500 | |
} |
501 | 4 | copyFile(file, outputDir); |
502 | |
} |
503 | 10 | } |
504 | |
|
505 | |
|
506 | |
|
507 | |
|
508 | |
|
509 | |
|
510 | |
|
511 | |
|
512 | |
|
513 | |
private static void extractJarResources(JarURLConnection connection, File outputDir, boolean keepParentDirectory) throws IOException |
514 | |
{ |
515 | 6 | JarFile jarFile = connection.getJarFile(); |
516 | 6 | JarEntry jarResource = connection.getJarEntry(); |
517 | 6 | Enumeration entries = jarFile.entries(); |
518 | 6 | InputStream inputStream = null; |
519 | 6 | OutputStream outputStream = null; |
520 | 6 | int jarResourceNameLenght = jarResource.getName().length(); |
521 | 750 | for (; entries.hasMoreElements();) |
522 | |
{ |
523 | 744 | JarEntry entry = (JarEntry) entries.nextElement(); |
524 | 744 | if (entry.getName().startsWith(jarResource.getName())) |
525 | |
{ |
526 | |
|
527 | 8 | String path = outputDir.getPath() + File.separator + entry.getName(); |
528 | |
|
529 | |
|
530 | 8 | if (!keepParentDirectory) |
531 | |
{ |
532 | 2 | if (entry.isDirectory()) |
533 | |
{ |
534 | 0 | if (entry.getName().equals(jarResource.getName())) |
535 | |
{ |
536 | 0 | continue; |
537 | |
} |
538 | 0 | path = outputDir.getPath() + File.separator + entry.getName().substring(jarResourceNameLenght, entry.getName().length()); |
539 | |
} |
540 | |
else |
541 | |
{ |
542 | 2 | if (entry.getName().length() > jarResourceNameLenght) |
543 | |
{ |
544 | 0 | path = outputDir.getPath() + File.separator + entry.getName().substring(jarResourceNameLenght, entry.getName().length()); |
545 | |
} |
546 | |
else |
547 | |
{ |
548 | 2 | path = outputDir.getPath() + File.separator + entry.getName().substring(entry.getName().lastIndexOf("/"), entry.getName().length()); |
549 | |
} |
550 | |
} |
551 | |
} |
552 | |
|
553 | 8 | File file = FileUtils.newFile(path); |
554 | 8 | if (!file.getParentFile().exists()) |
555 | |
{ |
556 | 2 | if (!file.getParentFile().mkdirs()) |
557 | |
{ |
558 | 0 | throw new IOException("Could not create directory: " + file.getParentFile()); |
559 | |
} |
560 | |
} |
561 | 8 | if (entry.isDirectory()) |
562 | |
{ |
563 | 2 | if (!file.exists() && !file.mkdirs()) |
564 | |
{ |
565 | 0 | throw new IOException("Could not create directory: " + file); |
566 | |
} |
567 | |
|
568 | |
} |
569 | |
else |
570 | |
{ |
571 | |
try |
572 | |
{ |
573 | 6 | inputStream = jarFile.getInputStream(entry); |
574 | 6 | outputStream = new BufferedOutputStream(new FileOutputStream(file)); |
575 | 6 | IOUtils.copy(inputStream, outputStream); |
576 | |
} |
577 | |
finally |
578 | |
{ |
579 | 6 | IOUtils.closeQuietly(inputStream); |
580 | 6 | IOUtils.closeQuietly(outputStream); |
581 | 6 | } |
582 | |
} |
583 | |
|
584 | |
} |
585 | 744 | } |
586 | 6 | } |
587 | |
|
588 | |
public static boolean renameFileHard(String srcFilePath, String destFilePath) |
589 | |
{ |
590 | 0 | if (StringUtils.isNotBlank(srcFilePath) && StringUtils.isNotBlank(destFilePath)) |
591 | |
{ |
592 | 0 | return renameFileHard(new File(srcFilePath), new File(destFilePath)); |
593 | |
} |
594 | |
else |
595 | |
{ |
596 | 0 | return false; |
597 | |
} |
598 | |
} |
599 | |
|
600 | |
public static boolean renameFileHard(File srcFile, File destFile) |
601 | |
{ |
602 | 0 | boolean isRenamed = false; |
603 | 0 | if (srcFile != null && destFile != null) |
604 | |
{ |
605 | 0 | logger.debug("Moving file " + srcFile.getAbsolutePath() + " to " + destFile.getAbsolutePath()); |
606 | 0 | if (!destFile.exists()) |
607 | |
{ |
608 | |
try |
609 | |
{ |
610 | 0 | if (srcFile.isFile()) |
611 | |
{ |
612 | 0 | logger.debug("Trying to rename file"); |
613 | 0 | FileInputStream in = null; |
614 | 0 | FileOutputStream out = null; |
615 | |
try |
616 | |
{ |
617 | 0 | in = new FileInputStream(srcFile); |
618 | 0 | out = new FileOutputStream(destFile); |
619 | 0 | out.getChannel().transferFrom(in.getChannel(), 0, srcFile.length()); |
620 | 0 | isRenamed = true; |
621 | |
} |
622 | 0 | catch (Exception e) |
623 | |
{ |
624 | 0 | logger.debug(e); |
625 | |
} |
626 | |
finally |
627 | |
{ |
628 | 0 | if (in != null) |
629 | |
{ |
630 | |
try |
631 | |
{ |
632 | 0 | in.close(); |
633 | |
} |
634 | 0 | catch (Exception inNotClosed) |
635 | |
{ |
636 | 0 | logger.debug(inNotClosed); |
637 | 0 | } |
638 | |
} |
639 | 0 | if (out != null) |
640 | |
{ |
641 | |
try |
642 | |
{ |
643 | 0 | out.close(); |
644 | |
} |
645 | 0 | catch (Exception outNotClosed) |
646 | |
{ |
647 | 0 | logger.debug(outNotClosed); |
648 | 0 | } |
649 | |
} |
650 | |
} |
651 | 0 | logger.debug("File renamed: " + isRenamed); |
652 | 0 | if (isRenamed) |
653 | |
{ |
654 | 0 | srcFile.delete(); |
655 | |
} |
656 | |
else |
657 | |
{ |
658 | 0 | destFile.delete(); |
659 | |
} |
660 | 0 | } |
661 | |
else |
662 | |
{ |
663 | 0 | logger.debug(srcFile.getAbsolutePath() + " is not a valid file."); |
664 | |
} |
665 | |
} |
666 | 0 | catch (Exception e) |
667 | |
{ |
668 | 0 | logger.debug("Error renaming file from " + srcFile.getAbsolutePath() + " to " + destFile.getAbsolutePath()); |
669 | 0 | } |
670 | |
} |
671 | |
else |
672 | |
{ |
673 | 0 | logger.debug("Error renaming file " + srcFile.getAbsolutePath() + ". Destination file " + destFile.getAbsolutePath() + " already exists."); |
674 | |
} |
675 | |
} |
676 | 0 | return isRenamed; |
677 | |
} |
678 | |
|
679 | |
public static boolean renameFile(String srcFilePath, String destFilePath) |
680 | |
{ |
681 | 0 | if (StringUtils.isNotBlank(srcFilePath) && StringUtils.isNotBlank(destFilePath)) |
682 | |
{ |
683 | 0 | return renameFile(new File(srcFilePath), new File(destFilePath)); |
684 | |
} |
685 | |
else |
686 | |
{ |
687 | 0 | return false; |
688 | |
} |
689 | |
} |
690 | |
|
691 | |
public static boolean renameFile(File srcFile, File destFile) |
692 | |
{ |
693 | 6 | boolean isRenamed = false; |
694 | 6 | if (srcFile != null && destFile != null) |
695 | |
{ |
696 | 6 | logger.debug("Moving file " + srcFile.getAbsolutePath() + " to " + destFile.getAbsolutePath()); |
697 | 6 | if (!destFile.exists()) |
698 | |
{ |
699 | |
try |
700 | |
{ |
701 | 6 | if (srcFile.isFile()) |
702 | |
{ |
703 | 6 | logger.debug("Trying to rename file"); |
704 | 6 | isRenamed = srcFile.renameTo(destFile); |
705 | 6 | if (!isRenamed && srcFile.exists()) |
706 | |
{ |
707 | 0 | logger.debug("Trying hard copy, assuming partition crossing ..."); |
708 | 0 | isRenamed = renameFileHard(srcFile, destFile); |
709 | |
} |
710 | 6 | logger.debug("File renamed: " + isRenamed); |
711 | |
} |
712 | |
else |
713 | |
{ |
714 | 0 | logger.debug(srcFile.getAbsolutePath() + " is not a valid file"); |
715 | |
} |
716 | |
} |
717 | 0 | catch (Exception e) |
718 | |
{ |
719 | 0 | logger.debug("Error moving file from " + srcFile.getAbsolutePath() + " to " + destFile.getAbsolutePath(), e); |
720 | 6 | } |
721 | |
} |
722 | |
else |
723 | |
{ |
724 | 0 | logger.debug("Error renaming file " + srcFile.getAbsolutePath() + ". Destination file " + destFile.getAbsolutePath() + " already exists."); |
725 | |
} |
726 | |
} |
727 | |
else |
728 | |
{ |
729 | 0 | logger.debug("Error renaming file. Source or destination file is null."); |
730 | |
} |
731 | |
|
732 | 6 | return isRenamed; |
733 | |
} |
734 | |
|
735 | |
|
736 | |
public static boolean moveFile(File sourceFile, File destinationFile) |
737 | |
{ |
738 | |
|
739 | 0 | boolean success = sourceFile.renameTo(destinationFile); |
740 | |
|
741 | 0 | if (!success) |
742 | |
{ |
743 | |
|
744 | 0 | FileInputStream fis = null; |
745 | 0 | FileOutputStream fos = null; |
746 | |
try |
747 | |
{ |
748 | 0 | fis = new FileInputStream(sourceFile); |
749 | 0 | fos = new FileOutputStream(destinationFile); |
750 | 0 | FileChannel srcChannel = fis.getChannel(); |
751 | 0 | FileChannel dstChannel = fos.getChannel(); |
752 | 0 | dstChannel.transferFrom(srcChannel, 0, srcChannel.size()); |
753 | 0 | srcChannel.close(); |
754 | 0 | dstChannel.close(); |
755 | 0 | success = sourceFile.delete(); |
756 | |
} |
757 | 0 | catch (IOException ioex) |
758 | |
{ |
759 | |
|
760 | 0 | success = false; |
761 | |
} |
762 | |
finally |
763 | |
{ |
764 | 0 | IOUtils.closeQuietly(fis); |
765 | 0 | IOUtils.closeQuietly(fos); |
766 | 0 | } |
767 | |
} |
768 | |
|
769 | 0 | return success; |
770 | |
} |
771 | |
|
772 | |
} |