public class IOUtils extends Object
| Constructor and Description |
|---|
IOUtils() |
| Modifier and Type | Method and Description |
|---|---|
static void |
closeQuietly(Closeable closeable)
Unconditionally close a
Closeable. |
static void |
closeQuietly(Socket sock)
Unconditionally close a
Socket. |
static long |
copy(InputStream input,
OutputStream output)
Copy bytes from an
InputStream to an
OutputStream. |
static int |
nextPowerOf2(int x)
Get the value that is equal or higher than this value, and that is a
power of two.
|
static byte[] |
readBytes(InputStream in)
Read a byte array.
|
static int |
readFully(InputStream in,
byte[] buffer,
int off,
int max)
Try to read the given number of bytes to the buffer.
|
static int |
readInt(InputStream in)
Read an integer (4 bytes).
|
static long |
readLong(InputStream in)
Read a long (8 bytes).
|
static String |
readString(InputStream in)
Read a String.
|
static int |
readVarInt(InputStream in)
Read a variable size integer.
|
static long |
readVarLong(InputStream in)
Read a variable size long.
|
static void |
skipFully(InputStream in,
long skip)
Skip a number of bytes in an input stream.
|
static void |
writeBytes(OutputStream out,
byte[] data)
Write a byte array.
|
static void |
writeInt(OutputStream out,
int x)
Write an integer (4 bytes).
|
static void |
writeLong(OutputStream out,
long x)
Write a long (8 bytes).
|
static void |
writeString(OutputStream out,
String s)
Write a String.
|
static void |
writeVarInt(OutputStream out,
int x)
Write a variable size integer.
|
static void |
writeVarLong(OutputStream out,
long x)
Write a variable size long.
|
public static int readFully(InputStream in, byte[] buffer, int off, int max) throws IOException
in - the input streambuffer - the output bufferoff - the offset in the buffermax - the number of bytes to read at mostIOExceptionpublic static void skipFully(InputStream in, long skip) throws IOException
in - the input streamskip - the number of bytes to skipEOFException - if the end of file has been reached before all bytes
could be skippedIOException - if an IO exception occurred while skippingpublic static void writeString(OutputStream out, String s) throws IOException
out - the data output streams - the string (maximum length about 2 GB)IOExceptionpublic static String readString(InputStream in) throws IOException
in - the data input streamIOExceptionpublic static void writeBytes(OutputStream out, byte[] data) throws IOException
out - the data output streamdata - the byte arrayIOExceptionpublic static byte[] readBytes(InputStream in) throws IOException
in - the data input streamIOExceptionpublic static void writeVarInt(OutputStream out, int x) throws IOException
out - the output streamx - the valueIOExceptionpublic static int readVarInt(InputStream in) throws IOException
in - the input streamIOExceptionpublic static void writeVarLong(OutputStream out, long x) throws IOException
out - the output streamx - the valueIOExceptionpublic static void writeLong(OutputStream out, long x) throws IOException
out - the output streamx - the valueIOExceptionpublic static long readLong(InputStream in) throws IOException
in - the input streamIOExceptionpublic static void writeInt(OutputStream out, int x) throws IOException
out - the output streamx - the valueIOExceptionpublic static int readInt(InputStream in) throws IOException
in - the input streamIOExceptionpublic static long readVarLong(InputStream in) throws IOException
in - the input streamIOExceptionpublic static int nextPowerOf2(int x)
x - the original valuepublic static void closeQuietly(Closeable closeable)
Closeable.
Equivalent to Closeable.close(), except any exceptions will be ignored.
This is typically used in finally blocks.
closeable - the object to close, may be null or already closedpublic static void closeQuietly(Socket sock)
Socket.
Equivalent to Socket.close(), except any exceptions will be ignored.
This is typically used in finally blocks.
sock - the Socket to close, may be null or already closedpublic static long copy(InputStream input, OutputStream output) throws IOException
InputStream to an
OutputStream.
This method buffers the input internally, so there is no need to use a
BufferedInputStream.
input - the InputStream to read fromoutput - the OutputStream to write toIOException - if an I/O error occursCopyright © 2012-2013 The Apache Software Foundation. All Rights Reserved.