svn commit: r1040798 - /ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilIO.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r1040798 - /ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilIO.java

doogie-3
Author: doogie
Date: Tue Nov 30 22:03:40 2010
New Revision: 1040798

URL: http://svn.apache.org/viewvc?rev=1040798&view=rev
Log:
Several utility methods for copying streams and
readers/writers/appendable.

Modified:
    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilIO.java

Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilIO.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilIO.java?rev=1040798&r1=1040797&r2=1040798&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilIO.java (original)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilIO.java Tue Nov 30 22:03:40 2010
@@ -38,6 +38,7 @@ import java.io.StringReader;
 import java.io.StringWriter;
 import java.io.Writer;
 import java.nio.ByteBuffer;
+import java.nio.CharBuffer;
 import java.nio.charset.Charset;
 
 import org.apache.commons.io.IOUtils;
@@ -52,6 +53,69 @@ public final class UtilIO {
     public static final Charset UTF8 = Charset.forName("UTF-8");
     public static final String module = UtilIO.class.getName();
 
+    /** Copy an InputStream to an OutputStream, optionally closing either
+     *  the input or the output.
+     *
+     * @param in the InputStream to copy from
+     * @param closeIn whether to close the input when the copy is done
+     * @param out the OutputStream to copy to
+     * @param closeOut whether to close the output when the copy is done
+     * @throws IOException if an error occurs
+     */
+    public static void copy(InputStream in, boolean closeIn, OutputStream out, boolean closeOut) throws IOException {
+        try {
+            try {
+                IOUtils.copy(in, out);
+            } finally {
+                if (closeIn) IOUtils.closeQuietly(in);
+            }
+        } finally {
+            if (closeOut) IOUtils.closeQuietly(out);
+        }
+    }
+
+    /** Copy a Reader to a Writer, optionally closing either the input or
+     *  the output.
+     *
+     * @param reader the Reader to copy from
+     * @param closeIn whether to close the input when the copy is done
+     * @param writer the Writer to copy to
+     * @param closeOut whether to close the output when the copy is done
+     * @throws IOException if an error occurs
+     */
+    public static void copy(Reader reader, boolean closeIn, Writer writer, boolean closeOut) throws IOException {
+        try {
+            try {
+                IOUtils.copy(reader, writer);
+            } finally {
+                if (closeIn) IOUtils.closeQuietly(reader);
+            }
+        } finally {
+            if (closeOut) IOUtils.closeQuietly(writer);
+        }
+    }
+
+    /** Copy a Reader to an Appendable, optionally closing the input.
+     *
+     * @param reader the Reader to copy from
+     * @param closeIn whether to close the input when the copy is done
+     * @param out the Appendable to copy to
+     * @throws IOException if an error occurs
+     */
+    public static void copy(Reader reader, boolean closeIn, Appendable out) throws IOException {
+        try {
+            CharBuffer buffer = CharBuffer.allocate(4096);
+            int r;
+            while ((r = reader.read(buffer)) != -1) {
+                buffer.rewind();
+                out.append(buffer);
+                buffer.flip();
+            }
+        } finally {
+            if (closeIn) IOUtils.closeQuietly(reader);
+        }
+    }
+
     /** Convert a byte array to a string; consistently uses \n line endings
      * in java.  This uses a default {@link Charset UTF-8} charset.
      *