public final class IOUtils extends Object
| 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 String |
humanReadableByteCount(long bytes)
Returns a human-readable version of the file size, where the input represents
a specific number of bytes.
|
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 mostIOException - If an error occurs.public 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)IOException - if an IO exception occurred while writingpublic static String readString(InputStream in) throws IOException
in - the data input streamIOException - if an IO exception occurred while readingpublic static void writeBytes(OutputStream out, byte[] data) throws IOException
out - the data output streamdata - the byte arrayIOException - if an IO exception occurred while writing.public static byte[] readBytes(InputStream in) throws IOException
in - the data input streamIOException - if an IO exception occurred while reading from the stream.public static void writeVarInt(OutputStream out, int x) throws IOException
out - the output streamx - the valueIOException - if an IO exception occurred while writing.public static int readVarInt(InputStream in) throws IOException
in - the input streamIOException - if an IO exception occurred while reading.public static void writeVarLong(OutputStream out, long x) throws IOException
out - the output streamx - the valueIOException - if an IO exception occurred while writing.public static void writeLong(OutputStream out, long x) throws IOException
out - the output streamx - the valueIOException - if an IO exception occurred while writing.public static long readLong(InputStream in) throws IOException
in - the input streamIOException - if an IO exception occurred while reading.public static void writeInt(OutputStream out, int x) throws IOException
out - the output streamx - the valueIOException - if an IO exception occurred while writing.public static int readInt(InputStream in) throws IOException
in - the input streamIOException - if an IO exception occurred while reading.public static long readVarLong(InputStream in) throws IOException
in - the input streamIOException - if an IO exception occurred while reading.public 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 occurspublic static String humanReadableByteCount(long bytes)
Copyright © 2012-2014 The Apache Software Foundation. All Rights Reserved.