allow the stream to optionally pull from the output stream's bandwidth limit queue (useful in very strange situations)

This commit is contained in:
jrandom
2004-06-13 19:36:41 +00:00
committed by zzz
parent f3154e8f5e
commit 8fd02ee8dd

View File

@ -18,31 +18,53 @@ import net.i2p.router.RouterContext;
public class BandwidthLimitedInputStream extends FilterInputStream { public class BandwidthLimitedInputStream extends FilterInputStream {
private RouterIdentity _peer; private RouterIdentity _peer;
private RouterContext _context; private RouterContext _context;
private boolean _pullFromOutbound;
public BandwidthLimitedInputStream(RouterContext context, InputStream source, RouterIdentity peer) { public BandwidthLimitedInputStream(RouterContext context, InputStream source, RouterIdentity peer) {
this(context, source, peer, false);
}
/**
* @param pullFromOutbound even though this is an input stream, if this is true, use the
* context's outbound bandwidth limiter queue for delays
*/
public BandwidthLimitedInputStream(RouterContext context, InputStream source, RouterIdentity peer, boolean pullFromOutbound) {
super(source); super(source);
_context = context; _context = context;
_peer = peer; _peer = peer;
_pullFromOutbound = pullFromOutbound;
} }
public int read() throws IOException { public int read() throws IOException {
_context.bandwidthLimiter().delayInbound(_peer, 1); if (_pullFromOutbound)
_context.bandwidthLimiter().delayOutbound(_peer, 1);
else
_context.bandwidthLimiter().delayInbound(_peer, 1);
return in.read(); return in.read();
} }
public int read(byte dest[]) throws IOException { public int read(byte dest[]) throws IOException {
int read = in.read(dest); int read = in.read(dest);
_context.bandwidthLimiter().delayInbound(_peer, read); if (_pullFromOutbound)
_context.bandwidthLimiter().delayOutbound(_peer, read);
else
_context.bandwidthLimiter().delayInbound(_peer, read);
return read; return read;
} }
public int read(byte dest[], int off, int len) throws IOException { public int read(byte dest[], int off, int len) throws IOException {
int read = in.read(dest, off, len); int read = in.read(dest, off, len);
_context.bandwidthLimiter().delayInbound(_peer, read); if (_pullFromOutbound)
_context.bandwidthLimiter().delayOutbound(_peer, read);
else
_context.bandwidthLimiter().delayInbound(_peer, read);
return read; return read;
} }
public long skip(long numBytes) throws IOException { public long skip(long numBytes) throws IOException {
long skip = in.skip(numBytes); long skip = in.skip(numBytes);
_context.bandwidthLimiter().delayInbound(_peer, (int)skip); if (_pullFromOutbound)
_context.bandwidthLimiter().delayOutbound(_peer, (int)skip);
else
_context.bandwidthLimiter().delayInbound(_peer, (int)skip);
return skip; return skip;
} }
} }