KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tomcat > util > http > fileupload > ThresholdingOutputStream


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
18
19 package org.apache.tomcat.util.http.fileupload;
20
21 import java.io.IOException JavaDoc;
22 import java.io.OutputStream JavaDoc;
23
24
25 /**
26  * An output stream which triggers an event when a specified number of bytes of
27  * data have been written to it. The event can be used, for example, to throw
28  * an exception if a maximum has been reached, or to switch the underlying
29  * stream type when the threshold is exceeded.
30  * <p>
31  * This class overrides all <code>OutputStream</code> methods. However, these
32  * overrides ultimately call the corresponding methods in the underlying output
33  * stream implementation.
34  * <p>
35  * NOTE: This implementation may trigger the event <em>before</em> the threshold
36  * is actually reached, since it triggers when a pending write operation would
37  * cause the threshold to be exceeded.
38  *
39  * @author <a HREF="mailto:martinc@apache.org">Martin Cooper</a>
40  *
41  * @version $Id: ThresholdingOutputStream.java 467222 2006-10-24 03:17:11Z markt $
42  */

43 public abstract class ThresholdingOutputStream
44     extends OutputStream JavaDoc
45 {
46
47     // ----------------------------------------------------------- Data members
48

49
50     /**
51      * The threshold at which the event will be triggered.
52      */

53     private int threshold;
54
55
56     /**
57      * The number of bytes written to the output stream.
58      */

59     private long written;
60
61
62     /**
63      * Whether or not the configured threshold has been exceeded.
64      */

65     private boolean thresholdExceeded;
66
67
68     // ----------------------------------------------------------- Constructors
69

70
71     /**
72      * Constructs an instance of this class which will trigger an event at the
73      * specified threshold.
74      *
75      * @param threshold The number of bytes at which to trigger an event.
76      */

77     public ThresholdingOutputStream(int threshold)
78     {
79         this.threshold = threshold;
80     }
81
82
83     // --------------------------------------------------- OutputStream methods
84

85
86     /**
87      * Writes the specified byte to this output stream.
88      *
89      * @param b The byte to be written.
90      *
91      * @exception IOException if an error occurs.
92      */

93     public void write(int b) throws IOException JavaDoc
94     {
95         checkThreshold(1);
96         getStream().write(b);
97         written++;
98     }
99
100
101     /**
102      * Writes <code>b.length</code> bytes from the specified byte array to this
103      * output stream.
104      *
105      * @param b The array of bytes to be written.
106      *
107      * @exception IOException if an error occurs.
108      */

109     public void write(byte b[]) throws IOException JavaDoc
110     {
111         checkThreshold(b.length);
112         getStream().write(b);
113         written += b.length;
114     }
115
116
117     /**
118      * Writes <code>len</code> bytes from the specified byte array starting at
119      * offset <code>off</code> to this output stream.
120      *
121      * @param b The byte array from which the data will be written.
122      * @param off The start offset in the byte array.
123      * @param len The number of bytes to write.
124      *
125      * @exception IOException if an error occurs.
126      */

127     public void write(byte b[], int off, int len) throws IOException JavaDoc
128     {
129         checkThreshold(len);
130         getStream().write(b, off, len);
131         written += len;
132     }
133
134
135     /**
136      * Flushes this output stream and forces any buffered output bytes to be
137      * written out.
138      *
139      * @exception IOException if an error occurs.
140      */

141     public void flush() throws IOException JavaDoc
142     {
143         getStream().flush();
144     }
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     public void close() throws IOException JavaDoc
154     {
155         try
156         {
157             flush();
158         }
159         catch (IOException JavaDoc ignored)
160         {
161             // ignore
162
}
163         getStream().close();
164     }
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         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         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</code> if the threshold has been reached;
197      * <code>false</code> otherwise.
198      */

199     public boolean isThresholdExceeded()
200     {
201         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      * @exception IOException if an error occurs.
217      */

218     protected void checkThreshold(int count) throws IOException JavaDoc
219     {
220         if (!thresholdExceeded && (written + count > threshold))
221         {
222             thresholdReached();
223             thresholdExceeded = true;
224         }
225     }
226
227
228     // ------------------------------------------------------- Abstract methods
229

230
231     /**
232      * Returns the underlying output stream, to which the corresponding
233      * <code>OutputStream</code> methods in this class will ultimately delegate.
234      *
235      * @return The underlying output stream.
236      *
237      * @exception IOException if an error occurs.
238      */

239     protected abstract OutputStream JavaDoc getStream() throws IOException JavaDoc;
240
241
242     /**
243      * Indicates that the configured threshold has been reached, and that a
244      * subclass should take whatever action necessary on this event. This may
245      * include changing the underlying output stream.
246      *
247      * @exception IOException if an error occurs.
248      */

249     protected abstract void thresholdReached() throws IOException JavaDoc;
250 }
251
Popular Tags