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