Coverage Report - org.apache.commons.io.input.TeeInputStream
 
Classes in this File Line Coverage Branch Coverage Complexity
TeeInputStream
81%
18/22
75%
6/8
1.667
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  * 
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  * 
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.commons.io.input;
 18  
 
 19  
 import java.io.IOException;
 20  
 import java.io.InputStream;
 21  
 import java.io.OutputStream;
 22  
 
 23  
 /**
 24  
  * InputStream proxy that transparently writes a copy of all bytes read
 25  
  * from the proxied stream to a given OutputStream. Using {@link #skip(long)}
 26  
  * or {@link #mark(int)}/{@link #reset()} on the stream will result on some
 27  
  * bytes from the input stream being skipped or duplicated in the output
 28  
  * stream.
 29  
  * <p>
 30  
  * The proxied input stream is closed when the {@link #close()} method is
 31  
  * called on this proxy. It is configurable whether the associated output
 32  
  * stream will also closed.
 33  
  *
 34  
  * @version $Id: TeeInputStream.java 1415850 2012-11-30 20:51:39Z ggregory $
 35  
  * @since 1.4
 36  
  */
 37  
 public class TeeInputStream extends ProxyInputStream {
 38  
 
 39  
     /**
 40  
      * The output stream that will receive a copy of all bytes read from the
 41  
      * proxied input stream.
 42  
      */
 43  
     private final OutputStream branch;
 44  
 
 45  
     /**
 46  
      * Flag for closing also the associated output stream when this
 47  
      * stream is closed.
 48  
      */
 49  
     private final boolean closeBranch;
 50  
 
 51  
     /**
 52  
      * Creates a TeeInputStream that proxies the given {@link InputStream}
 53  
      * and copies all read bytes to the given {@link OutputStream}. The given
 54  
      * output stream will not be closed when this stream gets closed.
 55  
      *
 56  
      * @param input input stream to be proxied
 57  
      * @param branch output stream that will receive a copy of all bytes read
 58  
      */
 59  
     public TeeInputStream(final InputStream input, final OutputStream branch) {
 60  14
         this(input, branch, false);
 61  14
     }
 62  
 
 63  
     /**
 64  
      * Creates a TeeInputStream that proxies the given {@link InputStream}
 65  
      * and copies all read bytes to the given {@link OutputStream}. The given
 66  
      * output stream will be closed when this stream gets closed if the
 67  
      * closeBranch parameter is {@code true}.
 68  
      *
 69  
      * @param input input stream to be proxied
 70  
      * @param branch output stream that will receive a copy of all bytes read
 71  
      * @param closeBranch flag for closing also the output stream when this
 72  
      *                    stream is closed
 73  
      */
 74  
     public TeeInputStream(
 75  
             final InputStream input, final OutputStream branch, final boolean closeBranch) {
 76  14
         super(input);
 77  14
         this.branch = branch;
 78  14
         this.closeBranch = closeBranch;
 79  14
     }
 80  
 
 81  
     /**
 82  
      * Closes the proxied input stream and, if so configured, the associated
 83  
      * output stream. An exception thrown from one stream will not prevent
 84  
      * closing of the other stream.
 85  
      *
 86  
      * @throws IOException if either of the streams could not be closed
 87  
      */
 88  
     @Override
 89  
     public void close() throws IOException {
 90  
         try {
 91  0
             super.close();
 92  
         } finally {
 93  0
             if (closeBranch) {
 94  0
                 branch.close();
 95  
             }
 96  
         }
 97  0
     }
 98  
 
 99  
     /**
 100  
      * Reads a single byte from the proxied input stream and writes it to
 101  
      * the associated output stream.
 102  
      *
 103  
      * @return next byte from the stream, or -1 if the stream has ended
 104  
      * @throws IOException if the stream could not be read (or written) 
 105  
      */
 106  
     @Override
 107  
     public int read() throws IOException {
 108  26
         final int ch = super.read();
 109  26
         if (ch != -1) {
 110  20
             branch.write(ch);
 111  
         }
 112  26
         return ch;
 113  
     }
 114  
 
 115  
     /**
 116  
      * Reads bytes from the proxied input stream and writes the read bytes
 117  
      * to the associated output stream.
 118  
      *
 119  
      * @param bts byte buffer
 120  
      * @param st start offset within the buffer
 121  
      * @param end maximum number of bytes to read
 122  
      * @return number of bytes read, or -1 if the stream has ended
 123  
      * @throws IOException if the stream could not be read (or written) 
 124  
      */
 125  
     @Override
 126  
     public int read(final byte[] bts, final int st, final int end) throws IOException {
 127  4
         final int n = super.read(bts, st, end);
 128  4
         if (n != -1) {
 129  2
             branch.write(bts, st, n);
 130  
         }
 131  4
         return n;
 132  
     }
 133  
 
 134  
     /**
 135  
      * Reads bytes from the proxied input stream and writes the read bytes
 136  
      * to the associated output stream.
 137  
      *
 138  
      * @param bts byte buffer
 139  
      * @return number of bytes read, or -1 if the stream has ended
 140  
      * @throws IOException if the stream could not be read (or written) 
 141  
      */
 142  
     @Override
 143  
     public int read(final byte[] bts) throws IOException {
 144  4
         final int n = super.read(bts);
 145  4
         if (n != -1) {
 146  2
             branch.write(bts, 0, n);
 147  
         }
 148  4
         return n;
 149  
     }
 150  
 
 151  
 }