001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.wicket.util.io;
018:
019: import java.io.IOException;
020: import java.io.OutputStream;
021:
022: /**
023: * An output stream which triggers an event when a specified number of bytes of data have
024: * been written to it. The event can be used, for example, to throw an exception if a
025: * maximum has been reached, or to switch the underlying stream type when the threshold is
026: * exceeded.
027: * <p>
028: * This class overrides all <code>OutputStream</code> methods. However, these overrides
029: * ultimately call the corresponding methods in the underlying output stream
030: * implementation.
031: * <p>
032: * NOTE: This implementation may trigger the event <em>before</em> the threshold is
033: * actually reached, since it triggers when a pending write operation would cause the
034: * threshold to be exceeded.
035: * @author <a href="mailto:martinc@apache.org">Martin Cooper</a>
036: * @version $Id$
037: */
038: public abstract class ThresholdingOutputStream extends OutputStream {
039:
040: // ----------------------------------------------------------- Data members
041:
042: /**
043: * The threshold at which the event will be triggered.
044: */
045: private final int threshold;
046:
047: /**
048: * The number of bytes written to the output stream.
049: */
050: private long written;
051:
052: /**
053: * Whether or not the configured threshold has been exceeded.
054: */
055: private boolean thresholdExceeded;
056:
057: // ----------------------------------------------------------- Constructors
058:
059: /**
060: * Constructs an instance of this class which will trigger an event at the specified
061: * threshold.
062: * @param threshold The number of bytes at which to trigger an event.
063: */
064: public ThresholdingOutputStream(int threshold) {
065: this .threshold = threshold;
066: }
067:
068: // --------------------------------------------------- OutputStream methods
069:
070: /**
071: * Writes the specified byte to this output stream.
072: * @param b The byte to be written.
073: * @exception IOException if an error occurs.
074: */
075: public void write(int b) throws IOException {
076: checkThreshold(1);
077: getStream().write(b);
078: written++;
079: }
080:
081: /**
082: * Writes <code>b.length</code> bytes from the specified byte array to this output
083: * stream.
084: * @param b The array of bytes to be written.
085: * @exception IOException if an error occurs.
086: */
087: public void write(byte b[]) throws IOException {
088: checkThreshold(b.length);
089: getStream().write(b);
090: written += b.length;
091: }
092:
093: /**
094: * Writes <code>len</code> bytes from the specified byte array starting at offset
095: * <code>off</code> to this output stream.
096: * @param b The byte array from which the data will be written.
097: * @param off The start offset in the byte array.
098: * @param len The number of bytes to write.
099: * @exception IOException if an error occurs.
100: */
101: public void write(byte b[], int off, int len) throws IOException {
102: checkThreshold(len);
103: getStream().write(b, off, len);
104: written += len;
105: }
106:
107: /**
108: * Flushes this output stream and forces any buffered output bytes to be written out.
109: * @exception IOException if an error occurs.
110: */
111: public void flush() throws IOException {
112: getStream().flush();
113: }
114:
115: /**
116: * Closes this output stream and releases any system resources associated with this
117: * stream.
118: * @exception IOException if an error occurs.
119: */
120: public void close() throws IOException {
121: try {
122: flush();
123: } catch (IOException ignored) {
124: // ignore
125: }
126: getStream().close();
127: }
128:
129: // --------------------------------------------------------- Public methods
130:
131: /**
132: * Returns the threshold, in bytes, at which an event will be triggered.
133: * @return The threshold point, in bytes.
134: */
135: public int getThreshold() {
136: return threshold;
137: }
138:
139: /**
140: * Returns the number of bytes that have been written to this output stream.
141: * @return The number of bytes written.
142: */
143: public long getByteCount() {
144: return written;
145: }
146:
147: /**
148: * Determines whether or not the configured threshold has been exceeded for this
149: * output stream.
150: * @return <code>true</code> if the threshold has been reached; <code>false</code>
151: * otherwise.
152: */
153: public boolean isThresholdExceeded() {
154: return (written > threshold);
155: }
156:
157: // ------------------------------------------------------ Protected methods
158:
159: /**
160: * Checks to see if writing the specified number of bytes would cause the configured
161: * threshold to be exceeded. If so, triggers an event to allow a concrete
162: * implementation to take action on this.
163: * @param count The number of bytes about to be written to the underlying output
164: * stream.
165: * @exception IOException if an error occurs.
166: */
167: protected void checkThreshold(int count) throws IOException {
168: if (!thresholdExceeded && (written + count > threshold)) {
169: thresholdReached();
170: thresholdExceeded = true;
171: }
172: }
173:
174: // ------------------------------------------------------- Abstract methods
175:
176: /**
177: * Returns the underlying output stream, to which the corresponding
178: * <code>OutputStream</code> methods in this class will ultimately delegate.
179: * @return The underlying output stream.
180: * @exception IOException if an error occurs.
181: */
182: protected abstract OutputStream getStream() throws IOException;
183:
184: /**
185: * Indicates that the configured threshold has been reached, and that a subclass
186: * should take whatever action necessary on this event. This may include changing the
187: * underlying output stream.
188: * @exception IOException if an error occurs.
189: */
190: protected abstract void thresholdReached() throws IOException;
191: }
|