Coverage Report - org.apache.commons.io.output.ThresholdingOutputStream
 
Classes in this File Line Coverage Branch Coverage Complexity
ThresholdingOutputStream
82%
28/34
100%
6/6
1,214
 
 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.IOException;
 20  
 import java.io.OutputStream;
 21  
 
 22  
 
 23  
 /**
 24  
  * An output stream which triggers an event when a specified number of bytes of
 25  
  * data have been written to it. The event can be used, for example, to throw
 26  
  * an exception if a maximum has been reached, or to switch the underlying
 27  
  * stream type when the threshold is exceeded.
 28  
  * <p>
 29  
  * This class overrides all <code>OutputStream</code> methods. However, these
 30  
  * overrides ultimately call the corresponding methods in the underlying output
 31  
  * stream implementation.
 32  
  * <p>
 33  
  * NOTE: This implementation may trigger the event <em>before</em> the threshold
 34  
  * is actually reached, since it triggers when a pending write operation would
 35  
  * cause the threshold to be exceeded.
 36  
  *
 37  
  */
 38  
 public abstract class ThresholdingOutputStream
 39  
     extends OutputStream
 40  
 {
 41  
 
 42  
     // ----------------------------------------------------------- Data members
 43  
 
 44  
 
 45  
     /**
 46  
      * The threshold at which the event will be triggered.
 47  
      */
 48  
     private final int threshold;
 49  
 
 50  
 
 51  
     /**
 52  
      * The number of bytes written to the output stream.
 53  
      */
 54  
     private long written;
 55  
 
 56  
 
 57  
     /**
 58  
      * Whether or not the configured threshold has been exceeded.
 59  
      */
 60  
     private boolean thresholdExceeded;
 61  
 
 62  
 
 63  
     // ----------------------------------------------------------- Constructors
 64  
 
 65  
 
 66  
     /**
 67  
      * Constructs an instance of this class which will trigger an event at the
 68  
      * specified threshold.
 69  
      *
 70  
      * @param threshold The number of bytes at which to trigger an event.
 71  
      */
 72  
     public ThresholdingOutputStream(final int threshold)
 73  262
     {
 74  262
         this.threshold = threshold;
 75  262
     }
 76  
 
 77  
 
 78  
     // --------------------------------------------------- OutputStream methods
 79  
 
 80  
 
 81  
     /**
 82  
      * Writes the specified byte to this output stream.
 83  
      *
 84  
      * @param b The byte to be written.
 85  
      *
 86  
      * @throws IOException if an error occurs.
 87  
      */
 88  
     @Override
 89  
     public void write(final int b) throws IOException
 90  
     {
 91  4
         checkThreshold(1);
 92  4
         getStream().write(b);
 93  4
         written++;
 94  4
     }
 95  
 
 96  
 
 97  
     /**
 98  
      * Writes <code>b.length</code> bytes from the specified byte array to this
 99  
      * output stream.
 100  
      *
 101  
      * @param b The array of bytes to be written.
 102  
      *
 103  
      * @throws IOException if an error occurs.
 104  
      */
 105  
     @Override
 106  
     public void write(final byte b[]) throws IOException
 107  
     {
 108  52
         checkThreshold(b.length);
 109  52
         getStream().write(b);
 110  52
         written += b.length;
 111  52
     }
 112  
 
 113  
 
 114  
     /**
 115  
      * Writes <code>len</code> bytes from the specified byte array starting at
 116  
      * offset <code>off</code> to this output stream.
 117  
      *
 118  
      * @param b   The byte array from which the data will be written.
 119  
      * @param off The start offset in the byte array.
 120  
      * @param len The number of bytes to write.
 121  
      *
 122  
      * @throws IOException if an error occurs.
 123  
      */
 124  
     @Override
 125  
     public void write(final byte b[], final int off, final int len) throws IOException
 126  
     {
 127  234
         checkThreshold(len);
 128  234
         getStream().write(b, off, len);
 129  234
         written += len;
 130  234
     }
 131  
 
 132  
 
 133  
     /**
 134  
      * Flushes this output stream and forces any buffered output bytes to be
 135  
      * written out.
 136  
      *
 137  
      * @throws IOException if an error occurs.
 138  
      */
 139  
     @Override
 140  
     public void flush() throws IOException
 141  
     {
 142  236
         getStream().flush();
 143  236
     }
 144  
 
 145  
 
 146  
     /**
 147  
      * Closes this output stream and releases any system resources associated
 148  
      * with this stream.
 149  
      *
 150  
      * @throws IOException if an error occurs.
 151  
      */
 152  
     @Override
 153  
     public void close() throws IOException
 154  
     {
 155  
         try
 156  
         {
 157  236
             flush();
 158  
         }
 159  0
         catch (final IOException ignored)
 160  
         {
 161  
             // ignore
 162  236
         }
 163  236
         getStream().close();
 164  236
     }
 165  
 
 166  
 
 167  
     // --------------------------------------------------------- Public methods
 168  
 
 169  
 
 170  
     /**
 171  
      * Returns the threshold, in bytes, at which an event will be triggered.
 172  
      *
 173  
      * @return The threshold point, in bytes.
 174  
      */
 175  
     public int getThreshold()
 176  
     {
 177  0
         return threshold;
 178  
     }
 179  
 
 180  
 
 181  
     /**
 182  
      * Returns the number of bytes that have been written to this output stream.
 183  
      *
 184  
      * @return The number of bytes written.
 185  
      */
 186  
     public long getByteCount()
 187  
     {
 188  0
         return written;
 189  
     }
 190  
 
 191  
 
 192  
     /**
 193  
      * Determines whether or not the configured threshold has been exceeded for
 194  
      * this output stream.
 195  
      *
 196  
      * @return {@code true} if the threshold has been reached;
 197  
      *         {@code false} otherwise.
 198  
      */
 199  
     public boolean isThresholdExceeded()
 200  
     {
 201  286
         return written > threshold;
 202  
     }
 203  
 
 204  
 
 205  
     // ------------------------------------------------------ Protected methods
 206  
 
 207  
 
 208  
     /**
 209  
      * Checks to see if writing the specified number of bytes would cause the
 210  
      * configured threshold to be exceeded. If so, triggers an event to allow
 211  
      * a concrete implementation to take action on this.
 212  
      *
 213  
      * @param count The number of bytes about to be written to the underlying
 214  
      *              output stream.
 215  
      *
 216  
      * @throws IOException if an error occurs.
 217  
      */
 218  
     protected void checkThreshold(final int count) throws IOException
 219  
     {
 220  290
         if (!thresholdExceeded && written + count > threshold)
 221  
         {
 222  132
             thresholdExceeded = true;
 223  132
             thresholdReached();
 224  
         }
 225  290
     }
 226  
 
 227  
     /**
 228  
      * Resets the byteCount to zero.  You can call this from 
 229  
      * {@link #thresholdReached()} if you want the event to be triggered again. 
 230  
      */
 231  
     protected void resetByteCount() 
 232  
     {
 233  0
         this.thresholdExceeded = false;
 234  0
         this.written = 0;
 235  0
     }
 236  
 
 237  
     /**
 238  
      * Sets the byteCount to count.  Useful for re-opening an output stream
 239  
      * that has previously been written to.
 240  
      *
 241  
      * @param count The number of bytes that have already been written to the
 242  
      * output stream
 243  
      *
 244  
      * @since 2.5
 245  
      */
 246  
     protected void setByteCount(final long count) {
 247  2
         this.written = count;
 248  2
     }
 249  
 
 250  
 
 251  
     // ------------------------------------------------------- Abstract methods
 252  
 
 253  
 
 254  
     /**
 255  
      * Returns the underlying output stream, to which the corresponding
 256  
      * <code>OutputStream</code> methods in this class will ultimately delegate.
 257  
      *
 258  
      * @return The underlying output stream.
 259  
      *
 260  
      * @throws IOException if an error occurs.
 261  
      */
 262  
     protected abstract OutputStream getStream() throws IOException;
 263  
 
 264  
 
 265  
     /**
 266  
      * Indicates that the configured threshold has been reached, and that a
 267  
      * subclass should take whatever action necessary on this event. This may
 268  
      * include changing the underlying output stream.
 269  
      *
 270  
      * @throws IOException if an error occurs.
 271  
      */
 272  
     protected abstract void thresholdReached() throws IOException;
 273  
 }