-
public final class UtilJunk drawer of utility methods.
-
-
Field Summary
Fields Modifier and Type Field Description public final static Array<byte>EMPTY_BYTE_ARRAYpublic final static Array<String>EMPTY_STRING_ARRAYpublic final static ResponseBodyEMPTY_RESPONSEpublic final static RequestBodyEMPTY_REQUESTpublic final static CharsetUTF_8public final static CharsetISO_8859_1public final static TimeZoneUTCpublic final static Comparator<String>NATURAL_ORDER
-
Method Summary
Modifier and Type Method Description static voidcheckOffsetAndCount(long arrayLength, long offset, long count)static booleanequal(Object a, Object b)Returns true if two possibly-null objects are equal. static voidcloseQuietly(Closeable closeable)Closes {@code closeable}, ignoring any checked exceptions.static voidcloseQuietly(Socket socket)Closes {@code socket}, ignoring any checked exceptions.static voidcloseQuietly(ServerSocket serverSocket)Closes {@code serverSocket}, ignoring any checked exceptions.static booleandiscard(Source source, int timeout, TimeUnit timeUnit)Attempts to exhaust {@code source}, returning true if successful.static booleanskipAll(Source source, int duration, TimeUnit timeUnit)Reads until {@code in}is exhausted or the deadline has been reached.static <T> List<T>immutableList(List<T> list)Returns an immutable copy of {@code list}.static <T> List<T>immutableList(Array<T> elements)Returns an immutable list containing {@code elements}.static ThreadFactorythreadFactory(String name, boolean daemon)static Array<String>intersect(Comparator<out Object> comparator, Array<String> first, Array<String> second)Returns an array containing only elements found in {@code first}and also in{@code * second}.static booleannonEmptyIntersection(Comparator<String> comparator, Array<String> first, Array<String> second)Returns true if there is an element in {@code first}that is also in{@code second}.static StringhostHeader(HttpUrl url, boolean includeDefaultPort)static booleanisAndroidGetsocknameError(AssertionError e)Returns true if {@code e}is due to a firmware bug fixed after Android 4.2.2.https://code.google.com/p/android/issues/detail?static intindexOf(Comparator<String> comparator, Array<String> array, String value)static Array<String>concat(Array<String> array, String value)static intskipLeadingAsciiWhitespace(String input, int pos, int limit)Increments {@code pos}until{@code input[pos]}is not ASCII whitespace.static intskipTrailingAsciiWhitespace(String input, int pos, int limit)Decrements {@code limit}until{@code input[limit - 1]}is not ASCII whitespace.static StringtrimSubstring(String string, int pos, int limit)Equivalent to {@code string.substring(pos, limit).trim()}.static intdelimiterOffset(String input, int pos, int limit, String delimiters)Returns the index of the first character in {@code input}that contains a character in{@code * delimiters}.static intdelimiterOffset(String input, int pos, int limit, char delimiter)Returns the index of the first character in {@code input}that is{@code delimiter}.static StringcanonicalizeHost(String host)If {@code host}is an IP address, this returns the IP address in canonical form.static intindexOfControlOrNonAscii(String input)Returns the index of the first character in {@code input}that is either a control character(like{@code \u0000 or \n}) or a non-ASCII character.static booleanverifyAsIpAddress(String host)Returns true if {@code host}is not a host name and might be an IP address.static Stringformat(String format, Array<Object> args)Returns a US formatted String. static CharsetbomAwareCharset(BufferedSource source, Charset charset)static intcheckDuration(String name, long duration, TimeUnit unit)static AssertionErrorassertionError(String message, Exception e)static intdecodeHexDigit(char c)-
-
Method Detail
-
checkOffsetAndCount
static void checkOffsetAndCount(long arrayLength, long offset, long count)
-
closeQuietly
static void closeQuietly(Closeable closeable)
Closes
{@code closeable}, ignoring any checked exceptions. Does nothing if{@code closeable}isnull.
-
closeQuietly
static void closeQuietly(Socket socket)
Closes
{@code socket}, ignoring any checked exceptions. Does nothing if{@code socket}isnull.
-
closeQuietly
static void closeQuietly(ServerSocket serverSocket)
Closes
{@code serverSocket}, ignoring any checked exceptions. Does nothing if{@code * serverSocket}is null.
-
discard
static boolean discard(Source source, int timeout, TimeUnit timeUnit)
Attempts to exhaust
{@code source}, returning true if successful. This is useful when reading acomplete source is helpful, such as when doing so completes a cache body or frees a socketconnection for reuse.
-
skipAll
static boolean skipAll(Source source, int duration, TimeUnit timeUnit)
Reads until
{@code in}is exhausted or the deadline has been reached. This is careful to notextend the deadline if one exists already.
-
immutableList
static <T> List<T> immutableList(List<T> list)
Returns an immutable copy of
{@code list}.
-
immutableList
static <T> List<T> immutableList(Array<T> elements)
Returns an immutable list containing
{@code elements}.
-
threadFactory
static ThreadFactory threadFactory(String name, boolean daemon)
-
intersect
static Array<String> intersect(Comparator<out Object> comparator, Array<String> first, Array<String> second)
Returns an array containing only elements found in
{@code first}and also in{@code * second}. The returned elements are in the same order as in{@code first}.
-
nonEmptyIntersection
static boolean nonEmptyIntersection(Comparator<String> comparator, Array<String> first, Array<String> second)
Returns true if there is an element in
{@code first}that is also in{@code second}. Thismethod terminates if any intersection is found. The sizes of both arguments are assumed to beso small, and the likelihood of an intersection so great, that it is not worth the CPU cost ofsorting or the memory cost of hashing.
-
hostHeader
static String hostHeader(HttpUrl url, boolean includeDefaultPort)
-
isAndroidGetsocknameError
static boolean isAndroidGetsocknameError(AssertionError e)
Returns true if
{@code e}is due to a firmware bug fixed after Android 4.2.2.https://code.google.com/p/android/issues/detail?id=54072
-
skipLeadingAsciiWhitespace
static int skipLeadingAsciiWhitespace(String input, int pos, int limit)
Increments
{@code pos}until{@code input[pos]}is not ASCII whitespace. Stops at{@code * limit}.
-
skipTrailingAsciiWhitespace
static int skipTrailingAsciiWhitespace(String input, int pos, int limit)
Decrements
{@code limit}until{@code input[limit - 1]}is not ASCII whitespace. Stops at{@code pos}.
-
trimSubstring
static String trimSubstring(String string, int pos, int limit)
Equivalent to
{@code string.substring(pos, limit).trim()}.
-
delimiterOffset
static int delimiterOffset(String input, int pos, int limit, String delimiters)
Returns the index of the first character in
{@code input}that contains a character in{@code * delimiters}. Returns limit if there is no such character.
-
delimiterOffset
static int delimiterOffset(String input, int pos, int limit, char delimiter)
Returns the index of the first character in
{@code input}that is{@code delimiter}. Returnslimit if there is no such character.
-
canonicalizeHost
static String canonicalizeHost(String host)
If
{@code host}is an IP address, this returns the IP address in canonical form.Otherwise this performs IDN ToASCII encoding and canonicalize the result to lowercase. Forexample this converts
{@code ☃.net}to{@code xn--n3h.net}, and{@code WwW.GoOgLe.cOm}to{@code www.google.com}.{@code null}will be returned if the host cannot be ToASCII encoded orif the result contains unsupported ASCII characters.
-
indexOfControlOrNonAscii
static int indexOfControlOrNonAscii(String input)
Returns the index of the first character in
{@code input}that is either a control character(like{@code \u0000 or \n}) or a non-ASCII character. Returns -1 if{@code input}has no suchcharacters.
-
verifyAsIpAddress
static boolean verifyAsIpAddress(String host)
Returns true if
{@code host}is not a host name and might be an IP address.
-
bomAwareCharset
static Charset bomAwareCharset(BufferedSource source, Charset charset)
-
checkDuration
static int checkDuration(String name, long duration, TimeUnit unit)
-
assertionError
static AssertionError assertionError(String message, Exception e)
-
decodeHexDigit
static int decodeHexDigit(char c)
-
-
-
-