Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Read it more slowly so as not to overload the system
public class SlowInputStream extends BufferedInputStream {
private final long start;
private final double rate;
private long read;
public synchronized int read(byte[] b, int off, int len) throws IOException {
int bytes =, off, len);
read += bytes;
return bytes;
public synchronized int read() throws IOException {
private void maybeWait() {
double millis = read / rate - (System.currentTimeMillis() - start);
if (millis > 0) {
try {
Thread.sleep((long) millis);
} catch (InterruptedException e) {
// Continue
public SlowInputStream(InputStream in, long length, int time, TimeUnit timeUnit) {
rate = ((double) length) / timeUnit.toMillis(time);
start = System.currentTimeMillis();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.