KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > fileupload > ThresholdingOutputStream


1 /*
2  * $Header: /home/cvs/jakarta-commons/fileupload/src/java/org/apache/commons/fileupload/ThresholdingOutputStream.java,v 1.3 2003/05/31 22:31:08 martinc Exp $
3  * $Revision: 1.3 $
4  * $Date: 2003/05/31 22:31:08 $
5  *
6  * ====================================================================
7  *
8  * The Apache Software License, Version 1.1
9  *
10  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
11  * reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  *
17  * 1. Redistributions of source code must retain the above copyright
18  * notice, this list of conditions and the following disclaimer.
19  *
20  * 2. Redistributions in binary form must reproduce the above copyright
21  * notice, this list of conditions and the following disclaimer in
22  * the documentation and/or other materials provided with the
23  * distribution.
24  *
25  * 3. The end-user documentation included with the redistribution, if
26  * any, must include the following acknowlegement:
27  * "This product includes software developed by the
28  * Apache Software Foundation (http://www.apache.org/)."
29  * Alternately, this acknowlegement may appear in the software itself,
30  * if and wherever such third-party acknowlegements normally appear.
31  *
32  * 4. The names "The Jakarta Project", "Commons", and "Apache Software
33  * Foundation" must not be used to endorse or promote products derived
34  * from this software without prior written permission. For written
35  * permission, please contact apache@apache.org.
36  *
37  * 5. Products derived from this software may not be called "Apache"
38  * nor may "Apache" appear in their names without prior written
39  * permission of the Apache Group.
40  *
41  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52  * SUCH DAMAGE.
53  * ====================================================================
54  *
55  * This software consists of voluntary contributions made by many
56  * individuals on behalf of the Apache Software Foundation. For more
57  * information on the Apache Software Foundation, please see
58  * <http://www.apache.org/>.
59  *
60  */

61
62
63 package org.apache.commons.fileupload;
64
65 import java.io.IOException JavaDoc;
66 import java.io.OutputStream JavaDoc;
67
68
69 /**
70  * An output stream which triggers an event when a specified number of bytes of
71  * data have been written to it. The event can be used, for example, to throw
72  * an exception if a maximum has been reached, or to switch the underlying
73  * stream type when the threshold is exceeded.
74  * <p>
75  * This class overrides all <code>OutputStream</code> methods. However, these
76  * overrides ultimately call the corresponding methods in the underlying output
77  * stream implementation.
78  * <p>
79  * NOTE: This implementation may trigger the event <em>before</em> the threshold
80  * is actually reached, since it triggers when a pending write operation would
81  * cause the threshold to be exceeded.
82  *
83  * @author <a HREF="mailto:martinc@apache.org">Martin Cooper</a>
84  *
85  * @version $Id: ThresholdingOutputStream.java,v 1.3 2003/05/31 22:31:08 martinc Exp $
86  */

