commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r1535219 - /commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/util/IoUtils.java
Date Thu, 24 Oct 2013 00:09:05 GMT
Author: ggregory
Date: Thu Oct 24 00:09:05 2013
New Revision: 1535219

URL: http://svn.apache.org/r1535219
Log:
Sort members in AB order.

Modified:
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/util/IoUtils.java

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/util/IoUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/util/IoUtils.java?rev=1535219&r1=1535218&r2=1535219&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/util/IoUtils.java
(original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/util/IoUtils.java
Thu Oct 24 00:09:05 2013
@@ -31,42 +31,49 @@ import java.nio.channels.FileChannel;
 import org.apache.commons.imaging.ImagingConstants;
 
 public class IoUtils implements ImagingConstants {
-    /**
-     * This class should never be instantiated.
-     */
-    private IoUtils() {
-    }
-
-    /**
-     * Reads an InputStream to the end.
-     * <p>
-     * 
-     * @param is
-     *            The InputStream to read.
-     * @return A byte array containing the contents of the InputStream
-     * @see InputStream
-     */
-    public static byte[] getInputStreamBytes(InputStream is) throws IOException {
-        ByteArrayOutputStream os = null;
-
+    public static final boolean copyFileNio(final File src, final File dst)
+            throws IOException {
+        FileChannel srcChannel = null, dstChannel = null;
         try {
-            os = new ByteArrayOutputStream(4096);
+            // Create channel on the source
+            srcChannel = new FileInputStream(src).getChannel();
 
-            is = new BufferedInputStream(is);
+            // Create channel on the destination
+            dstChannel = new FileOutputStream(dst).getChannel();
 
-            int count;
-            final byte[] buffer = new byte[4096];
-            while ((count = is.read(buffer, 0, 4096)) > 0) {
-                os.write(buffer, 0, count);
+            // // Copy file contents from source to destination
+            // dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
+
+            {
+                // long theoretical_max = (64 * 1024 * 1024) - (32 * 1024);
+                final int safe_max = (64 * 1024 * 1024) / 4;
+                final long size = srcChannel.size();
+                long position = 0;
+                while (position < size) {
+                    position += srcChannel.transferTo(position, safe_max,
+                            dstChannel);
+                }
             }
 
-            os.flush();
+            // Close the channels
+            srcChannel.close();
+            srcChannel = null;
+            dstChannel.close();
+            dstChannel = null;
 
-            return os.toByteArray();
+            return true;
         } finally {
             try {
-                if (os != null) {
-                    os.close();
+                if (srcChannel != null) {
+                    srcChannel.close();
+                }
+            } catch (final IOException e) {
+                Debug.debug(e);
+
+            }
+            try {
+                if (dstChannel != null) {
+                    dstChannel.close();
                 }
             } catch (final IOException e) {
                 Debug.debug(e);
@@ -74,6 +81,47 @@ public class IoUtils implements ImagingC
         }
     }
 
+    public static void copyStreamToStream(final InputStream src, final OutputStream dst)
+            throws IOException {
+        copyStreamToStream(src, dst, true);
+    }
+
+    public static void copyStreamToStream(final InputStream src, final OutputStream dst,
+            final boolean close_streams) throws IOException {
+        BufferedInputStream bis = null;
+        BufferedOutputStream bos = null;
+
+        try {
+            bis = new BufferedInputStream(src);
+            bos = new BufferedOutputStream(dst);
+
+            int count;
+            final byte[] buffer = new byte[4096];
+            while ((count = bis.read(buffer, 0, buffer.length)) > 0) {
+                dst.write(buffer, 0, count);
+            }
+            bos.flush();
+        } finally {
+            if (close_streams) {
+                try {
+                    if (bis != null) {
+                        bis.close();
+                    }
+                } catch (final IOException e) {
+                    Debug.debug(e);
+                }
+                try {
+                    if (bos != null) {
+                        bos.close();
+                    }
+                } catch (final IOException e) {
+                    Debug.debug(e);
+                }
+            }
+        }
+
+    }
+
     /**
      * Reads a File into memory.
      * <p>
@@ -101,21 +149,39 @@ public class IoUtils implements ImagingC
         }
     }
 
-    public static void writeToFile(final byte[] src, final File file) throws IOException
{
-        ByteArrayInputStream stream = null;
+    /**
+     * Reads an InputStream to the end.
+     * <p>
+     * 
+     * @param is
+     *            The InputStream to read.
+     * @return A byte array containing the contents of the InputStream
+     * @see InputStream
+     */
+    public static byte[] getInputStreamBytes(InputStream is) throws IOException {
+        ByteArrayOutputStream os = null;
 
         try {
-            stream = new ByteArrayInputStream(src);
+            os = new ByteArrayOutputStream(4096);
 
-            putInputStreamToFile(stream, file);
+            is = new BufferedInputStream(is);
+
+            int count;
+            final byte[] buffer = new byte[4096];
+            while ((count = is.read(buffer, 0, 4096)) > 0) {
+                os.write(buffer, 0, count);
+            }
+
+            os.flush();
+
+            return os.toByteArray();
         } finally {
             try {
-                if (stream != null) {
-                    stream.close();
+                if (os != null) {
+                    os.close();
                 }
-            } catch (final Exception e) {
+            } catch (final IOException e) {
                 Debug.debug(e);
-
             }
         }
     }
@@ -144,95 +210,29 @@ public class IoUtils implements ImagingC
         }
     }
 
-    public static void copyStreamToStream(final InputStream src, final OutputStream dst)
-            throws IOException {
-        copyStreamToStream(src, dst, true);
-    }
-
-    public static void copyStreamToStream(final InputStream src, final OutputStream dst,
-            final boolean close_streams) throws IOException {
-        BufferedInputStream bis = null;
-        BufferedOutputStream bos = null;
-
-        try {
-            bis = new BufferedInputStream(src);
-            bos = new BufferedOutputStream(dst);
-
-            int count;
-            final byte[] buffer = new byte[4096];
-            while ((count = bis.read(buffer, 0, buffer.length)) > 0) {
-                dst.write(buffer, 0, count);
-            }
-            bos.flush();
-        } finally {
-            if (close_streams) {
-                try {
-                    if (bis != null) {
-                        bis.close();
-                    }
-                } catch (final IOException e) {
-                    Debug.debug(e);
-                }
-                try {
-                    if (bos != null) {
-                        bos.close();
-                    }
-                } catch (final IOException e) {
-                    Debug.debug(e);
-                }
-            }
-        }
-
-    }
+    public static void writeToFile(final byte[] src, final File file) throws IOException
{
+        ByteArrayInputStream stream = null;
 
-    public static final boolean copyFileNio(final File src, final File dst)
-            throws IOException {
-        FileChannel srcChannel = null, dstChannel = null;
         try {
-            // Create channel on the source
-            srcChannel = new FileInputStream(src).getChannel();
-
-            // Create channel on the destination
-            dstChannel = new FileOutputStream(dst).getChannel();
-
-            // // Copy file contents from source to destination
-            // dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
-
-            {
-                // long theoretical_max = (64 * 1024 * 1024) - (32 * 1024);
-                final int safe_max = (64 * 1024 * 1024) / 4;
-                final long size = srcChannel.size();
-                long position = 0;
-                while (position < size) {
-                    position += srcChannel.transferTo(position, safe_max,
-                            dstChannel);
-                }
-            }
-
-            // Close the channels
-            srcChannel.close();
-            srcChannel = null;
-            dstChannel.close();
-            dstChannel = null;
+            stream = new ByteArrayInputStream(src);
 
-            return true;
+            putInputStreamToFile(stream, file);
         } finally {
             try {
-                if (srcChannel != null) {
-                    srcChannel.close();
+                if (stream != null) {
+                    stream.close();
                 }
-            } catch (final IOException e) {
+            } catch (final Exception e) {
                 Debug.debug(e);
 
             }
-            try {
-                if (dstChannel != null) {
-                    dstChannel.close();
-                }
-            } catch (final IOException e) {
-                Debug.debug(e);
-            }
         }
     }
 
+    /**
+     * This class should never be instantiated.
+     */
+    private IoUtils() {
+    }
+
 }



Mime
View raw message