improved javadoc
[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          * @param file
25          *            directory to delete with entire contents.
26          *
27          * @throws IOException
28          *             if filesystem error happens
29          */
30         public static void deleteRecursively(final File file) throws IOException {
31                 if (file.isDirectory()) {
32
33                         for (final File subFile : file.listFiles())
34                                 deleteRecursively(subFile);
35
36                         if (!file.delete())
37                                 throw new FileNotFoundException("Failed to delete directory: "
38                                                 + file);
39
40                         return;
41                 }
42
43                 if (file.isFile())
44                         if (!file.delete())
45                                 throw new FileNotFoundException("Failed to delete file: "
46                                                 + file);
47         }
48
49         public static byte[] getFileContents(final File file)
50                         throws FileNotFoundException, IOException {
51
52                 final byte[] result = new byte[(int) file.length()];
53                 final FileInputStream fileInputStream = new FileInputStream(file);
54                 fileInputStream.read(result);
55                 fileInputStream.close();
56                 return result;
57         }
58
59         public static String getFileContentsAsString(final File file)
60                         throws FileNotFoundException, IOException {
61                 try {
62                         return new String(getFileContents(file), "UTF-8");
63                 } catch (final UnsupportedEncodingException exception) {
64                         throw new RuntimeException(exception);
65                 }
66         }
67
68         /**
69          * Compares new file content with old file content. If content in equal,
70          * then leaves file as-is. If content differs, then overrides file with the
71          * new content.
72          *
73          * @param file
74          *            file to potentially overwrite
75          * @param newContent
76          *            new content
77          * @return <code>true</code> if file was overwritten.
78          *
79          * @throws FileNotFoundException
80          *             if file is not found.
81          * @throws IOException
82          *             if error happens during file IO.
83          */
84         public static boolean overwriteFileIfContentDiffers(final File file,
85                         final byte[] newContent) throws FileNotFoundException, IOException {
86
87                 checkForEquality: {
88                         if (file.length() == newContent.length) {
89
90                                 final byte[] oldContent = getFileContents(file);
91
92                                 for (int i = 0; i < newContent.length; i++)
93                                         if (newContent[i] != oldContent[i])
94                                                 break checkForEquality;
95
96                                 // new file content in identical to old content
97                                 return false;
98                         }
99                 }
100
101                 // New content differs from existing. Overwrite file.
102                 saveToFile(file, newContent);
103                 return true;
104         }
105
106         public static void saveToFile(final File file, final byte[] content)
107                         throws IOException {
108                 final FileOutputStream fos = new FileOutputStream(file);
109                 fos.write(content);
110                 fos.close();
111         }
112
113 }