You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ni...@apache.org on 2007/10/13 16:30:31 UTC

svn commit: r584407 - in /commons/proper/io/trunk: RELEASE-NOTES.txt src/java/org/apache/commons/io/input/TeeInputStream.java src/test/org/apache/commons/io/input/TeeInputStreamTest.java

Author: niallp
Date: Sat Oct 13 07:30:30 2007
New Revision: 584407

URL: http://svn.apache.org/viewvc?rev=584407&view=rev
Log:
IO-129 add new TeeInputStream implementation - thanks to Jukka Zitting

Added:
    commons/proper/io/trunk/src/java/org/apache/commons/io/input/TeeInputStream.java   (with props)
    commons/proper/io/trunk/src/test/org/apache/commons/io/input/TeeInputStreamTest.java   (with props)
Modified:
    commons/proper/io/trunk/RELEASE-NOTES.txt

Modified: commons/proper/io/trunk/RELEASE-NOTES.txt
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/RELEASE-NOTES.txt?rev=584407&r1=584406&r2=584407&view=diff
==============================================================================
--- commons/proper/io/trunk/RELEASE-NOTES.txt (original)
+++ commons/proper/io/trunk/RELEASE-NOTES.txt Sat Oct 13 07:30:30 2007
@@ -48,6 +48,9 @@
   - CloseShieldInputStream - prevents the underlying input stream from being closed.
   - CloseShieldOutputStream - prevents the underlying output stream from being closed.
 
+- TeeInputStream [IO-129]
+  - Add new Tee input stream implementation
+
 
 Feedback
 --------

Added: 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=584407&view=auto
==============================================================================
--- commons/proper/io/trunk/src/java/org/apache/commons/io/input/TeeInputStream.java (added)
+++ commons/proper/io/trunk/src/java/org/apache/commons/io/input/TeeInputStream.java Sat Oct 13 07:30:30 2007
@@ -0,0 +1,104 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.io.input;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+/**
+ * InputStream proxy that transparently writes a copy of all bytes read
+ * from the proxied stream to a given OutputStream. Using {@link #skip(long)}
+ * or {@link #mark(int)}/{@link #reset()} on the stream will result on some
+ * 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. 
+ *
+ * @since Commons IO 1.4
+ */
+public class TeeInputStream extends ProxyInputStream {
+
+    /**
+     * The output stream that will receive a copy of all bytes read from the
+     * proxied input stream.
+     */
+    private final OutputStream branch;
+
+    /**
+     * Creates a TeeInputStream that proxies the given {@link InputStream}
+     * and copies all read bytes to the given {@link OutputStream}.
+     *
+     * @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) {
+        super(input);
+        this.branch = branch;
+    }
+
+    /**
+     * Reads a single byte from the proxied input stream and writes it to
+     * the associated output stream.
+     *
+     * @return next byte from the stream, or -1 if the stream has ended
+     * @throws IOException if the stream could not be read (or written) 
+     */
+    public int read() throws IOException {
+        int ch = super.read();
+        if (ch != -1) {
+            branch.write(ch);
+        }
+        return ch;
+    }
+
+    /**
+     * Reads bytes from the proxied input stream and writes the read bytes
+     * to the associated output stream.
+     *
+     * @param bts byte buffer
+     * @param st start offset within the buffer
+     * @param end maximum number of bytes to read
+     * @return number of bytes read, or -1 if the stream has ended
+     * @throws IOException if the stream could not be read (or written) 
+     */
+    public int read(byte[] bts, int st, int end) throws IOException {
+        int n = super.read(bts, st, end);
+        if (n != -1) {
+            branch.write(bts, st, n);
+        }
+        return n;
+    }
+
+    /**
+     * Reads bytes from the proxied input stream and writes the read bytes
+     * to the associated output stream.
+     *
+     * @param bts byte buffer
+     * @return number of bytes read, or -1 if the stream has ended
+     * @throws IOException if the stream could not be read (or written) 
+     */
+    public int read(byte[] bts) throws IOException {
+        int n = super.read(bts);
+        if (n != -1) {
+            branch.write(bts, 0, n);
+        }
+        return n;
+    }
+
+}

Propchange: commons/proper/io/trunk/src/java/org/apache/commons/io/input/TeeInputStream.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/io/trunk/src/java/org/apache/commons/io/input/TeeInputStream.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: commons/proper/io/trunk/src/test/org/apache/commons/io/input/TeeInputStreamTest.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/org/apache/commons/io/input/TeeInputStreamTest.java?rev=584407&view=auto
==============================================================================
--- commons/proper/io/trunk/src/test/org/apache/commons/io/input/TeeInputStreamTest.java (added)
+++ commons/proper/io/trunk/src/test/org/apache/commons/io/input/TeeInputStreamTest.java Sat Oct 13 07:30:30 2007
@@ -0,0 +1,98 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.io.input;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+
+import junit.framework.TestCase;
+
+/**
+ * JUnit Test Case for {@link TeeInputStream}.
+ */
+public class TeeInputStreamTest extends TestCase {
+
+    private final String ASCII = "US-ASCII";
+
+    private InputStream tee;
+
+    private ByteArrayOutputStream output;
+
+    protected void setUp() throws Exception {
+        InputStream input = new ByteArrayInputStream("abc".getBytes(ASCII));
+        output = new ByteArrayOutputStream();
+        tee = new TeeInputStream(input, output);
+    }
+
+    public void testReadNothing() throws Exception {
+        assertEquals("", new String(output.toString(ASCII)));
+    }
+
+    public void testReadOneByte() throws Exception {
+        assertEquals('a', tee.read());
+        assertEquals("a", new String(output.toString(ASCII)));
+    }
+
+    public void testReadEverything() throws Exception {
+        assertEquals('a', tee.read());
+        assertEquals('b', tee.read());
+        assertEquals('c', tee.read());
+        assertEquals(-1, tee.read());
+        assertEquals("abc", new String(output.toString(ASCII)));
+    }
+
+    public void testReadToArray() throws Exception {
+        byte[] buffer = new byte[8];
+        assertEquals(3, tee.read(buffer));
+        assertEquals('a', buffer[0]);
+        assertEquals('b', buffer[1]);
+        assertEquals('c', buffer[2]);
+        assertEquals(-1, tee.read(buffer));
+        assertEquals("abc", new String(output.toString(ASCII)));
+    }
+
+    public void testReadToArrayWithOffset() throws Exception {
+        byte[] buffer = new byte[8];
+        assertEquals(3, tee.read(buffer, 4, 4));
+        assertEquals('a', buffer[4]);
+        assertEquals('b', buffer[5]);
+        assertEquals('c', buffer[6]);
+        assertEquals(-1, tee.read(buffer, 4, 4));
+        assertEquals("abc", new String(output.toString(ASCII)));
+    }
+
+    public void testSkip() throws Exception {
+        assertEquals('a', tee.read());
+        assertEquals(1, tee.skip(1));
+        assertEquals('c', tee.read());
+        assertEquals(-1, tee.read());
+        assertEquals("ac", new String(output.toString(ASCII)));
+    }
+
+    public void testMarkReset() throws Exception {
+        assertEquals('a', tee.read());
+        tee.mark(1);
+        assertEquals('b', tee.read());
+        tee.reset();
+        assertEquals('b', tee.read());
+        assertEquals('c', tee.read());
+        assertEquals(-1, tee.read());
+        assertEquals("abbc", new String(output.toString(ASCII)));
+    }
+
+}

Propchange: commons/proper/io/trunk/src/test/org/apache/commons/io/input/TeeInputStreamTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/io/trunk/src/test/org/apache/commons/io/input/TeeInputStreamTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL