allow the stream to optionally pull from the output stream's bandwidth limit queue (useful in very strange situations)
This commit is contained in:
@ -18,30 +18,52 @@ 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 {
|
||||||
|
if (_pullFromOutbound)
|
||||||
|
_context.bandwidthLimiter().delayOutbound(_peer, 1);
|
||||||
|
else
|
||||||
_context.bandwidthLimiter().delayInbound(_peer, 1);
|
_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);
|
||||||
|
if (_pullFromOutbound)
|
||||||
|
_context.bandwidthLimiter().delayOutbound(_peer, read);
|
||||||
|
else
|
||||||
_context.bandwidthLimiter().delayInbound(_peer, read);
|
_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);
|
||||||
|
if (_pullFromOutbound)
|
||||||
|
_context.bandwidthLimiter().delayOutbound(_peer, read);
|
||||||
|
else
|
||||||
_context.bandwidthLimiter().delayInbound(_peer, read);
|
_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);
|
||||||
|
if (_pullFromOutbound)
|
||||||
|
_context.bandwidthLimiter().delayOutbound(_peer, (int)skip);
|
||||||
|
else
|
||||||
_context.bandwidthLimiter().delayInbound(_peer, (int)skip);
|
_context.bandwidthLimiter().delayInbound(_peer, (int)skip);
|
||||||
return skip;
|
return skip;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user