@Override public int read() throws IOException { // Wait until at least 1 byte is available if a limit is set if(bpsLimit>=0) getNbAllowedBytes(); // Read the byte from the underlying stream int i = in.read(); // Increase read counter by 1 if(i>0) addToLimitCounter(1); return i; }
@Override public long skip(long l) throws IOException { long nbSkipped = in.skip(bpsLimit>=0?Math.min(getNbAllowedBytes(),l):l); // Increase read counter by the number of bytes that have actually been skipped by the underlying stream if(nbSkipped>0) addToLimitCounter(nbSkipped); return nbSkipped; }
@Override public int read(byte[] bytes, int off, int len) throws IOException { int nbRead; // Wait until at least 1 byte is available if a limit is set and try to read as many bytes are available // without exceeding the throughput limit or the number specified if(bpsLimit>=0) nbRead = in.read(bytes, off, Math.min(getNbAllowedBytes(),len)); else nbRead = in.read(bytes, off, len); // Increase read counter by the number of bytes that have actually been read by the underlying stream if(nbRead>0) addToLimitCounter(nbRead); return nbRead; }