This commit is contained in:
jrandom
2004-06-13 19:37:18 +00:00
committed by zzz
parent 8fd02ee8dd
commit 698927bed4

View File

@ -14,20 +14,25 @@ import java.io.OutputStream;
import net.i2p.data.RouterIdentity; import net.i2p.data.RouterIdentity;
import net.i2p.router.RouterContext; import net.i2p.router.RouterContext;
import net.i2p.util.Log;
public class BandwidthLimitedOutputStream extends FilterOutputStream { public class BandwidthLimitedOutputStream extends FilterOutputStream {
private RouterIdentity _peer; private RouterIdentity _peer;
private RouterContext _context; private RouterContext _context;
private Log _log;
public BandwidthLimitedOutputStream(RouterContext context, OutputStream source, RouterIdentity peer) { public BandwidthLimitedOutputStream(RouterContext context, OutputStream source, RouterIdentity peer) {
super(source); super(source);
_context = context; _context = context;
_peer = peer; _peer = peer;
_log = context.logManager().getLog(BandwidthLimitedOutputStream.class);
} }
private final static int CHUNK_SIZE = 64; private final static int CHUNK_SIZE = 1024;
public void write(int val) throws IOException { public void write(int val) throws IOException {
if (_log.shouldLog(Log.DEBUG))
_log.debug("Writing a single byte!", new Exception("Single byte from..."));
_context.bandwidthLimiter().delayOutbound(_peer, 1); _context.bandwidthLimiter().delayOutbound(_peer, 1);
out.write(val); out.write(val);
} }
@ -43,6 +48,8 @@ public class BandwidthLimitedOutputStream extends FilterOutputStream {
} }
} }
public void write(byte src[], int off, int len) throws IOException { public void write(byte src[], int off, int len) throws IOException {
if (_log.shouldLog(Log.DEBUG))
_log.debug("Writing " + len + " bytes");
if (src == null) return; if (src == null) return;
if (len <= 0) return; if (len <= 0) return;
if (len <= CHUNK_SIZE) { if (len <= CHUNK_SIZE) {
@ -52,8 +59,8 @@ public class BandwidthLimitedOutputStream extends FilterOutputStream {
int i = 0; int i = 0;
while (i+CHUNK_SIZE < len) { while (i+CHUNK_SIZE < len) {
_context.bandwidthLimiter().delayOutbound(_peer, CHUNK_SIZE); _context.bandwidthLimiter().delayOutbound(_peer, CHUNK_SIZE);
out.write(src, off+i*CHUNK_SIZE, CHUNK_SIZE); out.write(src, off+i, CHUNK_SIZE);
i++; i += CHUNK_SIZE;
} }
int remainder = len % CHUNK_SIZE; int remainder = len % CHUNK_SIZE;
if (remainder != 0) { if (remainder != 0) {