Version 1.4.2: bugfixes (unhtml, deltree)
[nikiroo-utils.git] / src / be / nikiroo / utils / IOUtils.java
index 92e312874599c6652454551bdd096cbc7b531226..4a185c6386ba31b84a474540f82d78aa25d691dc 100644 (file)
@@ -202,20 +202,15 @@ public class IOUtils {
         *            the target to delete
         */
        public static void deltree(File target) {
-               for (File file : target.listFiles()) {
-                       if (file.isDirectory()) {
+               File[] files = target.listFiles();
+               if (files != null) {
+                       for (File file : files) {
                                deltree(file);
-                       } else {
-                               if (!file.delete()) {
-                                       System.err.println("Cannot delete file: "
-                                                       + file.getAbsolutePath());
-                               }
                        }
                }
 
                if (!target.delete()) {
-                       System.err.println("Cannot delete file: "
-                                       + target.getAbsolutePath());
+                       System.err.println("Cannot delete: " + target.getAbsolutePath());
                }
        }
 
@@ -232,7 +227,7 @@ public class IOUtils {
         * @throws IOException
         *             in case of IO error
         */
-       static public BufferedImage toImage(InputStream in) throws IOException {
+       public static BufferedImage toImage(InputStream in) throws IOException {
                MarkableFileInputStream tmpIn = null;
                File tmp = null;
                try {
@@ -329,6 +324,23 @@ public class IOUtils {
                return image;
        }
 
+       /**
+        * Open the given /-separated resource (from the binary root).
+        * 
+        * @param name
+        *            the resource name
+        * 
+        * @return the opened resource if found, NLL if not
+        */
+       public static InputStream openResource(String name) {
+               ClassLoader loader = IOUtils.class.getClassLoader();
+               if (loader == null) {
+                       loader = ClassLoader.getSystemClassLoader();
+               }
+
+               return loader.getResourceAsStream(name);
+       }
+
        /**
         * Return the EXIF transformation flag of this image if any.
         * 
@@ -344,7 +356,7 @@ public class IOUtils {
         * @throws IOException
         *             in case of IO error
         */
-       static private int getExifTransorm(InputStream in) throws IOException {
+       private static int getExifTransorm(InputStream in) throws IOException {
                int[] exif_data = new int[100];
                int set_flag = 0;
                int is_motorola = 0;