Update of /var/cvs/src/org/mmbase/util
In directory james.mmbase.org:/tmp/cvs-serv10213

Modified Files:
        IOUtil.java 
Log Message:
Indentation (with 4 spaces, not 3). Javadoc. Using nio if copying between files


See also: http://cvs.mmbase.org/viewcvs/src/org/mmbase/util


Index: IOUtil.java
===================================================================
RCS file: /var/cvs/src/org/mmbase/util/IOUtil.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -b -r1.2 -r1.3
--- IOUtil.java 7 Apr 2009 08:23:27 -0000       1.2
+++ IOUtil.java 24 Apr 2009 12:15:20 -0000      1.3
@@ -10,21 +10,57 @@
 package org.mmbase.util;
 
 import java.io.*;
+import java.nio.channels.*;
+
+import org.mmbase.util.logging.*;
+
+/**
+ * Various utils to consisely and efficiently deal with streams
+ * @since MMBase-1.9.1
+ * @version $Id: IOUtil.java,v 1.3 2009/04/24 12:15:20 michiel Exp $
+ */
 
 public final class IOUtil {
 
+    private static final Logger log = Logging.getLoggerInstance(IOUtil.class);
+
    private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
 
    private IOUtil() {
-      // nothing
+        // nothing, only static methods.
    }
 
    /**
     * Copy bytes from an <code>InputStream</code> to an 
<code>OutputStream</code>.
     */
    public static long copy(final InputStream input, final OutputStream output) 
throws IOException {
+        if (input instanceof FileInputStream && output instanceof 
FileOutputStream) {
+            log.debug("Streams are for files, using nio.");
+            return copy((FileInputStream) input, (FileOutputStream) output);
+        } else {
       return copy(input, output, DEFAULT_BUFFER_SIZE);
    }
+    }
+
+    public static long copy(final FileInputStream input, final 
FileOutputStream output) throws IOException {
+        FileChannel source = null;
+        FileChannel destination = null;;
+        try {
+            source = input.getChannel();
+            destination = output.getChannel();
+            log.debug("Copying " + source.size() + " bytes");
+            destination.transferFrom(source, 0, source.size());
+            return source.size();
+        } finally {
+            if(source != null) {
+                source.close();
+            }
+            if(destination != null) {
+                destination.close();
+            }
+        }
+    }
+
 
    /**
     * Copy bytes from an <code>InputStream</code> to an 
<code>OutputStream</code>.
@@ -32,8 +68,7 @@
     * @param bufferSize
     *           Size of internal buffer to use.
     */
-   public static long copy(final InputStream input, final OutputStream output, 
final int bufferSize)
-         throws IOException {
+    public static long copy(final InputStream input, final OutputStream 
output, final int bufferSize) throws IOException {
       long size = 0;
       final byte[] buffer = new byte[bufferSize];
       int n = 0;
@@ -58,8 +93,7 @@
     * @param bufferSize
     *           Size of internal buffer to use.
     */
-   public static long copy(final Reader input, final Writer output, final int 
bufferSize)
-         throws IOException {
+    public static long copy(final Reader input, final Writer output, final int 
bufferSize) throws IOException {
       long size = 0;
       final char[] buffer = new char[bufferSize];
       int n = 0;
_______________________________________________
Cvs mailing list
[email protected]
http://lists.mmbase.org/mailman/listinfo/cvs

Reply via email to