java.lang.Object
org.miaixz.bus.http.Builder
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 Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe STOMP ACK frame command.static final String[]Array of browser-compatible date format strings.static final DateFormat[]Array of browser-compatible date formatters.static final StringThe HTTP CONNECT method.static final StringThe STOMP CONNECTED frame command.static final HeadersAn emptyHeadersinstance.static final ResponseBodyAn emptyResponseBodyinstance.static final StringThe STOMP ERROR frame command.static final longMaximum date value (December 31, 9999).static final StringThe STOMP MESSAGE frame command.static final Comparator<String> A comparator that orders strings naturally.static final org.miaixz.bus.core.io.ByteStringByte string containing characters that delimit quoted strings.static final StringThe STOMP SEND frame command.static final StringThe STOMP SUBSCRIBE frame command.static final org.miaixz.bus.core.io.ByteStringByte string containing characters that delimit tokens.static final StringThe STOMP UNKNOWN frame command.static final StringThe STOMP UNSUBSCRIBE frame command.static final TimeZoneThe UTC (Coordinated Universal Time) timezone. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidaddSuppressedIfPossible(Throwable e, Throwable suppressed) Adds a suppressed exception to the given throwable if possible.static CharsetbomAwareCharset(org.miaixz.bus.core.io.source.BufferSource source, Charset charset) Detects the character set of aBufferSourcebased on its Byte Order Mark (BOM), falling back to a default charset if no BOM is found.static StringcanonicalizeHost(String host) Canonicalizes a hostname, converting it to its ASCII representation and validating it.static intcheckDuration(String name, long duration, TimeUnit unit) Validates a duration value, ensuring it's non-negative and within integer limits when converted to milliseconds.static voidcheckOffsetAndCount(long arrayLength, long offset, long count) Checks if the given offset and count are within the bounds of an array of a specified length.static String[]Concatenates a string to an existing string array, returning a new array.static intdecodeHexDigit(char c) Decodes a hexadecimal character to its integer value.static intdelimiterOffset(String input, int pos, int limit, char delimiter) Finds the first occurrence of a specific delimiter character within a substring.static intdelimiterOffset(String input, int pos, int limit, String delimiters) Finds the first occurrence of any delimiter character within a substring.static booleanAttempts to exhaust the given source within a specified timeout.static StringFormats aDateobject into an RFC 1123 compliant string.static StringhostHeader(UnoUrl url, boolean includeDefaultPort) Generates a host header string for a given URL.static <T> List<T> immutableList(List<T> list) Returns an immutable copy of the given list.static <T> List<T> immutableList(T... elements) Returns an immutable list containing the given elements.static <K,V> Map <K, V> immutableMap(Map<K, V> map) Returns an immutable copy of the given map.static intindexOf(Comparator<String> comparator, String[] array, String value) Finds the index of a string in an array using a custom comparator.static intindexOfControlOrNonAscii(String input) Finds the index of the first control character or non-ASCII character in a string.static String[]intersect(Comparator<? super String> comparator, String[] first, String[] second) Returns the intersection of two string arrays based on a given comparator.static booleannonEmptyIntersection(Comparator<String> comparator, String[] first, String[] second) Checks if there is any common string between two arrays based on a given comparator.static DateParses a date string into aDateobject, trying various browser-compatible formats.static booleansameConnection(UnoUrl a, UnoUrl b) Checks if twoUnoUrlinstances can share the same connection.static booleanSkips all bytes from the source until it is exhausted or the timeout is reached.static intskipLeadingAsciiWhitespace(String input, int pos, int limit) Skips leading ASCII whitespace characters in a substring.static intskipTrailingAsciiWhitespace(String input, int pos, int limit) Skips trailing ASCII whitespace characters in a substring.static ThreadFactorythreadFactory(String name, boolean daemon) Creates aThreadFactorythat produces threads with a given name and daemon status.static List<Http2Header> toHeaderBlock(Headers headers) Converts aHeadersobject to a list of HTTP/2 headers.static HeaderstoHeaders(List<Http2Header> headerBlock) Converts a list of HTTP/2 headers to aHeadersobject.static StringtrimSubstring(String string, int pos, int limit) Trims leading and trailing ASCII whitespace from a substring.static booleanverifyAsIpAddress(String host) Verifies if the given host string is a valid IP address.
-
Field Details
-
MAX_DATE
public static final long MAX_DATEMaximum date value (December 31, 9999).- See Also:
-
EMPTY_HEADERS
An emptyHeadersinstance. -
EMPTY_RESPONSE
An emptyResponseBodyinstance. -
UTC
The UTC (Coordinated Universal Time) timezone. -
NATURAL_ORDER
A comparator that orders strings naturally. -
QUOTED_STRING_DELIMITERS
public static final org.miaixz.bus.core.io.ByteString QUOTED_STRING_DELIMITERSByte string containing characters that delimit quoted strings. -
TOKEN_DELIMITERS
public static final org.miaixz.bus.core.io.ByteString TOKEN_DELIMITERSByte string containing characters that delimit tokens. -
BROWSER_COMPATIBLE_DATE_FORMAT_STRINGS
Array of browser-compatible date format strings. -
BROWSER_COMPATIBLE_DATE_FORMATS
Array of browser-compatible date formatters. -
CONNECT
The HTTP CONNECT method.- See Also:
-
CONNECTED
The STOMP CONNECTED frame command.- See Also:
-
SEND
The STOMP SEND frame command.- See Also:
-
MESSAGE
The STOMP MESSAGE frame command.- See Also:
-
SUBSCRIBE
The STOMP SUBSCRIBE frame command.- See Also:
-
UNSUBSCRIBE
The STOMP UNSUBSCRIBE frame command.- See Also:
-
ACK
The STOMP ACK frame command.- See Also:
-
UNKNOWN
The STOMP UNKNOWN frame command.- See Also:
-
ERROR
The STOMP ERROR frame command.- See Also:
-
-
Constructor Details
-
Builder
public Builder()Constructs a newBuilderinstance.
-
-
Method Details
-
addSuppressedIfPossible
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:
trueif the source was successfully discarded,falseotherwise.
-
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:
trueif all bytes were skipped,falseif the timeout was reached.- Throws:
IOException- If an I/O error occurs during skipping.
-
immutableList
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
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
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
Creates aThreadFactorythat produces threads with a given name and daemon status.- Parameters:
name- The name prefix for the created threads.daemon-trueif the created threads should be daemon threads,falseotherwise.- Returns:
- A
ThreadFactoryinstance.
-
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:
trueif an intersection exists,falseotherwise.
-
hostHeader
Generates a host header string for a given URL.- Parameters:
url- The URL for which to generate the host header.includeDefaultPort-trueto include the port even if it's the default for the scheme,falseotherwise.- Returns:
- The host header string.
-
indexOf
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
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
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
limitif all are whitespace.
-
skipTrailingAsciiWhitespace
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
posif all are whitespace.
-
trimSubstring
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
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
limitif no delimiter is found.
-
delimiterOffset
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
limitif no delimiter is found.
-
canonicalizeHost
Canonicalizes a hostname, converting it to its ASCII representation and validating it.- Parameters:
host- The hostname to canonicalize.- Returns:
- The canonicalized hostname, or
nullif the host is invalid.
-
indexOfControlOrNonAscii
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
Verifies if the given host string is a valid IP address.- Parameters:
host- The host string to check.- Returns:
trueif the host is an IP address,falseotherwise.
-
bomAwareCharset
public static Charset bomAwareCharset(org.miaixz.bus.core.io.source.BufferSource source, Charset charset) throws IOException Detects the character set of aBufferSourcebased 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
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- TheTimeUnitof 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
Converts a list of HTTP/2 headers to aHeadersobject.- Parameters:
headerBlock- The list ofHttp2Headerobjects.- Returns:
- A
Headersinstance built from the HTTP/2 headers.
-
toHeaderBlock
Converts aHeadersobject to a list of HTTP/2 headers.- Parameters:
headers- TheHeadersobject to convert.- Returns:
- A list of
Http2Headerobjects.
-
sameConnection
Checks if twoUnoUrlinstances can share the same connection.- Parameters:
a- The first URL.b- The second URL.- Returns:
trueif the URLs can share a connection,falseotherwise.
-
parse
Parses a date string into aDateobject, trying various browser-compatible formats.- Parameters:
value- The date string to parse.- Returns:
- A
Dateobject if parsing is successful,nullotherwise.
-
format
Formats aDateobject into an RFC 1123 compliant string.- Parameters:
value- TheDateobject to format.- Returns:
- The formatted date string.
-