This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-io.git
commit e1933483fa783dc163070f4336c902923b9a982d Author: Gary Gregory <gardgreg...@gmail.com> AuthorDate: Sun May 16 07:30:02 2021 -0400 Sort members. --- .../io/output/ThresholdingOutputStream.java | 154 ++++++++++----------- 1 file changed, 71 insertions(+), 83 deletions(-) diff --git a/src/main/java/org/apache/commons/io/output/ThresholdingOutputStream.java b/src/main/java/org/apache/commons/io/output/ThresholdingOutputStream.java index 3c98108..e432cd4 100644 --- a/src/main/java/org/apache/commons/io/output/ThresholdingOutputStream.java +++ b/src/main/java/org/apache/commons/io/output/ThresholdingOutputStream.java @@ -34,8 +34,6 @@ import java.io.OutputStream; */ public abstract class ThresholdingOutputStream extends OutputStream { - // ----------------------------------------------------------- Data members - /** * The threshold at which the event will be triggered. */ @@ -51,8 +49,6 @@ public abstract class ThresholdingOutputStream extends OutputStream { */ private boolean thresholdExceeded; - // ----------------------------------------------------------- Constructors - /** * Constructs an instance of this class which will trigger an event at the specified threshold. * @@ -62,82 +58,65 @@ public abstract class ThresholdingOutputStream extends OutputStream { this.threshold = threshold; } - // --------------------------------------------------- OutputStream methods - /** - * Writes the specified byte to this output stream. + * Checks to see if writing the specified number of bytes would cause the configured threshold to be exceeded. If + * so, triggers an event to allow a concrete implementation to take action on this. * - * @param b The byte to be written. + * @param count The number of bytes about to be written to the underlying output stream. * * @throws IOException if an error occurs. */ - @SuppressWarnings("resource") // the underlying stream is managed by a subclass. - @Override - public void write(final int b) throws IOException { - checkThreshold(1); - getStream().write(b); - written++; + protected void checkThreshold(final int count) throws IOException { + if (!thresholdExceeded && written + count > threshold) { + thresholdExceeded = true; + thresholdReached(); + } } /** - * Writes {@code b.length} bytes from the specified byte array to this output stream. - * - * @param b The array of bytes to be written. + * Closes this output stream and releases any system resources associated with this stream. * * @throws IOException if an error occurs. */ - @SuppressWarnings("resource") // the underlying stream is managed by a subclass. @Override - public void write(final byte[] b) throws IOException { - checkThreshold(b.length); - getStream().write(b); - written += b.length; + public void close() throws IOException { + try { + flush(); + } catch (final IOException ignored) { + // ignore + } + getStream().close(); } /** - * Writes {@code len} bytes from the specified byte array starting at offset {@code off} to this output stream. - * - * @param b The byte array from which the data will be written. - * @param off The start offset in the byte array. - * @param len The number of bytes to write. + * Flushes this output stream and forces any buffered output bytes to be written out. * * @throws IOException if an error occurs. */ @SuppressWarnings("resource") // the underlying stream is managed by a subclass. @Override - public void write(final byte[] b, final int off, final int len) throws IOException { - checkThreshold(len); - getStream().write(b, off, len); - written += len; + public void flush() throws IOException { + getStream().flush(); } /** - * Flushes this output stream and forces any buffered output bytes to be written out. + * Returns the number of bytes that have been written to this output stream. * - * @throws IOException if an error occurs. + * @return The number of bytes written. */ - @SuppressWarnings("resource") // the underlying stream is managed by a subclass. - @Override - public void flush() throws IOException { - getStream().flush(); + public long getByteCount() { + return written; } /** - * Closes this output stream and releases any system resources associated with this stream. + * Returns the underlying output stream, to which the corresponding {@code OutputStream} methods in this class will + * ultimately delegate. + * + * @return The underlying output stream. * * @throws IOException if an error occurs. */ - @Override - public void close() throws IOException { - try { - flush(); - } catch (final IOException ignored) { - // ignore - } - getStream().close(); - } - - // --------------------------------------------------------- Public methods + protected abstract OutputStream getStream() throws IOException; /** * Returns the threshold, in bytes, at which an event will be triggered. @@ -149,15 +128,6 @@ public abstract class ThresholdingOutputStream extends OutputStream { } /** - * Returns the number of bytes that have been written to this output stream. - * - * @return The number of bytes written. - */ - public long getByteCount() { - return written; - } - - /** * Determines whether or not the configured threshold has been exceeded for this output stream. * * @return {@code true} if the threshold has been reached; {@code false} otherwise. @@ -166,23 +136,6 @@ public abstract class ThresholdingOutputStream extends OutputStream { return written > threshold; } - // ------------------------------------------------------ Protected methods - - /** - * Checks to see if writing the specified number of bytes would cause the configured threshold to be exceeded. If - * so, triggers an event to allow a concrete implementation to take action on this. - * - * @param count The number of bytes about to be written to the underlying output stream. - * - * @throws IOException if an error occurs. - */ - protected void checkThreshold(final int count) throws IOException { - if (!thresholdExceeded && written + count > threshold) { - thresholdExceeded = true; - thresholdReached(); - } - } - /** * Resets the byteCount to zero. You can call this from {@link #thresholdReached()} if you want the event to be * triggered again. @@ -203,23 +156,58 @@ public abstract class ThresholdingOutputStream extends OutputStream { this.written = count; } - // ------------------------------------------------------- Abstract methods + /** + * Indicates that the configured threshold has been reached, and that a subclass should take whatever action + * necessary on this event. This may include changing the underlying output stream. + * + * @throws IOException if an error occurs. + */ + protected abstract void thresholdReached() throws IOException; /** - * Returns the underlying output stream, to which the corresponding {@code OutputStream} methods in this class will - * ultimately delegate. + * Writes {@code b.length} bytes from the specified byte array to this output stream. * - * @return The underlying output stream. + * @param b The array of bytes to be written. * * @throws IOException if an error occurs. */ - protected abstract OutputStream getStream() throws IOException; + @SuppressWarnings("resource") // the underlying stream is managed by a subclass. + @Override + public void write(final byte[] b) throws IOException { + checkThreshold(b.length); + getStream().write(b); + written += b.length; + } /** - * Indicates that the configured threshold has been reached, and that a subclass should take whatever action - * necessary on this event. This may include changing the underlying output stream. + * Writes {@code len} bytes from the specified byte array starting at offset {@code off} to this output stream. + * + * @param b The byte array from which the data will be written. + * @param off The start offset in the byte array. + * @param len The number of bytes to write. * * @throws IOException if an error occurs. */ - protected abstract void thresholdReached() throws IOException; + @SuppressWarnings("resource") // the underlying stream is managed by a subclass. + @Override + public void write(final byte[] b, final int off, final int len) throws IOException { + checkThreshold(len); + getStream().write(b, off, len); + written += len; + } + + /** + * Writes the specified byte to this output stream. + * + * @param b The byte to be written. + * + * @throws IOException if an error occurs. + */ + @SuppressWarnings("resource") // the underlying stream is managed by a subclass. + @Override + public void write(final int b) throws IOException { + checkThreshold(1); + getStream().write(b); + written++; + } }