Package com.squareup.okhttp.internal
Class Util
- java.lang.Object
-
- com.squareup.okhttp.internal.Util
-
public final class Util extends Object
Junk drawer of utility methods.
-
-
Field Summary
Fields Modifier and Type Field Description static byte[]EMPTY_BYTE_ARRAYstatic String[]EMPTY_STRING_ARRAYstatic CharsetUTF_8A cheap and type-safe constant for the UTF-8 Charset.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidcheckOffsetAndCount(long arrayLength, long offset, long count)static voidcloseAll(Closeable a, Closeable b)Closesaandb.static voidcloseQuietly(Closeable closeable)Closescloseable, ignoring any checked exceptions.static voidcloseQuietly(ServerSocket serverSocket)ClosesserverSocket, ignoring any checked exceptions.static voidcloseQuietly(Socket socket)Closessocket, ignoring any checked exceptions.static booleandiscard(Source source, int timeout, TimeUnit timeUnit)Attempts to exhaustsource, returning true if successful.static booleanequal(Object a, Object b)Returns true if two possibly-null objects are equal.static StringhostHeader(HttpUrl url)static <T> List<T>immutableList(List<T> list)Returns an immutable copy oflist.static <T> List<T>immutableList(T... elements)Returns an immutable list containingelements.static <K,V>
Map<K,V>immutableMap(Map<K,V> map)Returns an immutable copy ofmap.static <T> T[]intersect(Class<T> arrayType, T[] first, T[] second)Returns an array containing containing only elements found infirstand also insecond.static booleanisAndroidGetsocknameError(AssertionError e)Returns true ifeis due to a firmware bug fixed after Android 4.2.2.static Stringmd5Hex(String s)Returns a 32 character string containing an MD5 hash ofs.static ByteStringsha1(ByteString s)Returns a SHA-1 hash ofs.static StringshaBase64(String s)Returns a Base 64-encoded string containing a SHA-1 hash ofs.static booleanskipAll(Source source, int duration, TimeUnit timeUnit)Reads untilinis exhausted or the deadline has been reached.static ThreadFactorythreadFactory(String name, boolean daemon)static StringtoHumanReadableAscii(String s)Returnsswith control characters and non-ASCII characters replaced with '?'.
-
-
-
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)
Closescloseable, ignoring any checked exceptions. Does nothing ifcloseableis null.
-
closeQuietly
public static void closeQuietly(Socket socket)
Closessocket, ignoring any checked exceptions. Does nothing ifsocketis null.
-
closeQuietly
public static void closeQuietly(ServerSocket serverSocket)
ClosesserverSocket, ignoring any checked exceptions. Does nothing ifserverSocketis null.
-
closeAll
public static void closeAll(Closeable a, Closeable b) throws IOException
Closesaandb. 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 exhaustsource, 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 untilinis 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 ofs.
-
shaBase64
public static String shaBase64(String s)
Returns a Base 64-encoded string containing a SHA-1 hash ofs.
-
sha1
public static ByteString sha1(ByteString s)
Returns a SHA-1 hash ofs.
-
immutableList
public static <T> List<T> immutableList(List<T> list)
Returns an immutable copy oflist.
-
immutableList
public static <T> List<T> immutableList(T... elements)
Returns an immutable list containingelements.
-
immutableMap
public static <K,V> Map<K,V> immutableMap(Map<K,V> map)
Returns an immutable copy ofmap.
-
threadFactory
public static ThreadFactory threadFactory(String name, boolean daemon)
-
intersect
public static <T> T[] intersect(Class<T> arrayType, T[] first, T[] second)
Returns an array containing containing only elements found infirstand also insecond. The returned elements are in the same order as infirst.
-
toHumanReadableAscii
public static String toHumanReadableAscii(String s)
Returnsswith control characters and non-ASCII characters replaced with '?'.
-
isAndroidGetsocknameError
public static boolean isAndroidGetsocknameError(AssertionError e)
Returns true ifeis due to a firmware bug fixed after Android 4.2.2. https://code.google.com/p/android/issues/detail?id=54072
-
-