14fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy/*
24fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * Licensed to the Apache Software Foundation (ASF) under one or more
34fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * contributor license agreements.  See the NOTICE file distributed with
44fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * this work for additional information regarding copyright ownership.
54fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * The ASF licenses this file to You under the Apache License, Version 2.0
64fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * (the "License"); you may not use this file except in compliance with
74fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * the License.  You may obtain a copy of the License at
84fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *
94fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *      http://www.apache.org/licenses/LICENSE-2.0
104fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *
114fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * Unless required by applicable law or agreed to in writing, software
124fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * distributed under the License is distributed on an "AS IS" BASIS,
134fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
144fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * See the License for the specific language governing permissions and
154fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * limitations under the License.
164fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy */
174fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedypackage org.apache.commons.io.output;
184fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
194fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedyimport java.io.IOException;
204fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedyimport java.io.OutputStream;
214fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
224fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
234fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy/**
244fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * An output stream which triggers an event when a specified number of bytes of
254fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * data have been written to it. The event can be used, for example, to throw
264fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * an exception if a maximum has been reached, or to switch the underlying
274fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * stream type when the threshold is exceeded.
284fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * <p>
294fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * This class overrides all <code>OutputStream</code> methods. However, these
304fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * overrides ultimately call the corresponding methods in the underlying output
314fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * stream implementation.
324fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * <p>
334fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * NOTE: This implementation may trigger the event <em>before</em> the threshold
344fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * is actually reached, since it triggers when a pending write operation would
354fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * cause the threshold to be exceeded.
364fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *
374fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * @author <a href="mailto:martinc@apache.org">Martin Cooper</a>
384fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *
394fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * @version $Id: ThresholdingOutputStream.java 540714 2007-05-22 19:39:44Z niallp $
404fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy */
414fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedypublic abstract class ThresholdingOutputStream
424fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    extends OutputStream
434fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy{
444fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
454fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    // ----------------------------------------------------------- Data members
464fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
474fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
484fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
494fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * The threshold at which the event will be triggered.
504fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
514fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    private int threshold;
524fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
534fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
544fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
554fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * The number of bytes written to the output stream.
564fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
574fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    private long written;
584fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
594fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
604fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
614fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Whether or not the configured threshold has been exceeded.
624fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
634fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    private boolean thresholdExceeded;
644fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
654fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
664fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    // ----------------------------------------------------------- Constructors
674fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
684fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
694fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
704fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Constructs an instance of this class which will trigger an event at the
714fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * specified threshold.
724fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
734fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @param threshold The number of bytes at which to trigger an event.
744fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
754fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public ThresholdingOutputStream(int threshold)
764fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    {
774fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        this.threshold = threshold;
784fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
794fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
804fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
814fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    // --------------------------------------------------- OutputStream methods
824fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
834fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
844fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
854fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Writes the specified byte to this output stream.
864fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
874fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @param b The byte to be written.
884fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
894fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @exception IOException if an error occurs.
904fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
914fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public void write(int b) throws IOException
924fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    {
934fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        checkThreshold(1);
944fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        getStream().write(b);
954fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        written++;
964fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
974fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
984fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
994fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
1004fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Writes <code>b.length</code> bytes from the specified byte array to this
1014fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * output stream.
1024fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
1034fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @param b The array of bytes to be written.
1044fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
1054fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @exception IOException if an error occurs.
1064fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
1074fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public void write(byte b[]) throws IOException
1084fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    {
1094fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        checkThreshold(b.length);
1104fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        getStream().write(b);
1114fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        written += b.length;
1124fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
1134fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
1144fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
1154fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
1164fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Writes <code>len</code> bytes from the specified byte array starting at
1174fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * offset <code>off</code> to this output stream.
1184fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
1194fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @param b   The byte array from which the data will be written.
1204fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @param off The start offset in the byte array.
1214fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @param len The number of bytes to write.
1224fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
1234fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @exception IOException if an error occurs.
1244fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
1254fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public void write(byte b[], int off, int len) throws IOException
1264fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    {
1274fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        checkThreshold(len);
1284fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        getStream().write(b, off, len);
1294fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        written += len;
1304fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
1314fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
1324fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
1334fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
1344fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Flushes this output stream and forces any buffered output bytes to be
1354fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * written out.
1364fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
1374fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @exception IOException if an error occurs.
1384fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
1394fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public void flush() throws IOException
1404fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    {
1414fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        getStream().flush();
1424fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
1434fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
1444fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
1454fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
1464fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Closes this output stream and releases any system resources associated
1474fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * with this stream.
1484fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
1494fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @exception IOException if an error occurs.
1504fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
1514fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public void close() throws IOException
1524fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    {
1534fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        try
1544fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        {
1554fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy            flush();
1564fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        }
1574fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        catch (IOException ignored)
1584fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        {
1594fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy            // ignore
1604fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        }
1614fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        getStream().close();
1624fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
1634fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
1644fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
1654fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    // --------------------------------------------------------- Public methods
1664fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
1674fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
1684fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
1694fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Returns the threshold, in bytes, at which an event will be triggered.
1704fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
1714fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @return The threshold point, in bytes.
1724fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
1734fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public int getThreshold()
1744fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    {
1754fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        return threshold;
1764fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
1774fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
1784fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
1794fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
1804fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Returns the number of bytes that have been written to this output stream.
1814fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
1824fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @return The number of bytes written.
1834fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
1844fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public long getByteCount()
1854fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    {
1864fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        return written;
1874fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
1884fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
1894fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
1904fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
1914fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Determines whether or not the configured threshold has been exceeded for
1924fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * this output stream.
1934fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
1944fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @return <code>true</code> if the threshold has been reached;
1954fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *         <code>false</code> otherwise.
1964fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
1974fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public boolean isThresholdExceeded()
1984fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    {
1994fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        return (written > threshold);
2004fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
2014fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
2024fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
2034fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    // ------------------------------------------------------ Protected methods
2044fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
2054fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
2064fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
2074fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Checks to see if writing the specified number of bytes would cause the
2084fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * configured threshold to be exceeded. If so, triggers an event to allow
2094fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * a concrete implementation to take action on this.
2104fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
2114fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @param count The number of bytes about to be written to the underlying
2124fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *              output stream.
2134fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
2144fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @exception IOException if an error occurs.
2154fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
2164fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    protected void checkThreshold(int count) throws IOException
2174fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    {
2184fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        if (!thresholdExceeded && (written + count > threshold))
2194fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        {
2204fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy            thresholdExceeded = true;
2214fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy            thresholdReached();
2224fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        }
2234fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
2244fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
2254fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
2264fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Resets the byteCount to zero.  You can call this from
2274fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * {@link #thresholdReached()} if you want the event to be triggered again.
2284fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
2294fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    protected void resetByteCount()
2304fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    {
2314fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        this.thresholdExceeded = false;
2324fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        this.written = 0;
2334fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
2344fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
2354fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    // ------------------------------------------------------- Abstract methods
2364fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
2374fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
2384fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
2394fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Returns the underlying output stream, to which the corresponding
2404fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * <code>OutputStream</code> methods in this class will ultimately delegate.
2414fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
2424fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @return The underlying output stream.
2434fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
2444fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @exception IOException if an error occurs.
2454fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
2464fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    protected abstract OutputStream getStream() throws IOException;
2474fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
2484fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
2494fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
2504fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Indicates that the configured threshold has been reached, and that a
2514fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * subclass should take whatever action necessary on this event. This may
2524fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * include changing the underlying output stream.
2534fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
2544fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @exception IOException if an error occurs.
2554fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
2564fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    protected abstract void thresholdReached() throws IOException;
2574fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy}
258