2
* Copyright 2001-2004 The Apache Software Foundation.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
18
package org.apache.commons.io.output;
20
import java.io.IOException;
21
import java.io.OutputStream;
25
* An output stream which triggers an event when a specified number of bytes of
26
* data have been written to it. The event can be used, for example, to throw
27
* an exception if a maximum has been reached, or to switch the underlying
28
* stream type when the threshold is exceeded.
30
* This class overrides all <code>OutputStream</code> methods. However, these
31
* overrides ultimately call the corresponding methods in the underlying output
32
* stream implementation.
34
* NOTE: This implementation may trigger the event <em>before</em> the threshold
35
* is actually reached, since it triggers when a pending write operation would
36
* cause the threshold to be exceeded.
38
* @author <a href="mailto:martinc@apache.org">Martin Cooper</a>
40
* @version $Id: ThresholdingOutputStream.java,v 1.2 2004/02/23 04:40:29 bayard Exp $
42
public abstract class ThresholdingOutputStream
46
// ----------------------------------------------------------- Data members
50
* The threshold at which the event will be triggered.
52
private int threshold;
56
* The number of bytes written to the output stream.
62
* Whether or not the configured threshold has been exceeded.
64
private boolean thresholdExceeded;
67
// ----------------------------------------------------------- Constructors
71
* Constructs an instance of this class which will trigger an event at the
72
* specified threshold.
74
* @param threshold The number of bytes at which to trigger an event.
76
public ThresholdingOutputStream(int threshold)
78
this.threshold = threshold;
82
// --------------------------------------------------- OutputStream methods
86
* Writes the specified byte to this output stream.
88
* @param b The byte to be written.
90
* @exception IOException if an error occurs.
92
public void write(int b) throws IOException
101
* Writes <code>b.length</code> bytes from the specified byte array to this
104
* @param b The array of bytes to be written.
106
* @exception IOException if an error occurs.
108
public void write(byte b[]) throws IOException
110
checkThreshold(b.length);
111
getStream().write(b);
117
* Writes <code>len</code> bytes from the specified byte array starting at
118
* offset <code>off</code> to this output stream.
120
* @param b The byte array from which the data will be written.
121
* @param off The start offset in the byte array.
122
* @param len The number of bytes to write.
124
* @exception IOException if an error occurs.
126
public void write(byte b[], int off, int len) throws IOException
129
getStream().write(b, off, len);
135
* Flushes this output stream and forces any buffered output bytes to be
138
* @exception IOException if an error occurs.
140
public void flush() throws IOException
147
* Closes this output stream and releases any system resources associated
150
* @exception IOException if an error occurs.
152
public void close() throws IOException
158
catch (IOException ignored)
166
// --------------------------------------------------------- Public methods
170
* Returns the threshold, in bytes, at which an event will be triggered.
172
* @return The threshold point, in bytes.
174
public int getThreshold()
181
* Returns the number of bytes that have been written to this output stream.
183
* @return The number of bytes written.
185
public long getByteCount()
192
* Determines whether or not the configured threshold has been exceeded for
193
* this output stream.
195
* @return <code>true</code> if the threshold has been reached;
196
* <code>false</code> otherwise.
198
public boolean isThresholdExceeded()
200
return (written > threshold);
204
// ------------------------------------------------------ Protected methods
208
* Checks to see if writing the specified number of bytes would cause the
209
* configured threshold to be exceeded. If so, triggers an event to allow
210
* a concrete implementation to take action on this.
212
* @param count The number of bytes about to be written to the underlying
215
* @exception IOException if an error occurs.
217
protected void checkThreshold(int count) throws IOException
219
if (!thresholdExceeded && (written + count > threshold))
222
thresholdExceeded = true;
227
// ------------------------------------------------------- Abstract methods
231
* Returns the underlying output stream, to which the corresponding
232
* <code>OutputStream</code> methods in this class will ultimately delegate.
234
* @return The underlying output stream.
236
* @exception IOException if an error occurs.
238
protected abstract OutputStream getStream() throws IOException;
242
* Indicates that the configured threshold has been reached, and that a
243
* subclass should take whatever action necessary on this event. This may
244
* include changing the underlying output stream.
246
* @exception IOException if an error occurs.
248
protected abstract void thresholdReached() throws IOException;