public final class Util extends Object
Modifier and Type | Field and Description |
---|---|
static byte[] |
EMPTY_BYTE_ARRAY |
static String[] |
EMPTY_STRING_ARRAY |
static Charset |
ISO_8859_1
A cheap and type-safe constant for the ISO-8859-1 Charset.
|
static Charset |
US_ASCII
A cheap and type-safe constant for the US-ASCII Charset.
|
static Charset |
UTF_8
A cheap and type-safe constant for the UTF-8 Charset.
|
Modifier and Type | Method and Description |
---|---|
static void |
checkOffsetAndCount(int arrayLength,
int offset,
int count) |
static void |
closeAll(Closeable a,
Closeable b)
Closes
a and b . |
static void |
closeQuietly(Closeable closeable)
Closes
closeable , ignoring any checked exceptions. |
static void |
closeQuietly(Socket socket)
Closes
socket , ignoring any checked exceptions. |
static int |
copy(InputStream in,
OutputStream out)
Copies all of the bytes from
in to out . |
static ThreadFactory |
daemonThreadFactory(String name) |
static void |
deleteContents(File dir)
Deletes the contents of
dir . |
static boolean |
equal(Object a,
Object b)
Returns true if two possibly-null objects are equal.
|
static int |
getDefaultPort(String scheme) |
static int |
getEffectivePort(URI uri) |
static int |
getEffectivePort(URL url) |
static <T> List<T> |
immutableList(List<T> list)
Returns an immutable copy of
list . |
static void |
pokeInt(byte[] dst,
int offset,
int value,
ByteOrder order) |
static String |
readAsciiLine(InputStream in)
Returns the ASCII characters up to but not including the next "\r\n", or
"\n".
|
static void |
readFully(InputStream in,
byte[] dst)
Fills 'dst' with bytes from 'in', throwing EOFException if insufficient bytes are available.
|
static void |
readFully(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 String |
readFully(Reader reader)
Returns the remainder of 'reader' as a string, closing it when done.
|
static int |
readSingleByte(InputStream in)
Implements InputStream.read(int) in terms of InputStream.read(byte[], int, int).
|
static void |
skipAll(InputStream in) |
static long |
skipByReading(InputStream in,
long byteCount)
Call
in.read() repeatedly until either the stream is exhausted or
byteCount bytes have been read. |
static void |
writeSingleByte(OutputStream out,
int b)
Implements OutputStream.write(int) in terms of OutputStream.write(byte[], int, int).
|
public static final byte[] EMPTY_BYTE_ARRAY
public static final String[] EMPTY_STRING_ARRAY
public static final Charset ISO_8859_1
public static final Charset US_ASCII
public static final Charset UTF_8
public static int getEffectivePort(URI uri)
public static int getEffectivePort(URL url)
public static int getDefaultPort(String scheme)
public static void checkOffsetAndCount(int arrayLength, int offset, int count)
public static void pokeInt(byte[] dst, int offset, int value, ByteOrder order)
public static boolean equal(Object a, Object b)
public static void closeQuietly(Closeable closeable)
closeable
, ignoring any checked exceptions. Does nothing
if closeable
is null.public static void closeQuietly(Socket socket)
socket
, ignoring any checked exceptions. Does nothing if
socket
is null.public static void closeAll(Closeable a, Closeable b) throws IOException
a
and b
. If either close fails, this completes
the other close and rethrows the first encountered exception.IOException
public static void deleteContents(File dir) throws IOException
dir
. Throws an IOException if any file
could not be deleted, or if dir
is not a readable directory.IOException
public static int readSingleByte(InputStream in) throws IOException
IOException
public static void writeSingleByte(OutputStream out, int b) throws IOException
IOException
public static void readFully(InputStream in, byte[] dst) throws IOException
IOException
public static void readFully(InputStream in, byte[] dst, int offset, int byteCount) throws IOException
DataInputStream.readFully(byte[], int, int)
.IOException
public static String readFully(Reader reader) throws IOException
IOException
public static void skipAll(InputStream in) throws IOException
IOException
public static long skipByReading(InputStream in, long byteCount) throws IOException
in.read()
repeatedly until either the stream is exhausted or
byteCount
bytes 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.
IOException
public static int copy(InputStream in, OutputStream out) throws IOException
in
to out
. Neither stream is closed.
Returns the total number of bytes transferred.IOException
public static String readAsciiLine(InputStream in) throws IOException
EOFException
- if the stream is exhausted before the next newline
character.IOException
public static <T> List<T> immutableList(List<T> list)
list
.public static ThreadFactory daemonThreadFactory(String name)
Copyright © 2014. All rights reserved.