/** * Write a single byte to the delegate stream, possibly blocking if * necessary to ensure that throughput doesn't exceed the limits. * * @param b the byte to write. * @throws IOException if an I/O error occurs on the OutputStream. */ @Override public void write(final int b) throws IOException { throttle.request(1); //Note that _request never returns zero. delegate.write(b); }
/** * Read an array of bytes from this InputStream. * * @param data the bytes to read. * @param offset the index in the array to start at. * @param len the number of bytes to read. * @throws IOException if an I/O error occurs on the InputStream. */ @Override public int read(byte[] data, int offset, int len) throws IOException { return delegate.read(data, offset, throttle.request(len)); } }
/** * Write bytes[offset...offset+totalLength-1] to the delegate stream, * possibly blocking if necessary to ensure that throughput doesn't exceed * the limits. * * @param data the bytes to write. * @param offset the index in the array to start at. * @param totalLength the number of bytes to write. * @throws IOException if an I/O error occurs on the OutputStream. */ @Override public void write(byte[] data, int offset, int totalLength) throws IOException { //Note that we delegate directly to out. Do NOT call super.write(); //that calls this.write() resulting in HALF the throughput. while(totalLength > 0) { int length = throttle.request(totalLength); delegate.write(data, offset, length); totalLength -= length; offset += length; } } }