com.limegroup.gnutella.util
Class ThrottledOutputStream

java.lang.Object
  extended byjava.io.OutputStream
      extended bycom.limegroup.gnutella.util.ThrottledOutputStream

public class ThrottledOutputStream
extends java.io.OutputStream

Wraps a stream to ensure that the caller can write no more than N bytes/sec. ThrottledOutputStream delegates to a BandwidthThrottle object to control throughput. By sharing a single BandwidthThrottle among multiple ThrottledOutputStream instances, the user can approximate fair global bandwidth sharing.

This implementation is based on the ThrottledOutputStream class from the Freenet project. It has been modified so that the bandwidth throttle is no longer static. It also no longer subclasses FilterOutputStream, as the temptation to call super.write() introduced some bugs.


Constructor Summary
ThrottledOutputStream(java.io.OutputStream delegate, BandwidthThrottle throttle)
          Wraps the delegate stream with the given throttle.
 
Method Summary
 void close()
           
 void flush()
           
 void write(byte[] data)
          Write the given bytes to the delegate stream, possibly blocking if necessary to ensure that throughput doesn't exceed the limits.
 void write(byte[] data, int offset, int totalLength)
          Write bytes[offset...offset+totalLength-1] to the delegate stream, possibly blocking if necessary to ensure that throughput doesn't exceed the limits.
 void write(int b)
          Write a single byte to the delegate stream, possibly blocking if necessary to ensure that throughput doesn't exceed the limits.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ThrottledOutputStream

public ThrottledOutputStream(java.io.OutputStream delegate,
                             BandwidthThrottle throttle)
Wraps the delegate stream with the given throttle.

Parameters:
delegate - the underlying stream for all IO
throttle - limits throughput. May be shared with other streams.
Method Detail

write

public void write(int b)
           throws java.io.IOException
Write a single byte to the delegate stream, possibly blocking if necessary to ensure that throughput doesn't exceed the limits.

Parameters:
b - the byte to write.
Throws:
java.io.IOException - if an I/O error occurs on the OutputStream.

write

public void write(byte[] data,
                  int offset,
                  int totalLength)
           throws java.io.IOException
Write bytes[offset...offset+totalLength-1] to the delegate stream, possibly blocking if necessary to ensure that throughput doesn't exceed the limits.

Parameters:
data - the bytes to write.
offset - the index in the array to start at.
totalLength - the number of bytes to write.
Throws:
java.io.IOException - if an I/O error occurs on the OutputStream.

write

public void write(byte[] data)
           throws java.io.IOException
Write the given bytes to the delegate stream, possibly blocking if necessary to ensure that throughput doesn't exceed the limits.

Throws:
java.io.IOException

flush

public void flush()
           throws java.io.IOException
Throws:
java.io.IOException

close

public void close()
           throws java.io.IOException
Throws:
java.io.IOException