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 CharsetISO_8859_1A cheap and type-safe constant for the ISO-8859-1 Charset.static CharsetUS_ASCIIA cheap and type-safe constant for the US-ASCII Charset.static CharsetUTF_8A cheap and type-safe constant for the UTF-8 Charset. -
Method Summary
Modifier and Type Method Description static voidcheckOffsetAndCount(int arrayLength, int offset, int count)static voidcloseAll(Closeable a, Closeable b)Closesaandb.static voidcloseQuietly(Closeable closeable)Closescloseable, ignoring any checked exceptions.static voidcloseQuietly(Socket socket)Closessocket, ignoring any checked exceptions.static intcopy(InputStream in, OutputStream out)Copies all of the bytes fromintoout.static ThreadFactorydaemonThreadFactory(String name)static voiddeleteContents(File dir)Deletes the contents ofdir.static booleanequal(Object a, Object b)Returns true if two possibly-null objects are equal.static intgetDefaultPort(String scheme)static intgetEffectivePort(URI uri)static intgetEffectivePort(URL url)static <T> List<T>immutableList(List<T> list)Returns an immutable copy oflist.static voidpokeInt(byte[] dst, int offset, int value, ByteOrder order)static StringreadAsciiLine(InputStream in)Returns the ASCII characters up to but not including the next "\r\n", or "\n".static voidreadFully(InputStream in, byte[] dst)Fills 'dst' with bytes from 'in', throwing EOFException if insufficient bytes are available.static voidreadFully(InputStream in, byte[] dst, int offset, int byteCount)Reads exactly 'byteCount' bytes from 'in' (into 'dst' at offset 'offset'), and throws EOFException if insufficient bytes are available.static StringreadFully(Reader reader)Returns the remainder of 'reader' as a string, closing it when done.static intreadSingleByte(InputStream in)Implements InputStream.read(int) in terms of InputStream.read(byte[], int, int).static voidskipAll(InputStream in)static longskipByReading(InputStream in, long byteCount)Callin.read()repeatedly until either the stream is exhausted orbyteCountbytes have been read.static voidwriteSingleByte(OutputStream out, int b)Implements OutputStream.write(int) in terms of OutputStream.write(byte[], int, int).
-
Field Details
-
EMPTY_BYTE_ARRAY
public static final byte[] EMPTY_BYTE_ARRAY -
EMPTY_STRING_ARRAY
-
ISO_8859_1
A cheap and type-safe constant for the ISO-8859-1 Charset. -
US_ASCII
A cheap and type-safe constant for the US-ASCII Charset. -
UTF_8
A cheap and type-safe constant for the UTF-8 Charset.
-
-
Method Details
-
getEffectivePort
-
getEffectivePort
-
getDefaultPort
-
checkOffsetAndCount
public static void checkOffsetAndCount(int arrayLength, int offset, int count) -
pokeInt
-
equal
Returns true if two possibly-null objects are equal. -
closeQuietly
Closescloseable, ignoring any checked exceptions. Does nothing ifcloseableis null. -
closeQuietly
Closessocket, ignoring any checked exceptions. Does nothing ifsocketis null. -
closeAll
Closesaandb. If either close fails, this completes the other close and rethrows the first encountered exception.- Throws:
IOException
-
deleteContents
Deletes the contents ofdir. Throws an IOException if any file could not be deleted, or ifdiris not a readable directory.- Throws:
IOException
-
readSingleByte
Implements InputStream.read(int) in terms of InputStream.read(byte[], int, int). InputStream assumes that you implement InputStream.read(int) and provides default implementations of the others, but often the opposite is more efficient.- Throws:
IOException
-
writeSingleByte
Implements OutputStream.write(int) in terms of OutputStream.write(byte[], int, int). OutputStream assumes that you implement OutputStream.write(int) and provides default implementations of the others, but often the opposite is more efficient.- Throws:
IOException
-
readFully
Fills 'dst' with bytes from 'in', throwing EOFException if insufficient bytes are available.- Throws:
IOException
-
readFully
public static void readFully(InputStream in, byte[] dst, int offset, int byteCount) throws IOExceptionReads exactly 'byteCount' bytes from 'in' (into 'dst' at offset 'offset'), and throws EOFException if insufficient bytes are available. Used to implementDataInputStream.readFully(byte[], int, int).- Throws:
IOException
-
readFully
Returns the remainder of 'reader' as a string, closing it when done.- Throws:
IOException
-
skipAll
- Throws:
IOException
-
skipByReading
Callin.read()repeatedly until either the stream is exhausted orbyteCountbytes have been read.This method reuses the skip buffer but is careful to never use it at the same time that another stream is using it. Otherwise streams that use the caller's buffer for consistency checks like CRC could be clobbered by other threads. A thread-local buffer is also insufficient because some streams may call other streams in their skip() method, also clobbering the buffer.
- Throws:
IOException
-
copy
Copies all of the bytes fromintoout. Neither stream is closed. Returns the total number of bytes transferred.- Throws:
IOException
-
readAsciiLine
Returns the ASCII characters up to but not including the next "\r\n", or "\n".- Throws:
EOFException- if the stream is exhausted before the next newline character.IOException
-
immutableList
Returns an immutable copy oflist. -
daemonThreadFactory
-