ThresholdingOutputStream.java

00001 /*
00002  * $Header: /home/cvs/jakarta-commons/fileupload/src/java/org/apache/commons/fileupload/ThresholdingOutputStream.java,v 1.3 2003/05/31 22:31:08 martinc Exp $
00003  * $Revision: 1.3 $
00004  * $Date: 2003/05/31 22:31:08 $
00005  *
00006  * ====================================================================
00007  *
00008  * The Apache Software License, Version 1.1
00009  *
00010  * Copyright (c) 2001-2003 The Apache Software Foundation.  All rights
00011  * reserved.
00012  *
00013  * Redistribution and use in source and binary forms, with or without
00014  * modification, are permitted provided that the following conditions
00015  * are met:
00016  *
00017  * 1. Redistributions of source code must retain the above copyright
00018  *    notice, this list of conditions and the following disclaimer.
00019  *
00020  * 2. Redistributions in binary form must reproduce the above copyright
00021  *    notice, this list of conditions and the following disclaimer in
00022  *    the documentation and/or other materials provided with the
00023  *    distribution.
00024  *
00025  * 3. The end-user documentation included with the redistribution, if
00026  *    any, must include the following acknowlegement:
00027  *       "This product includes software developed by the
00028  *        Apache Software Foundation (http://www.apache.org/)."
00029  *    Alternately, this acknowlegement may appear in the software itself,
00030  *    if and wherever such third-party acknowlegements normally appear.
00031  *
00032  * 4. The names "The Jakarta Project", "Commons", and "Apache Software
00033  *    Foundation" must not be used to endorse or promote products derived
00034  *    from this software without prior written permission. For written
00035  *    permission, please contact apache@apache.org.
00036  *
00037  * 5. Products derived from this software may not be called "Apache"
00038  *    nor may "Apache" appear in their names without prior written
00039  *    permission of the Apache Group.
00040  *
00041  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
00042  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00043  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00044  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
00045  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00046  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00047  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
00048  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00049  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00050  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00051  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00052  * SUCH DAMAGE.
00053  * ====================================================================
00054  *
00055  * This software consists of voluntary contributions made by many
00056  * individuals on behalf of the Apache Software Foundation.  For more
00057  * information on the Apache Software Foundation, please see
00058  * <http://www.apache.org/>.
00059  *
00060  */
00061 
00062 
00063 package org.apache.commons.fileupload;
00064 
00065 import java.io.IOException;
00066 import java.io.OutputStream;
00067 
00068 
00087 public abstract class ThresholdingOutputStream
00088     extends OutputStream
00089 {
00090 
00091     // ----------------------------------------------------------- Data members
00092 
00093 
00097     private int threshold;
00098 
00099 
00103     private long written;
00104 
00105 
00109     private boolean thresholdExceeded;
00110 
00111 
00112     // ----------------------------------------------------------- Constructors
00113 
00114 
00121     public ThresholdingOutputStream(int threshold)
00122     {
00123         this.threshold = threshold;
00124     }
00125 
00126 
00127     // --------------------------------------------------- OutputStream methods
00128 
00129 
00137     public void write(int b) throws IOException
00138     {
00139         checkThreshold(1);
00140         getStream().write(b);
00141         written++;
00142     }
00143 
00144 
00153     public void write(byte b[]) throws IOException
00154     {
00155         checkThreshold(b.length);
00156         getStream().write(b);
00157         written += b.length;
00158     }
00159 
00160 
00171     public void write(byte b[], int off, int len) throws IOException
00172     {
00173         checkThreshold(len);
00174         getStream().write(b, off, len);
00175         written += len;
00176     }
00177 
00178 
00185     public void flush() throws IOException
00186     {
00187         getStream().flush();
00188     }
00189 
00190 
00197     public void close() throws IOException
00198     {
00199         try
00200         {
00201             flush();
00202         }
00203         catch (IOException ignored)
00204         {
00205             // ignore
00206         }
00207         getStream().close();
00208     }
00209 
00210 
00211     // --------------------------------------------------------- Public methods
00212 
00213 
00219     public int getThreshold()
00220     {
00221         return threshold;
00222     }
00223 
00224 
00230     public long getByteCount()
00231     {
00232         return written;
00233     }
00234 
00235 
00243     public boolean isThresholdExceeded()
00244     {
00245         return (written > threshold);
00246     }
00247 
00248 
00249     // ------------------------------------------------------ Protected methods
00250 
00251 
00262     protected void checkThreshold(int count) throws IOException
00263     {
00264         if (!thresholdExceeded && (written + count > threshold))
00265         {
00266             thresholdReached();
00267             thresholdExceeded = true;
00268         }
00269     }
00270 
00271 
00272     // ------------------------------------------------------- Abstract methods
00273 
00274 
00283     protected abstract OutputStream getStream() throws IOException;
00284 
00285 
00293     protected abstract void thresholdReached() throws IOException;
00294 }

Generated on Mon Jan 11 21:19:17 2010 for OpenMobileIS by  doxygen 1.5.4