fix forceResetableStream
[nikiroo-utils.git] / src / be / nikiroo / utils / IOUtils.java
index 3e2f6e761481fbf7c0e741539c7f811dbf272daa..c7cbe259b5abab2e0d65d7639b83641eddd7a357 100644 (file)
@@ -17,7 +17,7 @@ import java.util.zip.ZipInputStream;
 import java.util.zip.ZipOutputStream;
 
 /**
- * This class offer some utilities based around Streams.
+ * This class offer some utilities based around Streams and Files.
  * 
  * @author niki
  */
@@ -213,7 +213,23 @@ public class IOUtils {
                        dir.mkdirs();
                }
 
-               FileWriter writerVersion = new FileWriter(new File(dir, filename));
+               writeSmallFile(new File(dir, filename), content);
+       }
+
+       /**
+        * Write the {@link String} content to {@link File}.
+        * 
+        * @param file
+        *            the {@link File} to write
+        * @param content
+        *            the content
+        * 
+        * @throws IOException
+        *             in case of I/O error
+        */
+       public static void writeSmallFile(File file, String content)
+                       throws IOException {
+               FileWriter writerVersion = new FileWriter(file);
                try {
                        writerVersion.write(content);
                } finally {
@@ -380,9 +396,6 @@ public class IOUtils {
         */
        public static InputStream forceResetableStream(InputStream in)
                        throws IOException {
-               MarkableFileInputStream tmpIn = null;
-               File tmp = null;
-
                boolean resetable = in.markSupported();
                if (resetable) {
                        try {
@@ -396,19 +409,32 @@ public class IOUtils {
                        return in;
                }
 
-               tmp = File.createTempFile(".tmp-stream", ".tmp");
+               final File tmp = File.createTempFile(".tmp-stream.", ".tmp");
                try {
                        write(in, tmp);
-                       tmpIn = new MarkableFileInputStream(new FileInputStream(tmp));
-                       return tmpIn;
-               } finally {
-                       try {
-                               if (tmpIn != null) {
-                                       tmpIn.close();
+                       in.close();
+
+                       final FileInputStream fis = new FileInputStream(tmp);
+                       return new MarkableFileInputStream(fis) {
+                               @Override
+                               public void close() throws IOException {
+                                       try {
+                                               try {
+                                                       super.close();
+                                               } finally {
+                                                       try {
+                                                               fis.close();
+                                                       } catch (IOException e) {
+                                                       }
+                                               }
+                                       } finally {
+                                               tmp.delete();
+                                       }
                                }
-                       } finally {
-                               tmp.delete();
-                       }
+                       };
+               } catch (IOException e) {
+                       tmp.delete();
+                       throw e;
                }
        }
 
@@ -432,4 +458,24 @@ public class IOUtils {
 
                return array;
        }
+
+       /**
+        * Convert the {@link File} into a byte array.
+        * 
+        * @param file
+        *            the input {@link File}
+        * 
+        * @return the array
+        * 
+        * @throws IOException
+        *             in case of I/O error
+        */
+       public static byte[] toByteArray(File file) throws IOException {
+               FileInputStream fis = new FileInputStream(file);
+               try {
+                       return toByteArray(fis);
+               } finally {
+                       fis.close();
+               }
+       }
 }