Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.apache.tomcat.util.http.fileupload;
 
An output stream which triggers an event when a specified number of bytes of data have been written to it. The event can be used, for example, to throw an exception if a maximum has been reached, or to switch the underlying stream type when the threshold is exceeded.

This class overrides all OutputStream methods. However, these overrides ultimately call the corresponding methods in the underlying output stream implementation.

NOTE: This implementation may trigger the event before the threshold is actually reached, since it triggers when a pending write operation would cause the threshold to be exceeded.

Author(s):
Martin Cooper
Version:
$Id: ThresholdingOutputStream.java 1281 2009-11-20 23:18:44Z remy.maucherat@jboss.com $
 
 public abstract class ThresholdingOutputStream
     extends OutputStream
 {
 
     // ----------------------------------------------------------- Data members
 

    
The threshold at which the event will be triggered.
 
     private int threshold;


    
The number of bytes written to the output stream.
 
     private long written;


    
Whether or not the configured threshold has been exceeded.
 
     private boolean thresholdExceeded;
 
 
     // ----------------------------------------------------------- Constructors
 

    
Constructs an instance of this class which will trigger an event at the specified threshold.

Parameters:
threshold The number of bytes at which to trigger an event.
 
     public ThresholdingOutputStream(int threshold)
     {
         this. = threshold;
     }
 
 
     // --------------------------------------------------- OutputStream methods
 

    
Writes the specified byte to this output stream.

Parameters:
b The byte to be written.
Throws:
java.io.IOException if an error occurs.
 
     @Override
     public void write(int bthrows IOException
     {
         checkThreshold(1);
         getStream().write(b);
         ++;
     }


    
Writes b.length bytes from the specified byte array to this output stream.

Parameters:
b The array of bytes to be written.
Throws:
java.io.IOException if an error occurs.
    @Override
    public void write(byte b[]) throws IOException
    {
        checkThreshold(b.length);
        getStream().write(b);
         += b.length;
    }


    
Writes len bytes from the specified byte array starting at offset off to this output stream.

Parameters:
b The byte array from which the data will be written.
off The start offset in the byte array.
len The number of bytes to write.
Throws:
java.io.IOException if an error occurs.
    @Override
    public void write(byte b[], int offint lenthrows IOException
    {
        checkThreshold(len);
        getStream().write(bofflen);
         += len;
    }


    
Flushes this output stream and forces any buffered output bytes to be written out.

Throws:
java.io.IOException if an error occurs.
    @Override
    public void flush() throws IOException
    {
        getStream().flush();
    }


    
Closes this output stream and releases any system resources associated with this stream.

Throws:
java.io.IOException if an error occurs.
    @Override
    public void close() throws IOException
    {
        try
        {
            flush();
        }
        catch (IOException ignored)
        {
            // ignore
        }
        getStream().close();
    }
    // --------------------------------------------------------- Public methods


    
Returns the threshold, in bytes, at which an event will be triggered.

Returns:
The threshold point, in bytes.
    public int getThreshold()
    {
        return ;
    }


    
Returns the number of bytes that have been written to this output stream.

Returns:
The number of bytes written.
    public long getByteCount()
    {
        return ;
    }


    
Determines whether or not the configured threshold has been exceeded for this output stream.

Returns:
true if the threshold has been reached; false otherwise.
    public boolean isThresholdExceeded()
    {
        return ( > );
    }
    // ------------------------------------------------------ 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.

Parameters:
count The number of bytes about to be written to the underlying output stream.
Throws:
java.io.IOException if an error occurs.
    protected void checkThreshold(int countthrows IOException
    {
        if (! && ( + count > ))
        {
             = true;
            thresholdReached();
        }
    }

    
Resets the byteCount to zero. You can call this from thresholdReached() if you want the event to be triggered again.
    protected void resetByteCount() 
    {
        this. = false;
        this. = 0;
    }
    // ------------------------------------------------------- Abstract methods


    
Returns the underlying output stream, to which the corresponding OutputStream methods in this class will ultimately delegate.

Returns:
The underlying output stream.
Throws:
java.io.IOException if an error occurs.
    protected abstract OutputStream getStream() throws IOException;


    
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:
java.io.IOException if an error occurs.
    protected abstract void thresholdReached() throws IOException;
New to GrepCode? Check out our FAQ X