Coverage Report - org.apache.commons.io.output.ProxyOutputStream
 
Classes in this File Line Coverage Branch Coverage Complexity
ProxyOutputStream
89%
33/37
100%
2/2
1,778
 
 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.FilterOutputStream;
 20  
 import java.io.IOException;
 21  
 import java.io.OutputStream;
 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 FilterOutputStream
 27  
  * to increase reusability.
 28  
  * <p>
 29  
  * See the protected methods for ways in which a subclass can easily decorate
 30  
  * a stream with custom pre-, post- or error processing functionality.
 31  
  *
 32  
  */
 33  
 public class ProxyOutputStream extends FilterOutputStream {
 34  
 
 35  
     /**
 36  
      * Constructs a new ProxyOutputStream.
 37  
      *
 38  
      * @param proxy  the OutputStream to delegate to
 39  
      */
 40  
     public ProxyOutputStream(final OutputStream proxy) {
 41  85
         super(proxy);
 42  
         // the proxy is stored in a protected superclass variable named 'out'
 43  85
     }
 44  
 
 45  
     /**
 46  
      * Invokes the delegate's <code>write(int)</code> method.
 47  
      * @param idx the byte to write
 48  
      * @throws IOException if an I/O error occurs
 49  
      */
 50  
     @Override
 51  
     public void write(final int idx) throws IOException {
 52  
         try {
 53  54
             beforeWrite(1);
 54  54
             out.write(idx);
 55  52
             afterWrite(1);
 56  2
         } catch (final IOException e) {
 57  2
             handleIOException(e);
 58  52
         }
 59  52
     }
 60  
 
 61  
     /**
 62  
      * Invokes the delegate's <code>write(byte[])</code> method.
 63  
      * @param bts the bytes to write
 64  
      * @throws IOException if an I/O error occurs
 65  
      */
 66  
     @Override
 67  
     public void write(final byte[] bts) throws IOException {
 68  
         try {
 69  56
             final int len = bts != null ? bts.length : 0;
 70  56
             beforeWrite(len);
 71  56
             out.write(bts);
 72  56
             afterWrite(len);
 73  0
         } catch (final IOException e) {
 74  0
             handleIOException(e);
 75  56
         }
 76  56
     }
 77  
 
 78  
     /**
 79  
      * Invokes the delegate's <code>write(byte[])</code> method.
 80  
      * @param bts the bytes to write
 81  
      * @param st The start offset
 82  
      * @param end The number of bytes to write
 83  
      * @throws IOException if an I/O error occurs
 84  
      */
 85  
     @Override
 86  
     public void write(final byte[] bts, final int st, final int end) throws IOException {
 87  
         try {
 88  1056799
             beforeWrite(end);
 89  1056799
             out.write(bts, st, end);
 90  1056799
             afterWrite(end);
 91  0
         } catch (final IOException e) {
 92  0
             handleIOException(e);
 93  1056799
         }
 94  1056799
     }
 95  
 
 96  
     /**
 97  
      * Invokes the delegate's <code>flush()</code> method.
 98  
      * @throws IOException if an I/O error occurs
 99  
      */
 100  
     @Override
 101  
     public void flush() throws IOException {
 102  
         try {
 103  36
             out.flush();
 104  1
         } catch (final IOException e) {
 105  1
             handleIOException(e);
 106  35
         }
 107  35
     }
 108  
 
 109  
     /**
 110  
      * Invokes the delegate's <code>close()</code> method.
 111  
      * @throws IOException if an I/O error occurs
 112  
      */
 113  
     @Override
 114  
     public void close() throws IOException {
 115  
         try {
 116  7
             out.close();
 117  2
         } catch (final IOException e) {
 118  2
             handleIOException(e);
 119  5
         }
 120  5
     }
 121  
 
 122  
     /**
 123  
      * Invoked by the write methods before the call is proxied. The number
 124  
      * of bytes to be written (1 for the {@link #write(int)} method, buffer
 125  
      * length for {@link #write(byte[])}, etc.) is given as an argument.
 126  
      * <p>
 127  
      * Subclasses can override this method to add common pre-processing
 128  
      * functionality without having to override all the write methods.
 129  
      * The default implementation does nothing.
 130  
      *
 131  
      * @since 2.0
 132  
      * @param n number of bytes to be written
 133  
      * @throws IOException if the pre-processing fails
 134  
      */
 135  
     protected void beforeWrite(final int n) throws IOException {
 136  8301
     }
 137  
 
 138  
     /**
 139  
      * Invoked by the write methods after the proxied call has returned
 140  
      * successfully. The number of bytes written (1 for the
 141  
      * {@link #write(int)} method, buffer length for {@link #write(byte[])},
 142  
      * etc.) is given as an argument.
 143  
      * <p>
 144  
      * Subclasses can override this method to add common post-processing
 145  
      * functionality without having to override all the write methods.
 146  
      * The default implementation does nothing.
 147  
      *
 148  
      * @since 2.0
 149  
      * @param n number of bytes written
 150  
      * @throws IOException if the post-processing fails
 151  
      */
 152  
     protected void afterWrite(final int n) throws IOException {
 153  1056907
     }
 154  
 
 155  
     /**
 156  
      * Handle any IOExceptions thrown.
 157  
      * <p>
 158  
      * This method provides a point to implement custom exception
 159  
      * handling. The default behaviour is to re-throw the exception.
 160  
      * @param e The IOException thrown
 161  
      * @throws IOException if an I/O error occurs
 162  
      * @since 2.0
 163  
      */
 164  
     protected void handleIOException(final IOException e) throws IOException {
 165  2
         throw e;
 166  
     }
 167  
 
 168  
 }