46abaa5d63c54ccab4a833ed95ffe87a6fd0dfaf
[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 import java.io.UnsupportedEncodingException;
18
19 public class IOHelper {
20
21         /**
22          * Deletes files and directories recursively. WARNING!!! Follows symlinks!!!
23          */
24         public static void deleteRecursively(final File file) throws IOException {
25                 if (file.isDirectory()) {
26
27                         for (final File subFile : file.listFiles())
28                                 deleteRecursively(subFile);
29
30                         if (!file.delete())
31                                 throw new FileNotFoundException("Failed to delete directory: "
32                                                 + file);
33
34                         return;
35                 }
36
37                 if (file.isFile())
38                         if (!file.delete())
39                                 throw new FileNotFoundException("Failed to delete file: "
40                                                 + file);
41         }
42
43         public static byte[] getFileContents(final File file)
44                         throws FileNotFoundException, IOException {
45
46                 final byte[] result = new byte[(int) file.length()];
47                 final FileInputStream fileInputStream = new FileInputStream(file);
48                 fileInputStream.read(result);
49                 fileInputStream.close();
50                 return result;
51         }
52
53         public static String getFileContentsAsString(final File file)
54                         throws FileNotFoundException, IOException {
55                 try {
56                         return new String(getFileContents(file), "UTF-8");
57                 } catch (final UnsupportedEncodingException exception) {
58                         throw new RuntimeException(exception);
59                 }
60         }
61
62         /**
63          * Compares new file content with old file content. If content in equal,
64          * then leaves file as-is. If content differs, then overrides file with the
65          * new content.
66          *
67          * @return <code>true</code> if file was overwritten.
68          */
69         public static boolean overwriteFileIfContentDiffers(final File file,
70                         final byte[] newContent) throws FileNotFoundException, IOException {
71
72                 checkForEquality: {
73                         if (file.length() == newContent.length) {
74
75                                 final byte[] oldContent = getFileContents(file);
76
77                                 for (int i = 0; i < newContent.length; i++)
78                                         if (newContent[i] != oldContent[i])
79                                                 break checkForEquality;
80
81                                 // new file content in identical to old content
82                                 return false;
83                         }
84                 }
85
86                 // New content differs from existing. Overwrite file.
87                 saveToFile(file, newContent);
88                 return true;
89         }
90
91         public static void saveToFile(final File file, final byte[] content)
92                         throws IOException {
93                 final FileOutputStream fos = new FileOutputStream(file);
94                 fos.write(content);
95                 fos.close();
96         }
97
98 }