5

I'd like to programmatically limit an upload or download operation in Java. I would assume that all I'd need to do was do check how fast the upload is going and insert Thread.sleep() accordingly like so:

while (file.hasMoreLines()) {
    String line = file.readLine();
    for (int i = 0; i < line.length(); i+=128) {
        outputStream.writeBytes(line.substr(i, i+128).getBytes());
        if (isHittingLimit())
            Thread.sleep(500);
    }
}

Will the above code work? If not, is there a better way to do this? Is there a tutorial which describes the theory?

Naftuli Kay
  • 87,710
  • 93
  • 269
  • 411
  • That will probably slow down the up/down load rate. Although depending how much data is sent on each iteration, and the length of the sleep period, it might be hard to limit accurately (i.e. the connection speed will change but it is not clear by how much). To make it finer tuned, in theory you would want to reduce both the amount of data sent at each iteration (from a line to a character for example) and the amount of time spent resting when a limit is hit. This should result in more frequent and finer adjustments... not posting as an answer since I have no tutorials and have not verified it. – gnomed Jun 07 '11 at 20:51
  • Also: it's not a good idea to use a Reader nor read by line since you are basically decoding data from bytes (UTF-8 or such) into characters, just to re-encode, take some of bytes, drop others. Rather, read certain number of bytes from InputStream, send those, and avoid decoding/encoding that is not needed here. – StaxMan Jun 07 '11 at 23:49

3 Answers3

7

Token Bucket Algorithm is a way to limit an upload or a download's bandwidth. You should read this article : it explains the use of this algorithm.

Using Guava RateLimiter :

// rate = 512 permits per second or 512 bytes per second in this case
final RateLimiter rateLimiter = RateLimiter.create(512.0); 

while (file.hasMoreLines()) {
    String line = file.readLine();
    for (int i = 0; i < line.length(); i+=128) {
        byte[] bytes = line.substr(i, i+128).getBytes();
        rateLimiter.acquire(bytes.length);
        outputStream.writeBytes(bytes);
    }
}

As explained in Guava docs: It is important to note that the number of permits requested never affects the throttling of the request itself (an invocation to acquire(1) and an invocation to acquire(1000) will result in exactly the same throttling, if any), but it affects the throttling of the next request. I.e., if an expensive task arrives at an idle RateLimiter, it will be granted immediately, but it is the next request that will experience extra throttling, thus paying for the cost of the expensive task.

Franck
  • 944
  • 14
  • 28
1

This is an old post, but how about this:

import com.google.common.util.concurrent.RateLimiter;
import java.io.IOException;
import java.io.OutputStream;

public final class ThrottledOutputStream extends OutputStream {
    private final OutputStream out;
    private final RateLimiter rateLimiter;

    public ThrottledOutputStream(OutputStream out, double bytesPerSecond) {
        this.out = out;
        this.rateLimiter = RateLimiter.create(bytesPerSecond);
    }

    public void setRate(double bytesPerSecond) {
        rateLimiter.setRate(bytesPerSecond);
    }

    @Override
    public void write(int b) throws IOException {
        rateLimiter.acquire();
        out.write(b);
    }

    @Override
    public void write(byte[] b) throws IOException {
        rateLimiter.acquire(b.length);
        out.write(b);
    }

    @Override
    public void write(byte[] b, int off, int len) throws IOException {
        rateLimiter.acquire(len);
        out.write(b, off, len);
    }

    @Override
    public void flush() throws IOException {
        out.flush();
    }

    @Override
    public void close() throws IOException {
        out.close();
    }
}

Depends on Guava, specifically the RateLimiter.

gub
  • 5,079
  • 3
  • 28
  • 33
0

You'll need some way for isHittingLimit to know how many bytes have been transmitted over how long. There's an interesting approach in this thread that you may be able to adapt.

Community
  • 1
  • 1
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521