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.
    • 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​(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​(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.
      • 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.
      • 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