code: public void write(byte[] buf, int offset, int length) throws IOException {
          if(buf == null) return;
          sanityCheck(buf, offset, length);
  
          lock.lock();
          try {
              if(closed) return;
              int bytes_written=0;
              while(bytes_written < length && !closed) {
  
                  if(read_pos > 0 && length - bytes_written > remaining())
                      compact();
  
                  if(remaining() > 0) {
                      int bytes_to_write=Math.min(length-bytes_written, remaining());
                      System.arraycopy(buf, offset+bytes_written, this.buf, write_pos, bytes_to_write);
                      write_pos+=bytes_to_write;
                      bytes_written+=bytes_to_write;
                      not_empty.signalAll();
                  }
                  else {
                      try {
                          not_full.await();
                      }
                      catch(InterruptedException e) {
                      }
                  }
              }
          }
          finally {
              lock.unlock();
          }
      }
docs: Appends bytes to the end of the stream. If the number of bytes to be written is greater than the remaining
  capacity, write() will block until the bytes can be added, or the stream is closed.
  <p/>
  This method will try to append partial buffers to the stream, e.g. if the remaining capacity is 2K, but the
  length of the buffer is 5K, 2K will be written and then write() will block until the remaining 3K can be
  added.
  @param buf The buffer to be added to the end of the stream
  @param offset The offset within buf at which bytes are read
  @param length The number of bytes to be added
  @throws IOException