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