IOUtils: better buffer size
[nikiroo-utils.git] / src / be / nikiroo / utils / IOUtils.java
index e9a378c0fd6e279c1568afb848808ab83f9cdf5e..a263eb1ea17ea59e5a5a605c2cde91337aefe6ac 100644 (file)
@@ -55,7 +55,7 @@ public class IOUtils {
         */
        public static void write(InputStream in, OutputStream out)
                        throws IOException {
-               byte buffer[] = new byte[4069];
+               byte buffer[] = new byte[4096];
                for (int len = 0; (len = in.read(buffer)) > 0;) {
                        out.write(buffer, 0, len);
                }
@@ -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;
                }
        }