commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nia...@apache.org
Subject svn commit: r584746 - /commons/proper/io/trunk/src/java/org/apache/commons/io/input/TeeInputStream.java
Date Mon, 15 Oct 2007 11:37:16 GMT
Author: niallp
Date: Mon Oct 15 04:37:15 2007
New Revision: 584746

URL: http://svn.apache.org/viewvc?rev=584746&view=rev
Log:
IO-129 Add a configurable option to close the OutputStream - patch from Jukka Zitting

Modified:
    commons/proper/io/trunk/src/java/org/apache/commons/io/input/TeeInputStream.java

Modified: commons/proper/io/trunk/src/java/org/apache/commons/io/input/TeeInputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/java/org/apache/commons/io/input/TeeInputStream.java?rev=584746&r1=584745&r2=584746&view=diff
==============================================================================
--- commons/proper/io/trunk/src/java/org/apache/commons/io/input/TeeInputStream.java (original)
+++ commons/proper/io/trunk/src/java/org/apache/commons/io/input/TeeInputStream.java Mon Oct
15 04:37:15 2007
@@ -27,8 +27,9 @@
  * bytes from the input stream being skipped or duplicated in the output
  * stream.
  * <p>
- * Unlike the proxied input stream (that gets closed when {@link #close()}
- * is called), the associated output stream is never closed by this class. 
+ * The proxied input stream is closed when the {@link #close()} method is
+ * called on this proxy. It is configurable whether the associated output
+ * stream will also closed.
  *
  * @since Commons IO 1.4
  */
@@ -41,15 +42,56 @@
     private final OutputStream branch;
 
     /**
+     * Flag for closing also the associated output stream when this
+     * stream is closed.
+     */
+    private final boolean closeBranch;
+
+    /**
      * Creates a TeeInputStream that proxies the given {@link InputStream}
-     * and copies all read bytes to the given {@link OutputStream}.
+     * and copies all read bytes to the given {@link OutputStream}. The given
+     * output stream will not be closed when this stream gets closed.
      *
      * @param input input stream to be proxied
      * @param branch output stream that will receive a copy of all bytes read
      */
     public TeeInputStream(InputStream input, OutputStream branch) {
+        this(input, branch, false);
+    }
+
+    /**
+     * Creates a TeeInputStream that proxies the given {@link InputStream}
+     * and copies all read bytes to the given {@link OutputStream}. The given
+     * output stream will be closed when this stream gets closed if the
+     * closeBranch parameter is <code>true</code>.
+     *
+     * @param input input stream to be proxied
+     * @param branch output stream that will receive a copy of all bytes read
+     * @param closeBranch flag for closing also the output stream when this
+     *                    stream is closed
+     */
+    public TeeInputStream(
+            InputStream input, OutputStream branch, boolean closeBranch) {
         super(input);
         this.branch = branch;
+        this.closeBranch = closeBranch;
+    }
+
+    /**
+     * Closes the proxied input stream and, if so configured, the associated
+     * output stream. An exception thrown from one stream will not prevent
+     * closing of the other stream.
+     *
+     * @throws IOException if either of the streams could not be closed
+     */
+    public void close() throws IOException {
+        try {
+            super.close();
+        } finally {
+            if (closeBranch) {
+                branch.close();
+            }
+        }
     }
 
     /**



Mime
View raw message