Class Util



  • public final class Util
    extends Object
    Junk drawer of utility methods.
    • Field Detail

      • EMPTY_BYTE_ARRAY

        public static final byte[] EMPTY_BYTE_ARRAY
      • EMPTY_STRING_ARRAY

        public static final String[] EMPTY_STRING_ARRAY
      • UTF_8

        public static final Charset UTF_8
        A cheap and type-safe constant for the UTF-8 Charset.
      • UTC

        public static final TimeZone UTC
        GMT and UTC are equivalent for our purposes.
    • Method Detail

      • checkOffsetAndCount

        public static void checkOffsetAndCount(long arrayLength,
                                               long offset,
                                               long count)
      • equal

        public static boolean equal(Object a,
                                    Object b)
        Returns true if two possibly-null objects are equal.
      • closeQuietly

        public static void closeQuietly(Closeable closeable)
        Closes closeable, ignoring any checked exceptions. Does nothing if closeable is null.
      • closeQuietly

        public static void closeQuietly(Socket socket)
        Closes socket, ignoring any checked exceptions. Does nothing if socket is null.
      • closeQuietly

        public static void closeQuietly(ServerSocket serverSocket)
        Closes serverSocket, ignoring any checked exceptions. Does nothing if serverSocket is null.
      • closeAll

        public static void closeAll(Closeable a,
                                    Closeable b)
                             throws IOException
        Closes a and b. If either close fails, this completes the other close and rethrows the first encountered exception.
        Throws:
        IOException
      • discard

        public static boolean discard(okio.Source source,
                                      int timeout,
                                      TimeUnit timeUnit)
        Attempts to exhaust source, returning true if successful. This is useful when reading a complete source is helpful, such as when doing so completes a cache body or frees a socket connection for reuse.
      • skipAll

        public static boolean skipAll(okio.Source source,
                                      int duration,
                                      TimeUnit timeUnit)
                               throws IOException
        Reads until in is exhausted or the deadline has been reached. This is careful to not extend the deadline if one exists already.
        Throws:
        IOException
      • md5Hex

        public static String md5Hex(String s)
        Returns a 32 character string containing an MD5 hash of s.
      • shaBase64

        public static String shaBase64(String s)
        Returns a Base 64-encoded string containing a SHA-1 hash of s.
      • sha1

        public static okio.ByteString sha1(okio.ByteString s)
        Returns a SHA-1 hash of s.
      • sha256

        public static okio.ByteString sha256(okio.ByteString s)
        Returns a SHA-256 hash of s.
      • immutableList

        public static <T> List<T> immutableList(List<T> list)
        Returns an immutable copy of list.
      • immutableList

        public static <T> List<T> immutableList(T... elements)
        Returns an immutable list containing elements.
      • immutableMap

        public static <K,V> Map<K,V> immutableMap(Map<K,V> map)
        Returns an immutable copy of map.
      • intersect

        public static <T> T[] intersect(Class<T> arrayType,
                                        T[] first,
                                        T[] second)
        Returns an array containing containing only elements found in first and also in second. The returned elements are in the same order as in first.
      • hostHeader

        public static String hostHeader(HttpUrl url,
                                        boolean includeDefaultPort)
      • toHumanReadableAscii

        public static String toHumanReadableAscii(String s)
        Returns s with control characters and non-ASCII characters replaced with '?'.
      • isAndroidGetsocknameError

        public static boolean isAndroidGetsocknameError(AssertionError e)
        Returns true if e is due to a firmware bug fixed after Android 4.2.2. https://code.google.com/p/android/issues/detail?id=54072
      • contains

        public static boolean contains(String[] array,
                                       String value)
      • skipLeadingAsciiWhitespace

        public static int skipLeadingAsciiWhitespace(String input,
                                                     int pos,
                                                     int limit)
        Increments pos until input[pos] is not ASCII whitespace. Stops at limit.
      • skipTrailingAsciiWhitespace

        public static int skipTrailingAsciiWhitespace(String input,
                                                      int pos,
                                                      int limit)
        Decrements limit until input[limit - 1] is not ASCII whitespace. Stops at pos.
      • trimSubstring

        public static String trimSubstring(String string,
                                           int pos,
                                           int limit)
        Equivalent to string.substring(pos, limit).trim().
      • delimiterOffset

        public static int delimiterOffset(String input,
                                          int pos,
                                          int limit,
                                          String delimiters)
        Returns the index of the first character in input that contains a character in delimiters. Returns limit if there is no such character.
      • delimiterOffset

        public static int delimiterOffset(String input,
                                          int pos,
                                          int limit,
                                          char delimiter)
        Returns the index of the first character in input that is delimiter. Returns limit if there is no such character.
      • domainToAscii

        public static String domainToAscii(String input)
        Performs IDN ToASCII encoding and canonicalize the result to lowercase. e.g. This converts ☃.net to xn--n3h.net, and WwW.GoOgLe.cOm to www.google.com. null will be returned if the input cannot be ToASCII encoded or if the result contains unsupported ASCII characters.
      • verifyAsIpAddress

        public static boolean verifyAsIpAddress(String host)
        Returns true if host is not a host name and might be an IP address.