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