private int maxAllowedToSet(StringSizePair filterKey) {
        Integer existingSizeInt = fieldSizes.get(filterKey.string);
        int existingSize = existingSizeInt == null ? 0 : existingSizeInt;

        //this is how much is allowed by the overall total limit
        int allowedByMaxTotal = maxTotalEstimatedSize - estimatedSize;

        //if we're overwriting a value, that value's data size is now available
        allowedByMaxTotal += existingSize;

        //if we're adding a key, we need to subtract that value
        allowedByMaxTotal -= existingSizeInt == null ? filterKey.size : 0;

        return Math.min(maxFieldSize, allowedByMaxTotal);
    }
@param maxKeySize maximum key size in UTF-16 bytes-- keys will be truncated to this
                  length; if less than 0, keys will not be truncated
@param maxEstimatedSize
@param includeFields if null or empty, all fields are included; otherwise, which fields
                     to add to the metadata object.
@param excludeFields these fields will not be included (unless they're in {@link StandardWriteFilter#ALWAYS_SET_FIELDS})
@param includeEmpty if <code>true</code>, this will set or add an empty value to the
                    metadata object.
tika-core/src/main/java/org/apache/tika/metadata/writefilter/StandardWriteFilter.java