Changed license to LGPLv3 or later.
[svjatoslav_commons.git] / src / main / java / eu / svjatoslav / commons / file / IOHelper.java
1 /*
2  * Svjatoslav Commons - shared library of common functionality.
3  * Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 3 of the GNU Lesser General Public License
7  * or later as published by the Free Software Foundation.
8  */
9
10 package eu.svjatoslav.commons.file;
11
12 import java.io.File;
13 import java.io.FileInputStream;
14 import java.io.FileNotFoundException;
15 import java.io.FileOutputStream;
16 import java.io.IOException;
17
18 public class IOHelper {
19
20         /**
21          * Deletes files and directories recursively. WARNING!!! Follows symlinks!!!
22          */
23         public static void deleteRecursively(final File file) throws IOException {
24                 if (file.isDirectory()) {
25
26                         for (final File subFile : file.listFiles())
27                                 deleteRecursively(subFile);
28
29                         if (!file.delete())
30                                 throw new FileNotFoundException("Failed to delete directory: "
31                                                 + file);
32
33                         return;
34                 }
35
36                 if (file.isFile())
37                         if (!file.delete())
38                                 throw new FileNotFoundException("Failed to delete file: "
39                                                 + file);
40         }
41
42         public static byte[] getFileContents(final File file)
43                         throws FileNotFoundException, IOException {
44
45                 final byte[] result = new byte[(int) file.length()];
46                 final FileInputStream fileInputStream = new FileInputStream(file);
47                 fileInputStream.read(result);
48                 fileInputStream.close();
49                 return result;
50         }
51
52         /**
53          * Compares new file content with old file content. If content in equal,
54          * then leaves file as-is. If content differs, then overrides file with the
55          * new content.
56          * 
57          * @return <code>true</code> if file was overwritten.
58          */
59         public static boolean overwriteFileIfContentDiffers(final File file,
60                         final byte[] newContent) throws FileNotFoundException, IOException {
61
62                 checkForEquality: {
63                         if (file.length() == newContent.length) {
64
65                                 final byte[] oldContent = getFileContents(file);
66
67                                 for (int i = 0; i < newContent.length; i++)
68                                         if (newContent[i] != oldContent[i])
69                                                 break checkForEquality;
70
71                                 // new file content in identical to old content
72                                 return false;
73                         }
74                 }
75
76                 // New content differs from existing. Overwrite file.
77                 saveToFile(file, newContent);
78                 return true;
79         }
80
81         public static void saveToFile(final File file, final byte[] content)
82                         throws IOException {
83                 final FileOutputStream fos = new FileOutputStream(file);
84                 fos.write(content);
85                 fos.close();
86         }
87
88 }