Coverage Report - org.apache.commons.io.input.ProxyInputStream
 
Classes in this File Line Coverage Branch Coverage Complexity
ProxyInputStream
70%
33/47
75%
3/4
2.154
 
 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 static org.apache.commons.io.IOUtils.EOF;
 20  
 
 21  
 import java.io.FilterInputStream;
 22  
 import java.io.IOException;
 23  
 import java.io.InputStream;
 24  
 
 25  
 /**
 26  
  * A Proxy stream which acts as expected, that is it passes the method
 27  
  * calls on to the proxied stream and doesn't change which methods are
 28  
  * being called.
 29  
  * <p>
 30  
  * It is an alternative base class to FilterInputStream
 31  
  * to increase reusability, because FilterInputStream changes the
 32  
  * methods being called, such as read(byte[]) to read(byte[], int, int).
 33  
  * <p>
 34  
  * See the protected methods for ways in which a subclass can easily decorate
 35  
  * a stream with custom pre-, post- or error processing functionality.
 36  
  *
 37  
  * @version $Id: ProxyInputStream.java 1603493 2014-06-18 15:46:07Z ggregory $
 38  
  */
 39  
 public abstract class ProxyInputStream extends FilterInputStream {
 40  
 
 41  
     /**
 42  
      * Constructs a new ProxyInputStream.
 43  
      *
 44  
      * @param proxy  the InputStream to delegate to
 45  
      */
 46  
     public ProxyInputStream(final InputStream proxy) {
 47  616
         super(proxy);
 48  
         // the proxy is stored in a protected superclass variable named 'in'
 49  616
     }
 50  
 
 51  
     /**
 52  
      * Invokes the delegate's <code>read()</code> method.
 53  
      * @return the byte read or -1 if the end of stream
 54  
      * @throws IOException if an I/O error occurs
 55  
      */
 56  
     @Override
 57  
     public int read() throws IOException {
 58  
         try {
 59  28
             beforeRead(1);
 60  28
             final int b = in.read();
 61  27
             afterRead(b != EOF ? 1 : EOF);
 62  27
             return b;
 63  1
         } catch (final IOException e) {
 64  1
             handleIOException(e);
 65  0
             return EOF;
 66  
         }
 67  
     }
 68  
 
 69  
     /**
 70  
      * Invokes the delegate's <code>read(byte[])</code> method.
 71  
      * @param bts the buffer to read the bytes into
 72  
      * @return the number of bytes read or EOF if the end of stream
 73  
      * @throws IOException if an I/O error occurs
 74  
      */
 75  
     @Override
 76  
     public int read(final byte[] bts) throws IOException {
 77  
         try {
 78  1056816
             beforeRead(bts != null ? bts.length : 0);
 79  1056816
             final int n = in.read(bts);
 80  1056816
             afterRead(n);
 81  1056816
             return n;
 82  0
         } catch (final IOException e) {
 83  0
             handleIOException(e);
 84  0
             return EOF;
 85  
         }
 86  
     }
 87  
 
 88  
     /**
 89  
      * Invokes the delegate's <code>read(byte[], int, int)</code> method.
 90  
      * @param bts the buffer to read the bytes into
 91  
      * @param off The start offset
 92  
      * @param len The number of bytes to read
 93  
      * @return the number of bytes read or -1 if the end of stream
 94  
      * @throws IOException if an I/O error occurs
 95  
      */
 96  
     @Override
 97  
     public int read(final byte[] bts, final int off, final int len) throws IOException {
 98  
         try {
 99  35
             beforeRead(len);
 100  35
             final int n = in.read(bts, off, len);
 101  35
             afterRead(n);
 102  35
             return n;
 103  0
         } catch (final IOException e) {
 104  0
             handleIOException(e);
 105  0
             return EOF;
 106  
         }
 107  
     }
 108  
 
 109  
     /**
 110  
      * Invokes the delegate's <code>skip(long)</code> method.
 111  
      * @param ln the number of bytes to skip
 112  
      * @return the actual number of bytes skipped
 113  
      * @throws IOException if an I/O error occurs
 114  
      */
 115  
     @Override
 116  
     public long skip(final long ln) throws IOException {
 117  
         try {
 118  2
             return in.skip(ln);
 119  0
         } catch (final IOException e) {
 120  0
             handleIOException(e);
 121  0
             return 0;
 122  
         }
 123  
     }
 124  
 
 125  
     /**
 126  
      * Invokes the delegate's <code>available()</code> method.
 127  
      * @return the number of available bytes
 128  
      * @throws IOException if an I/O error occurs
 129  
      */
 130  
     @Override
 131  
     public int available() throws IOException {
 132  
         try {
 133  52
             return super.available();
 134  1
         } catch (final IOException e) {
 135  1
             handleIOException(e);
 136  0
             return 0;
 137  
         }
 138  
     }
 139  
 
 140  
     /**
 141  
      * Invokes the delegate's <code>close()</code> method.
 142  
      * @throws IOException if an I/O error occurs
 143  
      */
 144  
     @Override
 145  
     public void close() throws IOException {
 146  
         try {
 147  381
             in.close();
 148  1
         } catch (final IOException e) {
 149  1
             handleIOException(e);
 150  380
         }
 151  380
     }
 152  
 
 153  
     /**
 154  
      * Invokes the delegate's <code>mark(int)</code> method.
 155  
      * @param readlimit read ahead limit
 156  
      */
 157  
     @Override
 158  
     public synchronized void mark(final int readlimit) {
 159  1
         in.mark(readlimit);
 160  1
     }
 161  
 
 162  
     /**
 163  
      * Invokes the delegate's <code>reset()</code> method.
 164  
      * @throws IOException if an I/O error occurs
 165  
      */
 166  
     @Override
 167  
     public synchronized void reset() throws IOException {
 168  
         try {
 169  1
             in.reset();
 170  0
         } catch (final IOException e) {
 171  0
             handleIOException(e);
 172  1
         }
 173  1
     }
 174  
 
 175  
     /**
 176  
      * Invokes the delegate's <code>markSupported()</code> method.
 177  
      * @return true if mark is supported, otherwise false
 178  
      */
 179  
     @Override
 180  
     public boolean markSupported() {
 181  4
         return in.markSupported();
 182  
     }
 183  
 
 184  
     /**
 185  
      * Invoked by the read methods before the call is proxied. The number
 186  
      * of bytes that the caller wanted to read (1 for the {@link #read()}
 187  
      * method, buffer length for {@link #read(byte[])}, etc.) is given as
 188  
      * an argument.
 189  
      * <p>
 190  
      * Subclasses can override this method to add common pre-processing
 191  
      * functionality without having to override all the read methods.
 192  
      * The default implementation does nothing.
 193  
      * <p>
 194  
      * Note this method is <em>not</em> called from {@link #skip(long)} or
 195  
      * {@link #reset()}. You need to explicitly override those methods if
 196  
      * you want to add pre-processing steps also to them.
 197  
      *
 198  
      * @since 2.0
 199  
      * @param n number of bytes that the caller asked to be read
 200  
      * @throws IOException if the pre-processing fails
 201  
      */
 202  
     protected void beforeRead(final int n) throws IOException {
 203  
         // no-op
 204  1056879
     }
 205  
 
 206  
     /**
 207  
      * Invoked by the read methods after the proxied call has returned
 208  
      * successfully. The number of bytes returned to the caller (or -1 if
 209  
      * the end of stream was reached) is given as an argument.
 210  
      * <p>
 211  
      * Subclasses can override this method to add common post-processing
 212  
      * functionality without having to override all the read methods.
 213  
      * The default implementation does nothing.
 214  
      * <p>
 215  
      * Note this method is <em>not</em> called from {@link #skip(long)} or
 216  
      * {@link #reset()}. You need to explicitly override those methods if
 217  
      * you want to add post-processing steps also to them.
 218  
      *
 219  
      * @since 2.0
 220  
      * @param n number of bytes read, or -1 if the end of stream was reached
 221  
      * @throws IOException if the post-processing fails
 222  
      */
 223  
     protected void afterRead(final int n) throws IOException {
 224  
         // no-op
 225  8274
     }
 226  
 
 227  
     /**
 228  
      * Handle any IOExceptions thrown.
 229  
      * <p>
 230  
      * This method provides a point to implement custom exception
 231  
      * handling. The default behaviour is to re-throw the exception.
 232  
      * @param e The IOException thrown
 233  
      * @throws IOException if an I/O error occurs
 234  
      * @since 2.0
 235  
      */
 236  
     protected void handleIOException(final IOException e) throws IOException {
 237  0
         throw e;
 238  
     }
 239  
 
 240  
 }