Code cleanup and formatting. Migrated to java 1.8.
[svjatoslav_commons.git] / src / main / java / eu / svjatoslav / commons / file / IOHelper.java
old mode 100644 (file)
new mode 100755 (executable)
index 97d3b44..e83561a
 /*
  * Svjatoslav Commons - shared library of common functionality.
- * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- * 
+ * Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
+ *
  * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 2 of the GNU General Public License
- * as published by the Free Software Foundation.
+ * modify it under the terms of version 3 of the GNU Lesser General Public License
+ * or later as published by the Free Software Foundation.
  */
 
 package eu.svjatoslav.commons.file;
 
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.IOException;
+import java.io.*;
 
 public class IOHelper {
 
-       public static byte[] getFileContents(final File file)
-                       throws FileNotFoundException, IOException {
-
-               final byte[] result = new byte[(int) file.length()];
-               final FileInputStream fileInputStream = new FileInputStream(file);
-               fileInputStream.read(result);
-               fileInputStream.close();
-               return result;
-       }
-
-       /**
-        * Compares new file content with old file content. If content in equal,
-        * then leaves file as-is. If content differs, then overrides file with the
-        * new content.
-        * 
-        * @return <code>true</code> if file was overwritten.
-        */
-       public static boolean overwriteFileIfContentDiffers(final File file,
-                       final byte[] newContent) throws FileNotFoundException, IOException {
-
-               checkForEquality: {
-                       if (file.length() == newContent.length) {
-
-                               final byte[] oldContent = getFileContents(file);
-
-                               for (int i = 0; i < newContent.length; i++)
-                                       if (newContent[i] != oldContent[i])
-                                               break checkForEquality;
-
-                               // new file content in identical to old content
-                               return false;
-                       }
-               }
-
-               // New content differs from existing. Overwrite file.
-               saveToFile(file, newContent);
-               return true;
-       }
-
-       public static void saveToFile(final File file, final byte[] content)
-                       throws IOException {
-               final FileOutputStream fos = new FileOutputStream(file);
-               fos.write(content);
-               fos.close();
-       }
+    /**
+     * Deletes files and directories recursively. WARNING!!! Follows symlinks!!!
+     *
+     * @param file directory to delete with entire contents.
+     * @throws IOException if filesystem error happens
+     */
+    public static void deleteRecursively(final File file) throws IOException {
+        if (file.isDirectory()) {
+
+            File[] files = file.listFiles();
+            if (files == null)
+                throw new RuntimeException("Failed to read directory content for: "
+                        + file);
+
+            for (final File subFile : files)
+                deleteRecursively(subFile);
+
+            if (!file.delete())
+                throw new RuntimeException("Failed to delete directory: "
+                        + file);
+
+            return;
+        }
+
+        if (file.isFile())
+            if (!file.delete())
+                throw new FileNotFoundException("Failed to delete file: "
+                        + file);
+    }
+
+    public static byte[] getFileContents(final File file)
+            throws IOException {
+
+        final byte[] result = new byte[(int) file.length()];
+        try (final FileInputStream fileInputStream = new FileInputStream(file)) {
+            if (fileInputStream.read(result) != result.length)
+                throw new RuntimeException("Could not read file content:" + file);
+        }
+        return result;
+    }
+
+    public static String getFileContentsAsString(final File file)
+            throws IOException {
+        try {
+            return new String(getFileContents(file), "UTF-8");
+        } catch (final UnsupportedEncodingException exception) {
+            throw new RuntimeException(exception);
+        }
+    }
+
+    /**
+     * Compares new file content with old file content. If content in equal,
+     * then leaves file as-is. If content differs, then overrides file with the
+     * new content.
+     *
+     * @param file       file to potentially overwrite
+     * @param newContent new content
+     * @return <code>true</code> if file was overwritten.
+     * @throws FileNotFoundException if file is not found.
+     * @throws IOException           if error happens during file IO.
+     */
+    public static boolean overwriteFileIfContentDiffers(final File file,
+                                                        final byte[] newContent) throws IOException {
+
+        checkForEquality:
+        {
+            if (file.length() == newContent.length) {
+
+                final byte[] oldContent = getFileContents(file);
+
+                for (int i = 0; i < newContent.length; i++)
+                    if (newContent[i] != oldContent[i])
+                        break checkForEquality;
+
+                // new file content in identical to old content
+                return false;
+            }
+        }
+
+        // New content differs from existing. Overwrite file.
+        saveToFile(file, newContent);
+        return true;
+    }
+
+    public static void saveToFile(final File file, final byte[] content)
+            throws IOException {
+        try (final FileOutputStream fos = new FileOutputStream(file)) {
+            fos.write(content);
+        }
+    }
 
 }