code: public void write(byte[] buf, int off, int len) throws IOException {
      while (true) {
        lockW.lock();
        try {
          // fail if closed
          checkClosed();
          
          final int lenToWrite = Math.min(len, length - availableCount.get());
          final int lenForward = Math.min(lenToWrite, length - writeCursor);
          final int lenRemaining = lenToWrite - lenForward;
          
          // after writeCursor
          if (lenForward > 0) {
            System.arraycopy(buf, off, bytes, writeCursor, lenForward);
            incWriteCursor(lenForward);
          }
          
          // before writeCursor
          if (lenRemaining > 0) {
            System.arraycopy(buf, off + lenForward, bytes, 0, lenRemaining);
            incWriteCursor(lenRemaining);
          } 
          
          availableCount.addAndGet(lenToWrite);
          totalWritten += lenToWrite;
          
          // modify offset and len for next iteration
          off += lenToWrite;
          len -= lenToWrite;        
          if (len == 0) {
            return;
          }
        } finally {
          lockW.unlock();
        }
        sleep(1);
      }
    }
docs: Write buf to the buffer, will block until it can write len bytes.
  Will fail if buffer is closed.