private int writeToNewlyAllocatedRecord(final ByteBuffer content,
                                          final int requestedRecordCapacity) throws IOException {
    final int pageSize = pagedStorage.getPageSize();

    final int recordLength = content.limit();
    if (recordLength > maxCapacityForPageSize) {
      //Actually, at this point it must be guaranteed recordLength<=maxCapacityForPageSize, but lets check again:
      throw new IllegalStateException(
        "recordLength(=" + recordLength + ") > maxCapacityForPageSize(=" + maxCapacityForPageSize + ") -- can't fit");
    }
    final int implementableCapacity = Math.min(requestedRecordCapacity, maxCapacityForPageSize);
    checkCapacityHardLimit(implementableCapacity);


    final byte recordSizeType = recordSizeTypeByCapacity(implementableCapacity);
    final RecordLayout recordLayout = recordLayoutForType(recordSizeType);
    final int fullRecordSize = recordLayout.fullRecordSize(implementableCapacity);
    if (fullRecordSize > pageSize) {
      throw new IllegalArgumentException("record size(header:" + recordLayout.headerSize() + " + capacity:" + implementableCapacity + ")" +
                                         " should be <= pageSize(=" + pageSize + ")");
    }

    final IntRef actualRecordSizeRef = new IntRef();//actual record size may be >= requested totalRecordSize 
    final int newRecordId = allocateSlotForRecord(pageSize, fullRecordSize, actualRecordSizeRef);
    final long newRecordOffset = idToOffset(newRecordId);
    final int actualRecordSize = actualRecordSizeRef.get();
    final int actualRecordCapacity = actualRecordSize - recordLayout.headerSize();
    final int newRecordLength = content.remaining();

    //check everything before write anything:
    checkCapacityHardLimit(actualRecordCapacity);
    checkLengthHardLimit(newRecordLength);

    final int offsetOnPage = toOffsetOnPage(newRecordOffset);
    pagedStorage.lockWrite();
    try {
      final DirectBufferWrapper page = pagedStorage.getByteBuffer(newRecordOffset, /*forWrite: */ true);
      try {
        recordLayout.putRecord(page.getBuffer(), offsetOnPage,
                               actualRecordCapacity, newRecordLength, NULL_ID,
                               content);
        page.fileSizeMayChanged(offsetOnPage + actualRecordSize);
        page.markDirty();
        return newRecordId;
      }
      finally {
        page.unlock();
      }
    }
    finally {
      pagedStorage.unlockWrite();

      recordsAllocated.incrementAndGet();
      totalLiveRecordsCapacityBytes.addAndGet(actualRecordCapacity);
      totalLiveRecordsPayloadBytes.addAndGet(newRecordLength);
    }
  }
private int writeToNewlyAllocatedRecord(final ByteBuffer content,
                                          final int requestedRecordCapacity)