private int findAttributePage(final PersistentFSConnection connection,
                                final int fileId,
                                final @NotNull FileAttribute attribute,
                                final boolean toWrite) throws IOException {
    PersistentFSConnection.ensureIdIsValid(fileId);

    final int encodedAttrId = connection.getAttributeId(attribute.getId());

    int recordId = fileAttributeRecordId(connection, fileId);
    boolean directoryRecord = false;

    if (recordId == NON_EXISTENT_ATTRIBUTE_RECORD_ID) {
      if (!toWrite) {
        return NON_EXISTENT_ATTRIBUTE_RECORD_ID;
      }

      recordId = attributesBlobStorage.createNewRecord();
      updateFileAttributeRecordId(connection, fileId, recordId);
      directoryRecord = true;
    }
    else {
      try (DataInputStream attrRefs = attributesBlobStorage.readStream(recordId)) {
        if (bulkAttrReadSupport) {
          skipRecordHeader(attrRefs, PersistentFSConnection.RESERVED_ATTR_ID, fileId);
        }

        while (attrRefs.available() > 0) {
          final int attIdOnPage = DataInputOutputUtil.readINT(attrRefs);
          final int attrAddressOrSize = DataInputOutputUtil.readINT(attrRefs);

          if (attIdOnPage == encodedAttrId) {
            if (inlineAttributes) {
              if (attrAddressOrSize < INLINE_ATTRIBUTE_SMALLER_THAN) {
                //return record id, but negated, so clients understand that it is not dedicated attr record, but
                // directory record, with attribute inlined:
                return -recordId;
              }
              else {
                return attrAddressOrSize - INLINE_ATTRIBUTE_SMALLER_THAN;
              }
            }
            else {
              return attrAddressOrSize;
            }
          }
          else {
            if (inlineAttributes && attrAddressOrSize < INLINE_ATTRIBUTE_SMALLER_THAN) {
              attrRefs.skipBytes(attrAddressOrSize);
            }
          }
        }
      }
    }

    if (toWrite) {
      try (AbstractStorage.AppenderStream appender = attributesBlobStorage.appendStream(recordId)) {
        if (bulkAttrReadSupport) {
          if (directoryRecord) {
            DataInputOutputUtil.writeINT(appender, PersistentFSConnection.RESERVED_ATTR_ID);
            DataInputOutputUtil.writeINT(appender, fileId);
          }
        }

        DataInputOutputUtil.writeINT(appender, encodedAttrId);
        int attrAddress = attributesBlobStorage.createNewRecord();
        DataInputOutputUtil.writeINT(appender, inlineAttributes ? attrAddress + INLINE_ATTRIBUTE_SMALLER_THAN : attrAddress);
        PersistentFSConnection.REASONABLY_SMALL.attrPageRequested = true;
        return attrAddress;
      }
      finally {
        PersistentFSConnection.REASONABLY_SMALL.attrPageRequested = false;
      }
    }

    return NON_EXISTENT_ATTRIBUTE_RECORD_ID;
  }
private int findAttributePage(final PersistentFSConnection connection,
                                final int fileId,
                                final @NotNull FileAttribute attribute,
                                final boolean toWrite)