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