code: private int writeAtMost(ByteBuffer data, int offset, int length)
      {
          if (current >= bufferOffset + buffer.length)
              reBuffer();
  
          assert current < bufferOffset + buffer.length
          : String.format("File (%s) offset %d, buffer offset %d.", getPath(), current, bufferOffset);
  
  
          int toCopy = Math.min(length, buffer.length - bufferCursor());
  
          // copy bytes from external buffer
          ByteBufferUtil.arrayCopy(data, offset, buffer, bufferCursor(), toCopy);
  
          assert current <= bufferOffset + buffer.length
          : String.format("File (%s) offset %d, buffer offset %d.", getPath(), current, bufferOffset);
  
          validBufferBytes = Math.max(validBufferBytes, bufferCursor() + toCopy);
          current += toCopy;
  
          return toCopy;
      }
docs: /*
  Write at most "length" bytes from "data" starting at position "offset", and
  return the number of bytes written. caller is responsible for setting
  isDirty.