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