Class ResourceLimits

java.lang.Object
opennlp.tools.util.ResourceLimits

public final class ResourceLimits extends Object
Shared upper bounds for counts read from user-supplied resources, so a crafted file cannot force an outsized allocation before validation completes.
Since:
3.0.0
  • Field Details

    • MAX_ENTRIES_PROPERTY

      public static final String MAX_ENTRIES_PROPERTY
      System property for overriding MAX_ENTRIES. Set at JVM startup, e.g. -DOPENNLP_MAX_ENTRIES=5000000. Falls back to 10_000_000 if absent or invalid.
      See Also:
    • MAX_ENTRIES

      public static final int MAX_ENTRIES
      Upper bound on count fields and resource sizes that drive allocations (matrix dimensions, lexicon entries, model outcome counts, and similar). Configurable via MAX_ENTRIES_PROPERTY.
    • MAX_MATRIX_CELLS_PROPERTY

      public static final String MAX_MATRIX_CELLS_PROPERTY
      System property for overriding MAX_MATRIX_CELLS. Set at JVM startup, e.g. -Dopennlp.max.matrix.cells=20000000. Falls back to 134_217_728 if absent or invalid.
      See Also:
    • MAX_MATRIX_CELLS

      public static final int MAX_MATRIX_CELLS
      Upper bound on the cell count of a two-dimensional cost table, whose entries are far smaller than the record-sized entries MAX_ENTRIES bounds. The default of 2^27 cells caps a 16-bit cost matrix at 256 MiB, which admits every published MeCab-format distribution (mecab-ko-dic 2.1.1 alone declares 3822 x 2693, above MAX_ENTRIES) while still refusing the roughly 4 GiB allocation a crafted 46340 46340 header would force. Configurable via MAX_MATRIX_CELLS_PROPERTY.
  • Method Details

    • initLimit

      @Internal public static long initLimit(String property, long defaultValue)
      Reads a positive long limit from a system property. The value is trimmed before parsing.
      Parameters:
      property - The system property name. Must not be null.
      defaultValue - The value used when the property is absent, not a number, or not positive.
      Returns:
      The configured limit, or defaultValue.
      Throws:
      IllegalArgumentException - Thrown if property is null.
    • requireWithinMaxEntries

      @Internal public static void requireWithinMaxEntries(long count, String label)
      Checks a count read from a resource against MAX_ENTRIES.
      Parameters:
      count - The count to check.
      label - Describes the count in the error message, e.g. "Outcome count". Must not be null.
      Throws:
      IllegalArgumentException - Thrown if label is null, or if count is negative or exceeds MAX_ENTRIES.
    • requireWithinMaxEntries

      @Internal public static <E extends Exception> void requireWithinMaxEntries(long count, String label, Function<String,E> exceptionFactory) throws E
      Checks a count read from a resource against MAX_ENTRIES and reports a violation through an exception of the caller's choice.
      Type Parameters:
      E - The type of exception thrown on a violation.
      Parameters:
      count - The count to check.
      label - Describes the count in the error message, e.g. "unigram count". Must not be null.
      exceptionFactory - Creates the exception from the error message. Must not be null.
      Throws:
      E - Thrown if count is negative or exceeds MAX_ENTRIES.
      IllegalArgumentException - Thrown if label or exceptionFactory is null.