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