code: private void trim() throws IOException {
          if (isTrimShouldBeExecuted()) {
  
              /**
               * Need to store more bufs, may it is not possible to read out all
               * data at once. The available method only returns an int value
               * instead a long value. Store all read parts of the full buffer in
               * a deque.
               */
              final Deque<byte[]> tmpBuffer = new LinkedList<>();
  
              int available;
              // empty the current buffer, read out all bytes
              while ((available = is.available()) > 0) {
                  final byte[] buf = new byte[available];
                  // read out of the buffer
                  // and store the result to the tmpBuffer
                  int read = is.read(buf);
                  // should never happen
                  assert read == available : "Read not enough bytes from buffer.";
                  tmpBuffer.add(buf);
              }
              /**
               * Write all previously read parts back to the buffer. The buffer is
               * clean and contains no elements because all parts are read out.
               */
              try {
                  /**
                   * The reference to the buffer deque is not reachable out of the
                   * trim method. It is possible to ignore the safeWrite flag to
                   * prevent not necessary clone operations.
                   */
                  ignoreSafeWrite = true;
                  while (!tmpBuffer.isEmpty()) {
                      // pollFirst returns always a non null value, tmpBuffer is only filled with non null values
                      os.write(tmpBuffer.pollFirst());
                  }
              } finally {
                  ignoreSafeWrite = false;
              }
          }
      }
docs: This method trims the buffer. This method can be invoked after every
  write operation. The method checks itself if the buffer should be trimmed
  or not.