fix forceResetableStream
[nikiroo-utils.git] / src / be / nikiroo / utils / IOUtils.java
index 0d9bc378a83b856599eae7f2afaec30fa3711332..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
  */
@@ -396,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 {
@@ -412,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;
                }
        }
 
@@ -448,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();
+               }
+       }
 }