code: private void throttledTransfer(MockResponse policy, Socket socket, BufferedSource source,
        BufferedSink sink, long byteCount, boolean isRequest) throws IOException {
      if (byteCount == 0) return;
  
      Buffer buffer = new Buffer();
      long bytesPerPeriod = policy.getThrottleBytesPerPeriod();
      long periodDelayMs = policy.getThrottlePeriod(TimeUnit.MILLISECONDS);
  
      long halfByteCount = byteCount / 2;
      boolean disconnectHalfway = isRequest
          ? policy.getSocketPolicy() == DISCONNECT_DURING_REQUEST_BODY
          : policy.getSocketPolicy() == DISCONNECT_DURING_RESPONSE_BODY;
  
      while (!socket.isClosed()) {
        for (long b = 0; b < bytesPerPeriod; ) {
          // Ensure we do not read past the allotted bytes in this period.
          long toRead = Math.min(byteCount, bytesPerPeriod - b);
          // Ensure we do not read past halfway if the policy will kill the connection.
          if (disconnectHalfway) {
            toRead = Math.min(toRead, byteCount - halfByteCount);
          }
  
          long read = source.read(buffer, toRead);
          if (read == -1) return;
  
          sink.write(buffer, read);
          sink.flush();
          b += read;
          byteCount -= read;
  
          if (disconnectHalfway && byteCount == halfByteCount) {
            socket.close();
            return;
          }
  
          if (byteCount == 0) return;
        }
  
        if (periodDelayMs != 0) {
          try {
            Thread.sleep(periodDelayMs);
          } catch (InterruptedException e) {
            throw new AssertionError(e);
          }
        }
      }
    }
docs: Transfer bytes from {@code source} to {@code sink} until either {@code byteCount} bytes have
  been transferred or {@code source} is exhausted. The transfer is throttled according to {@code
  policy}.