public final class IOUtils
extends java.lang.Object
Modifier and Type | Method and Description |
---|---|
static void |
closeQuietly(java.io.Closeable closeable)
Unconditionally close a
Closeable . |
static void |
closeQuietly(java.net.Socket sock)
Unconditionally close a
Socket . |
static long |
copy(java.io.InputStream input,
java.io.OutputStream output)
Copy bytes from an
InputStream to an
OutputStream . |
static java.lang.String |
humanReadableByteCount(long bytes)
Returns a human-readable version of the file size, where the input represents
a specific number of bytes.
|
static long |
nextPowerOf2(int x)
Get the value that is equal or higher than this value, and that is a
power of two.
|
static byte[] |
readBytes(java.io.InputStream in)
Read a byte array.
|
static int |
readFully(java.nio.channels.FileChannel channel,
int position,
java.nio.ByteBuffer buffer)
Try to read the given number of bytes starting at the specified position
into the buffer.
|
static int |
readFully(java.io.InputStream in,
byte[] buffer,
int off,
int max)
Try to read the given number of bytes to the buffer.
|
static int |
readInt(java.io.InputStream in)
Read an integer (4 bytes).
|
static long |
readLong(java.io.InputStream in)
Read a long (8 bytes).
|
static java.lang.String |
readString(java.io.InputStream in)
Read a String.
|
static int |
readVarInt(java.io.InputStream in)
Read a variable size integer.
|
static long |
readVarLong(java.io.InputStream in)
Read a variable size long.
|
static void |
skipFully(java.io.InputStream in,
long skip)
Skip a number of bytes in an input stream.
|
static void |
writeBytes(java.io.OutputStream out,
byte[] data)
Write a byte array.
|
static void |
writeInt(java.io.OutputStream out,
int x)
Write an integer (4 bytes).
|
static void |
writeLong(java.io.OutputStream out,
long x)
Write a long (8 bytes).
|
static void |
writeString(java.io.OutputStream out,
java.lang.String s)
Write a String.
|
static void |
writeVarInt(java.io.OutputStream out,
int x)
Write a variable size integer.
|
static void |
writeVarLong(java.io.OutputStream out,
long x)
Write a variable size long.
|
public static int readFully(java.io.InputStream in, byte[] buffer, int off, int max) throws java.io.IOException
in
- the input streambuffer
- the output bufferoff
- the offset in the buffermax
- the number of bytes to read at mostjava.io.IOException
- If an error occurs.public static int readFully(java.nio.channels.FileChannel channel, int position, java.nio.ByteBuffer buffer) throws java.io.IOException
channel
- the input channelposition
- the position to start reading from the channelbuffer
- the output bufferjava.io.IOException
- If an error occurs.public static void skipFully(java.io.InputStream in, long skip) throws java.io.IOException
in
- the input streamskip
- the number of bytes to skipjava.io.EOFException
- if the end of file has been reached before all bytes
could be skippedjava.io.IOException
- if an IO exception occurred while skippingpublic static void writeString(java.io.OutputStream out, java.lang.String s) throws java.io.IOException
out
- the data output streams
- the string (maximum length about 2 GB)java.io.IOException
- if an IO exception occurred while writingpublic static java.lang.String readString(java.io.InputStream in) throws java.io.IOException
in
- the data input streamjava.io.IOException
- if an IO exception occurred while readingpublic static void writeBytes(java.io.OutputStream out, byte[] data) throws java.io.IOException
out
- the data output streamdata
- the byte arrayjava.io.IOException
- if an IO exception occurred while writing.public static byte[] readBytes(java.io.InputStream in) throws java.io.IOException
in
- the data input streamjava.io.IOException
- if an IO exception occurred while reading from the stream.public static void writeVarInt(java.io.OutputStream out, int x) throws java.io.IOException
out
- the output streamx
- the valuejava.io.IOException
- if an IO exception occurred while writing.public static int readVarInt(java.io.InputStream in) throws java.io.IOException
in
- the input streamjava.io.IOException
- if an IO exception occurred while reading.public static void writeVarLong(java.io.OutputStream out, long x) throws java.io.IOException
out
- the output streamx
- the valuejava.io.IOException
- if an IO exception occurred while writing.public static void writeLong(java.io.OutputStream out, long x) throws java.io.IOException
out
- the output streamx
- the valuejava.io.IOException
- if an IO exception occurred while writing.public static long readLong(java.io.InputStream in) throws java.io.IOException
in
- the input streamjava.io.IOException
- if an IO exception occurred while reading.public static void writeInt(java.io.OutputStream out, int x) throws java.io.IOException
out
- the output streamx
- the valuejava.io.IOException
- if an IO exception occurred while writing.public static int readInt(java.io.InputStream in) throws java.io.IOException
in
- the input streamjava.io.IOException
- if an IO exception occurred while reading.public static long readVarLong(java.io.InputStream in) throws java.io.IOException
in
- the input streamjava.io.IOException
- if an IO exception occurred while reading.public static long nextPowerOf2(int x)
x
- the original value.public static void closeQuietly(java.io.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(java.net.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(java.io.InputStream input, java.io.OutputStream output) throws java.io.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 tojava.io.IOException
- if an I/O error occurspublic static java.lang.String humanReadableByteCount(long bytes)
Copyright © 2010 - 2020 Adobe. All Rights Reserved