Android: How To Limit Download Speed
I use AndroidHttpClient to download videos from internet. There are ways to increase download speed, but I want to know how to slow down the download speed using AndroidHttpClient
Solution 1:
Slowing the download speed is quite simple. Just read from the InputStream more slowly:
publicbyte[] rateLimitedDownload(InputStream in, int bytesPerSecond) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = newbyte[bytesPerSecond];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// nothing
}
}
returnout.toByteArray();
}
This code isn't perfect - any time spent actually reading from in is not considered, and reads may be less than expected. It should give you an idea though.
Post a Comment for "Android: How To Limit Download Speed"