Module bus.http

Class Builder

java.lang.Object
org.miaixz.bus.http.Builder

public class Builder extends Object
Utility class for HTTP-related operations.

Provides utility methods for handling HTTP requests and responses, including data parsing, encoding, collection operations, date formatting, and more.

Since:
Java 17+
Author:
Kimi Liu
  • Field Details

    • MAX_DATE

      public static final long MAX_DATE
      Maximum date value (December 31, 9999).
      See Also:
    • EMPTY_HEADERS

      public static final Headers EMPTY_HEADERS
      An empty Headers instance.
    • EMPTY_RESPONSE

      public static final ResponseBody EMPTY_RESPONSE
      An empty ResponseBody instance.
    • UTC

      public static final TimeZone UTC
      The UTC (Coordinated Universal Time) timezone.
    • NATURAL_ORDER

      public static final Comparator<String> NATURAL_ORDER
      A comparator that orders strings naturally.
    • QUOTED_STRING_DELIMITERS

      public static final org.miaixz.bus.core.io.ByteString QUOTED_STRING_DELIMITERS
      Byte string containing characters that delimit quoted strings.
    • TOKEN_DELIMITERS

      public static final org.miaixz.bus.core.io.ByteString TOKEN_DELIMITERS
      Byte string containing characters that delimit tokens.
    • BROWSER_COMPATIBLE_DATE_FORMAT_STRINGS

      public static final String[] BROWSER_COMPATIBLE_DATE_FORMAT_STRINGS
      Array of browser-compatible date format strings.
    • BROWSER_COMPATIBLE_DATE_FORMATS

      public static final DateFormat[] BROWSER_COMPATIBLE_DATE_FORMATS
      Array of browser-compatible date formatters.
    • CONNECT

      public static final String CONNECT
      The HTTP CONNECT method.
      See Also:
    • CONNECTED

      public static final String CONNECTED
      The STOMP CONNECTED frame command.
      See Also:
    • SEND

      public static final String SEND
      The STOMP SEND frame command.
      See Also:
    • MESSAGE

      public static final String MESSAGE
      The STOMP MESSAGE frame command.
      See Also:
    • SUBSCRIBE

      public static final String SUBSCRIBE
      The STOMP SUBSCRIBE frame command.
      See Also:
    • UNSUBSCRIBE

      public static final String UNSUBSCRIBE
      The STOMP UNSUBSCRIBE frame command.
      See Also:
    • ACK

      public static final String ACK
      The STOMP ACK frame command.
      See Also:
    • UNKNOWN

      public static final String UNKNOWN
      The STOMP UNKNOWN frame command.
      See Also:
    • ERROR

      public static final String ERROR
      The STOMP ERROR frame command.
      See Also:
  • Constructor Details

    • Builder

      public Builder()
      Constructs a new Builder instance.
  • Method Details

    • addSuppressedIfPossible

      public static void addSuppressedIfPossible(Throwable e, Throwable suppressed)
      Adds a suppressed exception to the given throwable if possible.
      Parameters:
      e - The primary throwable.
      suppressed - The throwable to be suppressed.
    • checkOffsetAndCount

      public static void checkOffsetAndCount(long arrayLength, long offset, long count)
      Checks if the given offset and count are within the bounds of an array of a specified length.
      Parameters:
      arrayLength - The total length of the array.
      offset - The starting offset.
      count - The number of elements.
      Throws:
      ArrayIndexOutOfBoundsException - If the offset or count are invalid for the given array length.
    • discard

      public static boolean discard(org.miaixz.bus.core.io.source.Source source, int timeout, TimeUnit timeUnit)
      Attempts to exhaust the given source within a specified timeout.
      Parameters:
      source - The source to discard.
      timeout - The maximum time to wait.
      timeUnit - The unit of time for the timeout.
      Returns:
      true if the source was successfully discarded, false otherwise.
    • skipAll

      public static boolean skipAll(org.miaixz.bus.core.io.source.Source source, int duration, TimeUnit timeUnit) throws IOException
      Skips all bytes from the source until it is exhausted or the timeout is reached.
      Parameters:
      source - The source to skip.
      duration - The maximum time to wait.
      timeUnit - The unit of time for the duration.
      Returns:
      true if all bytes were skipped, false if the timeout was reached.
      Throws:
      IOException - If an I/O error occurs during skipping.
    • immutableList

      public static <T> List<T> immutableList(List<T> list)
      Returns an immutable copy of the given list.
      Type Parameters:
      T - The type of elements in the list.
      Parameters:
      list - The list to make immutable.
      Returns:
      An immutable list.
    • immutableMap

      public static <K, V> Map<K,V> immutableMap(Map<K,V> map)
      Returns an immutable copy of the given map.
      Type Parameters:
      K - The type of keys in the map.
      V - The type of values in the map.
      Parameters:
      map - The map to make immutable.
      Returns:
      An immutable map.
    • immutableList

      public static <T> List<T> immutableList(T... elements)
      Returns an immutable list containing the given elements.
      Type Parameters:
      T - The type of elements.
      Parameters:
      elements - The elements to include in the list.
      Returns:
      An immutable list.
    • threadFactory

      public static ThreadFactory threadFactory(String name, boolean daemon)
      Creates a ThreadFactory that produces threads with a given name and daemon status.
      Parameters:
      name - The name prefix for the created threads.
      daemon - true if the created threads should be daemon threads, false otherwise.
      Returns:
      A ThreadFactory instance.
    • intersect

      public static String[] intersect(Comparator<? super String> comparator, String[] first, String[] second)
      Returns the intersection of two string arrays based on a given comparator.
      Parameters:
      comparator - The comparator to use for string comparison.
      first - The first array of strings.
      second - The second array of strings.
      Returns:
      A new array containing strings present in both input arrays.
    • nonEmptyIntersection

      public static boolean nonEmptyIntersection(Comparator<String> comparator, String[] first, String[] second)
      Checks if there is any common string between two arrays based on a given comparator.
      Parameters:
      comparator - The comparator to use for string comparison.
      first - The first array of strings.
      second - The second array of strings.
      Returns:
      true if an intersection exists, false otherwise.
    • hostHeader

      public static String hostHeader(UnoUrl url, boolean includeDefaultPort)
      Generates a host header string for a given URL.
      Parameters:
      url - The URL for which to generate the host header.
      includeDefaultPort - true to include the port even if it's the default for the scheme, false otherwise.
      Returns:
      The host header string.
    • indexOf

      public static int indexOf(Comparator<String> comparator, String[] array, String value)
      Finds the index of a string in an array using a custom comparator.
      Parameters:
      comparator - The comparator to use for string comparison.
      array - The array to search.
      value - The string value to find.
      Returns:
      The index of the value in the array, or -1 if not found.
    • concat

      public static String[] concat(String[] array, String value)
      Concatenates a string to an existing string array, returning a new array.
      Parameters:
      array - The original array.
      value - The string value to append.
      Returns:
      A new array with the appended string.
    • skipLeadingAsciiWhitespace

      public static int skipLeadingAsciiWhitespace(String input, int pos, int limit)
      Skips leading ASCII whitespace characters in a substring.
      Parameters:
      input - The input string.
      pos - The starting position (inclusive).
      limit - The ending position (exclusive).
      Returns:
      The index of the first non-whitespace character, or limit if all are whitespace.
    • skipTrailingAsciiWhitespace

      public static int skipTrailingAsciiWhitespace(String input, int pos, int limit)
      Skips trailing ASCII whitespace characters in a substring.
      Parameters:
      input - The input string.
      pos - The starting position (inclusive).
      limit - The ending position (exclusive).
      Returns:
      The index of the character after the last non-whitespace character, or pos if all are whitespace.
    • trimSubstring

      public static String trimSubstring(String string, int pos, int limit)
      Trims leading and trailing ASCII whitespace from a substring.
      Parameters:
      string - The input string.
      pos - The starting position (inclusive).
      limit - The ending position (exclusive).
      Returns:
      The trimmed substring.
    • delimiterOffset

      public static int delimiterOffset(String input, int pos, int limit, String delimiters)
      Finds the first occurrence of any delimiter character within a substring.
      Parameters:
      input - The input string.
      pos - The starting position (inclusive).
      limit - The ending position (exclusive).
      delimiters - A string containing all possible delimiter characters.
      Returns:
      The index of the first delimiter, or limit if no delimiter is found.
    • delimiterOffset

      public static int delimiterOffset(String input, int pos, int limit, char delimiter)
      Finds the first occurrence of a specific delimiter character within a substring.
      Parameters:
      input - The input string.
      pos - The starting position (inclusive).
      limit - The ending position (exclusive).
      delimiter - The single delimiter character to find.
      Returns:
      The index of the delimiter, or limit if no delimiter is found.
    • canonicalizeHost

      public static String canonicalizeHost(String host)
      Canonicalizes a hostname, converting it to its ASCII representation and validating it.
      Parameters:
      host - The hostname to canonicalize.
      Returns:
      The canonicalized hostname, or null if the host is invalid.
    • indexOfControlOrNonAscii

      public static int indexOfControlOrNonAscii(String input)
      Finds the index of the first control character or non-ASCII character in a string.
      Parameters:
      input - The input string.
      Returns:
      The index of the first invalid character, or -1 if all characters are valid ASCII.
    • verifyAsIpAddress

      public static boolean verifyAsIpAddress(String host)
      Verifies if the given host string is a valid IP address.
      Parameters:
      host - The host string to check.
      Returns:
      true if the host is an IP address, false otherwise.
    • bomAwareCharset

      public static Charset bomAwareCharset(org.miaixz.bus.core.io.source.BufferSource source, Charset charset) throws IOException
      Detects the character set of a BufferSource based on its Byte Order Mark (BOM), falling back to a default charset if no BOM is found.
      Parameters:
      source - The buffer source to check for BOM.
      charset - The default charset to use if no BOM is detected.
      Returns:
      The detected or default Charset.
      Throws:
      IOException - If an I/O error occurs while reading from the source.
    • checkDuration

      public static int checkDuration(String name, long duration, TimeUnit unit)
      Validates a duration value, ensuring it's non-negative and within integer limits when converted to milliseconds.
      Parameters:
      name - The name of the duration parameter (for error messages).
      duration - The duration value.
      unit - The TimeUnit of the duration.
      Returns:
      The duration in milliseconds as an integer.
      Throws:
      IllegalArgumentException - If the duration is negative, the unit is null, or the duration is too large/small.
    • decodeHexDigit

      public static int decodeHexDigit(char c)
      Decodes a hexadecimal character to its integer value.
      Parameters:
      c - The hexadecimal character.
      Returns:
      The integer value (0-15), or -1 if the character is not a valid hex digit.
    • toHeaders

      public static Headers toHeaders(List<Http2Header> headerBlock)
      Converts a list of HTTP/2 headers to a Headers object.
      Parameters:
      headerBlock - The list of Http2Header objects.
      Returns:
      A Headers instance built from the HTTP/2 headers.
    • toHeaderBlock

      public static List<Http2Header> toHeaderBlock(Headers headers)
      Converts a Headers object to a list of HTTP/2 headers.
      Parameters:
      headers - The Headers object to convert.
      Returns:
      A list of Http2Header objects.
    • sameConnection

      public static boolean sameConnection(UnoUrl a, UnoUrl b)
      Checks if two UnoUrl instances can share the same connection.
      Parameters:
      a - The first URL.
      b - The second URL.
      Returns:
      true if the URLs can share a connection, false otherwise.
    • parse

      public static Date parse(String value)
      Parses a date string into a Date object, trying various browser-compatible formats.
      Parameters:
      value - The date string to parse.
      Returns:
      A Date object if parsing is successful, null otherwise.
    • format

      public static String format(Date value)
      Formats a Date object into an RFC 1123 compliant string.
      Parameters:
      value - The Date object to format.
      Returns:
      The formatted date string.