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