Class InputSize

java.lang.Object
dev.omnist.document.InputSize

public final class InputSize extends Object
The one place the input-size bound lives (omnist-spec section 2.4.2, D-23 to D-26).

A reader refuses an input of more than Limits.maxInputBytes() bytes with the code "document.limit.input-size" at path $, before decoding or parsing; an input of exactly the maximum is accepted. The unit is bytes, not characters, and the count is taken on the input as received: a leading byte-order mark counts (three bytes), because the check runs before the BOM is stripped.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    document.limit.input-size: the input is larger than the maximum (D-23).
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    checkLength(long length, int maxBytes, String advice)
    Checks the size of a byte array.
    static boolean
    exceeds(String text, int maxBytes)
    Whether text is more than maxBytes UTF-8 bytes long.
    static String
    message(String text, int maxBytes)
    The message of a refusal for text of known size.
    static byte[]
    readBounded(InputStream in, int maxBytes, String advice)
    Reads in to its end, stopping as soon as it is known to hold more than maxBytes: at most maxBytes + 1 bytes are ever buffered.
    refusal(String text, int maxBytes)
    The refusal for text over the maximum.
    static long
    The length of text in UTF-8 bytes, counted without allocating an encoded copy.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • CODE

      public static final String CODE
      document.limit.input-size: the input is larger than the maximum (D-23).
      See Also:
  • Method Details

    • utf8Length

      public static long utf8Length(CharSequence text)
      The length of text in UTF-8 bytes, counted without allocating an encoded copy. A lone surrogate has no UTF-8 encoding and is counted as the three bytes of U+FFFD.
      Parameters:
      text - the text
      Returns:
      its UTF-8 length
    • exceeds

      public static boolean exceeds(String text, int maxBytes)
      Whether text is more than maxBytes UTF-8 bytes long. Decides without a scan when the character count alone settles it: every character is at least one byte (a surrogate pair is two characters and four bytes), and at most three bytes per character.
      Parameters:
      text - the text, or null (never over the limit)
      maxBytes - the maximum
      Returns:
      true if the text is over the maximum
    • message

      public static String message(String text, int maxBytes)
      The message of a refusal for text of known size.
      Parameters:
      text - the refused text
      maxBytes - the maximum it exceeded
      Returns:
      the message
    • refusal

      public static DocumentParseException refusal(String text, int maxBytes)
      The refusal for text over the maximum.
      Parameters:
      text - the refused text
      maxBytes - the maximum it exceeded
      Returns:
      the exception to throw
    • readBounded

      public static byte[] readBounded(InputStream in, int maxBytes, String advice) throws IOException
      Reads in to its end, stopping as soon as it is known to hold more than maxBytes: at most maxBytes + 1 bytes are ever buffered.
      Parameters:
      in - the stream
      maxBytes - the maximum accepted size
      advice - appended to the refusal message, saying how to raise the maximum
      Returns:
      the bytes read, at most maxBytes of them
      Throws:
      IOException - if the stream fails
      DocumentParseException - with code "document.limit.input-size" at $ if the stream holds more
    • checkLength

      public static void checkLength(long length, int maxBytes, String advice)
      Checks the size of a byte array.
      Parameters:
      length - the length of the input in bytes
      maxBytes - the maximum accepted size
      advice - appended to the refusal message, saying how to raise the maximum
      Throws:
      DocumentParseException - with code "document.limit.input-size" at $ if length > maxBytes