87 public abstract class ThresholdingOutputStream
88     extends OutputStream JavaDoc
89 {
90
91     // ----------------------------------------------------------- Data members
92

93
94     /**
95      * The threshold at which the event will be triggered.
96      */

97     private int threshold;
98
99
100     /**
101      * The number of bytes written to the output stream.
102      */

103     private long written;
104
105
106     /**
107      * Whether or not the configured threshold has been exceeded.
108      */

109     private boolean thresholdExceeded;
110
111
112     // ----------------------------------------------------------- Constructors
113

114
115     /**
116      * Constructs an instance of this class which will trigger an event at the
117      * specified threshold.
118      *
119      * @param threshold The number of bytes at which to trigger an event.
120      */

121     public ThresholdingOutputStream(int threshold)
122     {
123         this.threshold = threshold;
124     }
125
126
127     // --------------------------------------------------- OutputStream methods
128

129
130     /**
131      * Writes the specified byte to this output stream.
132      *
133      * @param b The byte to be written.
134      *
135      * @exception IOException if an error occurs.
136      */

137     public void write(int b) throws IOException JavaDoc
138     {
139         checkThreshold(1);
140         getStream().write(b);
141         written++;
142     }
143
144
145     /**
146      * Writes <code>b.length</code> bytes from the specified byte array to this
147      * output stream.
148      *
149      * @param b The array of bytes to be written.
150      *
151      * @exception IOException if an error occurs.
152      */

153     public void write(byte b[]) throws IOException JavaDoc
154     {
155         checkThreshold(b.length);
156         getStream().write(b);
157         written += b.length;
158     }
159
160
161     /**
162      * Writes <code>len</code> bytes from the specified byte array starting at
163      * offset <code>off</code> to this output stream.
164      *
165      * @param b The byte array from which the data will be written.
166      * @param off The start offset in the byte array.
167      * @param len The number of bytes to write.
168      *
169      * @exception IOException if an error occurs.
170      */

171     public void write(byte b[], int off, int len) throws IOException JavaDoc
172     {
173         checkThreshold(len);
174         getStream().write(b, off, len);
175         written += len;
176     }
177
178
179     /**
180      * Flushes this output stream and forces any buffered output bytes to be
181      * written out.
182      *
183      * @exception IOException if an error occurs.
184      */

185     public void flush() throws IOException JavaDoc
186     {
187         getStream().flush();
188     }
189
190
191     /**
192      * Closes this output stream and releases any system resources associated
193      * with this stream.
194      *
195      * @exception IOException if an error occurs.
196      */

197     public void close() throws IOException JavaDoc
198     {
199         try
200         {
201             flush();
202         }
203         catch (IOException JavaDoc ignored)
204         {
205             // ignore
206
}
207         getStream().close();
208     }
209
210
211     // --------------------------------------------------------- Public methods
212

213
214     /**
215      * Returns the threshold, in bytes, at which an event will be triggered.
216      *
217      * @return The threshold point, in bytes.
218      */

219     public int getThreshold()
220     {
221         return threshold;
222     }
223
224
225     /**
226      * Returns the number of bytes that have been written to this output stream.
227      *
228      * @return The number of bytes written.
229      */

230     public long getByteCount()
231     {
232         return written;
233     }
234
235
236     /**
237      * Determines whether or not the configured threshold has been exceeded for
238      * this output stream.
239      *
240      * @return <code>true</code> if the threshold has been reached;
241      * <code>false</code> otherwise.
242      */

243     public boolean isThresholdExceeded()
244     {
245         return (written > threshold);
246     }
247
248
249     // ------------------------------------------------------ Protected methods
250

251
252     /**
253      * Checks to see if writing the specified number of bytes would cause the
254      * configured threshold to be exceeded. If so, triggers an event to allow
255      * a concrete implementation to take action on this.
256      *
257      * @param count The number of bytes about to be written to the underlying
258      * output stream.
259      *
260      * @exception IOException if an error occurs.
261      */

262     protected void checkThreshold(int count) throws IOException JavaDoc
263     {
264         if (!thresholdExceeded && (written + count > threshold))
265         {
266             thresholdReached();
267             thresholdExceeded = true;
268         }
269     }
270
271
272     // ------------------------------------------------------- Abstract methods
273

274
275     /**
276      * Returns the underlying output stream, to which the corresponding
277      * <code>OutputStream</code> methods in this class will ultimately delegate.
278      *
279      * @return The underlying output stream.
280      *
281      * @exception IOException if an error occurs.
282      */

283     protected abstract OutputStream JavaDoc getStream() throws IOException JavaDoc;
284
285
286     /**
287      * Indicates that the configured threshold has been reached, and that a
288      * subclass should take whatever action necessary on this event. This may
289      * include changing the underlying output stream.
290      *
291      * @exception IOException if an error occurs.
292      */

293     protected abstract void thresholdReached() throws IOException JavaDoc;
294 }
295
Popular Tags