public class Util
extends java.lang.Object
| 限定符和类型 | 字段和说明 |
|---|---|
static byte[] |
EMPTY_BYTE_ARRAY |
static java.lang.String[] |
EMPTY_STRING_ARRAY |
static java.util.TimeZone |
UTC
GMT and UTC are equivalent for our purposes.
|
static java.nio.charset.Charset |
UTF_8
A cheap and type-safe constant for the UTF-8 Charset.
|
| 限定符和类型 | 方法和说明 |
|---|---|
static void |
checkOffsetAndCount(long arrayLength,
long offset,
long count) |
static void |
closeAll(java.io.Closeable a,
java.io.Closeable b)
Closes
a and b. |
static void |
closeQuietly(java.io.Closeable closeable)
Closes
closeable, ignoring any checked exceptions. |
static void |
closeQuietly(java.net.ServerSocket serverSocket)
Closes
serverSocket, ignoring any checked exceptions. |
static void |
closeQuietly(java.net.Socket socket)
Closes
socket, ignoring any checked exceptions. |
static java.lang.String[] |
concat(java.lang.String[] array,
java.lang.String value) |
static boolean |
contains(java.lang.String[] array,
java.lang.String value) |
static int |
delimiterOffset(java.lang.String input,
int pos,
int limit,
char delimiter)
Returns the index of the first character in
input that is delimiter. |
static int |
delimiterOffset(java.lang.String input,
int pos,
int limit,
java.lang.String delimiters)
Returns the index of the first character in
input that contains a character in delimiters. |
static boolean |
discard(Source source,
int timeout,
java.util.concurrent.TimeUnit timeUnit)
Attempts to exhaust
source, returning true if successful. |
static java.lang.String |
domainToAscii(java.lang.String input)
Performs IDN ToASCII encoding and canonicalize the result to lowercase. e.g.
|
static boolean |
equal(java.lang.Object a,
java.lang.Object b)
Returns true if two possibly-null objects are equal.
|
static java.lang.String |
hostHeader(HttpUrl url,
boolean includeDefaultPort) |
static <T> java.util.List<T> |
immutableList(java.util.List<T> list)
Returns an immutable copy of
list. |
static <T> java.util.List<T> |
immutableList(T... elements)
Returns an immutable list containing
elements. |
static <K,V> java.util.Map<K,V> |
immutableMap(java.util.Map<K,V> map)
Returns an immutable copy of
map. |
static <T> T[] |
intersect(java.lang.Class<T> arrayType,
T[] first,
T[] second)
Returns an array containing containing only elements found in
first and also in second. |
static boolean |
isAndroidGetsocknameError(java.lang.AssertionError e)
Returns true if
e is due to a firmware bug fixed after Android 4.2.2. |
static java.lang.String |
md5Hex(java.lang.String s)
Returns a 32 character string containing an MD5 hash of
s. |
static ByteString |
sha1(ByteString s)
Returns a SHA-1 hash of
s. |
static ByteString |
sha256(ByteString s)
Returns a SHA-256 hash of
s. |
static java.lang.String |
shaBase64(java.lang.String s)
Returns a Base 64-encoded string containing a SHA-1 hash of
s. |
static boolean |
skipAll(Source source,
int duration,
java.util.concurrent.TimeUnit timeUnit)
Reads until
in is exhausted or the deadline has been reached. |
static int |
skipLeadingAsciiWhitespace(java.lang.String input,
int pos,
int limit)
Increments
pos until input[pos] is not ASCII whitespace. |
static int |
skipTrailingAsciiWhitespace(java.lang.String input,
int pos,
int limit)
Decrements
limit until input[limit - 1] is not ASCII whitespace. |
static java.util.concurrent.ThreadFactory |
threadFactory(java.lang.String name,
boolean daemon) |
static java.lang.String |
toHumanReadableAscii(java.lang.String s)
Returns
s with control characters and non-ASCII characters replaced with '?'. |
static java.lang.String |
trimSubstring(java.lang.String string,
int pos,
int limit)
Equivalent to
string.substring(pos, limit).trim(). |
static boolean |
verifyAsIpAddress(java.lang.String host)
Returns true if
host is not a host name and might be an IP address. |
public static final byte[] EMPTY_BYTE_ARRAY
public static final java.lang.String[] EMPTY_STRING_ARRAY
public static final java.nio.charset.Charset UTF_8
public static final java.util.TimeZone UTC
public static void checkOffsetAndCount(long arrayLength,
long offset,
long count)
public static boolean equal(java.lang.Object a,
java.lang.Object b)
public static void closeQuietly(java.io.Closeable closeable)
closeable, ignoring any checked exceptions. Does nothing if closeable is
null.public static void closeQuietly(java.net.Socket socket)
socket, ignoring any checked exceptions. Does nothing if socket is
null.public static void closeQuietly(java.net.ServerSocket serverSocket)
serverSocket, ignoring any checked exceptions. Does nothing if serverSocket is null.public static void closeAll(java.io.Closeable a,
java.io.Closeable b)
throws java.io.IOException
a and b. If either close fails, this completes the other close and
rethrows the first encountered exception.java.io.IOExceptionpublic static boolean discard(Source source, int timeout, java.util.concurrent.TimeUnit timeUnit)
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.public static boolean skipAll(Source source, int duration, java.util.concurrent.TimeUnit timeUnit) throws java.io.IOException
in is exhausted or the deadline has been reached. This is careful to not
extend the deadline if one exists already.java.io.IOExceptionpublic static java.lang.String md5Hex(java.lang.String s)
s.public static java.lang.String shaBase64(java.lang.String s)
s.public static ByteString sha1(ByteString s)
s.public static ByteString sha256(ByteString s)
s.public static <T> java.util.List<T> immutableList(java.util.List<T> list)
list.public static <T> java.util.List<T> immutableList(T... elements)
elements.public static <K,V> java.util.Map<K,V> immutableMap(java.util.Map<K,V> map)
map.public static java.util.concurrent.ThreadFactory threadFactory(java.lang.String name,
boolean daemon)
public static <T> T[] intersect(java.lang.Class<T> arrayType,
T[] first,
T[] second)
first and also in second. The returned elements are in the same order as in first.public static java.lang.String hostHeader(HttpUrl url, boolean includeDefaultPort)
public static java.lang.String toHumanReadableAscii(java.lang.String s)
s with control characters and non-ASCII characters replaced with '?'.public static boolean isAndroidGetsocknameError(java.lang.AssertionError e)
e is due to a firmware bug fixed after Android 4.2.2.
https://code.google.com/p/android/issues/detail?id=54072public static boolean contains(java.lang.String[] array,
java.lang.String value)
public static java.lang.String[] concat(java.lang.String[] array,
java.lang.String value)
public static int skipLeadingAsciiWhitespace(java.lang.String input,
int pos,
int limit)
pos until input[pos] is not ASCII whitespace. Stops at limit.public static int skipTrailingAsciiWhitespace(java.lang.String input,
int pos,
int limit)
limit until input[limit - 1] is not ASCII whitespace. Stops at
pos.public static java.lang.String trimSubstring(java.lang.String string,
int pos,
int limit)
string.substring(pos, limit).trim().public static int delimiterOffset(java.lang.String input,
int pos,
int limit,
java.lang.String delimiters)
input that contains a character in delimiters. Returns limit if there is no such character.public static int delimiterOffset(java.lang.String input,
int pos,
int limit,
char delimiter)
input that is delimiter. Returns
limit if there is no such character.public static java.lang.String domainToAscii(java.lang.String input)
☃.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.public static boolean verifyAsIpAddress(java.lang.String host)
host is not a host name and might be an IP address.