3434 */
3535public abstract class ThresholdingOutputStream extends OutputStream {
3636
37- // ----------------------------------------------------------- Data members
38-
3937 /**
4038 * The threshold at which the event will be triggered.
4139 */
@@ -51,8 +49,6 @@ public abstract class ThresholdingOutputStream extends OutputStream {
5149 */
5250 private boolean thresholdExceeded ;
5351
54- // ----------------------------------------------------------- Constructors
55-
5652 /**
5753 * Constructs an instance of this class which will trigger an event at the specified threshold.
5854 *
@@ -62,82 +58,65 @@ public ThresholdingOutputStream(final int threshold) {
6258 this .threshold = threshold ;
6359 }
6460
65- // --------------------------------------------------- OutputStream methods
66-
6761 /**
68- * Writes the specified byte to this output stream.
62+ * Checks to see if writing the specified number of bytes would cause the configured threshold to be exceeded. If
63+ * so, triggers an event to allow a concrete implementation to take action on this.
6964 *
70- * @param b The byte to be written.
65+ * @param count The number of bytes about to be written to the underlying output stream .
7166 *
7267 * @throws IOException if an error occurs.
7368 */
74- @ SuppressWarnings ("resource" ) // the underlying stream is managed by a subclass.
75- @ Override
76- public void write (final int b ) throws IOException {
77- checkThreshold (1 );
78- getStream ().write (b );
79- written ++;
69+ protected void checkThreshold (final int count ) throws IOException {
70+ if (!thresholdExceeded && written + count > threshold ) {
71+ thresholdExceeded = true ;
72+ thresholdReached ();
73+ }
8074 }
8175
8276 /**
83- * Writes {@code b.length} bytes from the specified byte array to this output stream.
84- *
85- * @param b The array of bytes to be written.
77+ * Closes this output stream and releases any system resources associated with this stream.
8678 *
8779 * @throws IOException if an error occurs.
8880 */
89- @ SuppressWarnings ("resource" ) // the underlying stream is managed by a subclass.
9081 @ Override
91- public void write (final byte [] b ) throws IOException {
92- checkThreshold (b .length );
93- getStream ().write (b );
94- written += b .length ;
82+ public void close () throws IOException {
83+ try {
84+ flush ();
85+ } catch (final IOException ignored ) {
86+ // ignore
87+ }
88+ getStream ().close ();
9589 }
9690
9791 /**
98- * Writes {@code len} bytes from the specified byte array starting at offset {@code off} to this output stream.
99- *
100- * @param b The byte array from which the data will be written.
101- * @param off The start offset in the byte array.
102- * @param len The number of bytes to write.
92+ * Flushes this output stream and forces any buffered output bytes to be written out.
10393 *
10494 * @throws IOException if an error occurs.
10595 */
10696 @ SuppressWarnings ("resource" ) // the underlying stream is managed by a subclass.
10797 @ Override
108- public void write (final byte [] b , final int off , final int len ) throws IOException {
109- checkThreshold (len );
110- getStream ().write (b , off , len );
111- written += len ;
98+ public void flush () throws IOException {
99+ getStream ().flush ();
112100 }
113101
114102 /**
115- * Flushes this output stream and forces any buffered output bytes to be written out .
103+ * Returns the number of bytes that have been written to this output stream .
116104 *
117- * @throws IOException if an error occurs .
105+ * @return The number of bytes written .
118106 */
119- @ SuppressWarnings ("resource" ) // the underlying stream is managed by a subclass.
120- @ Override
121- public void flush () throws IOException {
122- getStream ().flush ();
107+ public long getByteCount () {
108+ return written ;
123109 }
124110
125111 /**
126- * Closes this output stream and releases any system resources associated with this stream.
112+ * Returns the underlying output stream, to which the corresponding {@code OutputStream} methods in this class will
113+ * ultimately delegate.
114+ *
115+ * @return The underlying output stream.
127116 *
128117 * @throws IOException if an error occurs.
129118 */
130- @ Override
131- public void close () throws IOException {
132- try {
133- flush ();
134- } catch (final IOException ignored ) {
135- // ignore
136- }
137- getStream ().close ();
138- }
139-
140- // --------------------------------------------------------- Public methods
119+ protected abstract OutputStream getStream () throws IOException ;
141120
142121 /**
143122 * Returns the threshold, in bytes, at which an event will be triggered.
@@ -148,15 +127,6 @@ public int getThreshold() {
148127 return threshold ;
149128 }
150129
151- /**
152- * Returns the number of bytes that have been written to this output stream.
153- *
154- * @return The number of bytes written.
155- */
156- public long getByteCount () {
157- return written ;
158- }
159-
160130 /**
161131 * Determines whether or not the configured threshold has been exceeded for this output stream.
162132 *
@@ -166,23 +136,6 @@ public boolean isThresholdExceeded() {
166136 return written > threshold ;
167137 }
168138
169- // ------------------------------------------------------ Protected methods
170-
171- /**
172- * Checks to see if writing the specified number of bytes would cause the configured threshold to be exceeded. If
173- * so, triggers an event to allow a concrete implementation to take action on this.
174- *
175- * @param count The number of bytes about to be written to the underlying output stream.
176- *
177- * @throws IOException if an error occurs.
178- */
179- protected void checkThreshold (final int count ) throws IOException {
180- if (!thresholdExceeded && written + count > threshold ) {
181- thresholdExceeded = true ;
182- thresholdReached ();
183- }
184- }
185-
186139 /**
187140 * Resets the byteCount to zero. You can call this from {@link #thresholdReached()} if you want the event to be
188141 * triggered again.
@@ -203,23 +156,58 @@ protected void setByteCount(final long count) {
203156 this .written = count ;
204157 }
205158
206- // ------------------------------------------------------- Abstract methods
159+ /**
160+ * Indicates that the configured threshold has been reached, and that a subclass should take whatever action
161+ * necessary on this event. This may include changing the underlying output stream.
162+ *
163+ * @throws IOException if an error occurs.
164+ */
165+ protected abstract void thresholdReached () throws IOException ;
207166
208167 /**
209- * Returns the underlying output stream, to which the corresponding {@code OutputStream} methods in this class will
210- * ultimately delegate.
168+ * Writes {@code b.length} bytes from the specified byte array to this output stream.
211169 *
212- * @return The underlying output stream .
170+ * @param b The array of bytes to be written .
213171 *
214172 * @throws IOException if an error occurs.
215173 */
216- protected abstract OutputStream getStream () throws IOException ;
174+ @ SuppressWarnings ("resource" ) // the underlying stream is managed by a subclass.
175+ @ Override
176+ public void write (final byte [] b ) throws IOException {
177+ checkThreshold (b .length );
178+ getStream ().write (b );
179+ written += b .length ;
180+ }
217181
218182 /**
219- * Indicates that the configured threshold has been reached, and that a subclass should take whatever action
220- * necessary on this event. This may include changing the underlying output stream.
183+ * Writes {@code len} bytes from the specified byte array starting at offset {@code off} to this output stream.
184+ *
185+ * @param b The byte array from which the data will be written.
186+ * @param off The start offset in the byte array.
187+ * @param len The number of bytes to write.
221188 *
222189 * @throws IOException if an error occurs.
223190 */
224- protected abstract void thresholdReached () throws IOException ;
191+ @ SuppressWarnings ("resource" ) // the underlying stream is managed by a subclass.
192+ @ Override
193+ public void write (final byte [] b , final int off , final int len ) throws IOException {
194+ checkThreshold (len );
195+ getStream ().write (b , off , len );
196+ written += len ;
197+ }
198+
199+ /**
200+ * Writes the specified byte to this output stream.
201+ *
202+ * @param b The byte to be written.
203+ *
204+ * @throws IOException if an error occurs.
205+ */
206+ @ SuppressWarnings ("resource" ) // the underlying stream is managed by a subclass.
207+ @ Override
208+ public void write (final int b ) throws IOException {
209+ checkThreshold (1 );
210+ getStream ().write (b );
211+ written ++;
212+ }
225213}
0 commit comments