Class IOUtils
java.lang.Object
org.apache.jackrabbit.oak.commons.IOUtils
Input/output utility methods.
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
closeQuietly
(Closeable closeable) Unconditionally close aCloseable
.static void
closeQuietly
(Socket sock) Unconditionally close aSocket
.static long
copy
(InputStream input, OutputStream output) Copy bytes from anInputStream
to anOutputStream
.static String
humanReadableByteCount
(long bytes) Returns a human-readable version of the file size, where the input represents a specific number of bytes.static String
humanReadableByteCountBin
(long bytes) Returns a human-readable version of this byte count.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
(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
readFully
(FileChannel channel, int position, ByteBuffer buffer) Try to read the given number of bytes starting at the specified position into the buffer.static int
readInt
(InputStream in) Read an integer (4 bytes).static long
readLong
(InputStream in) Read a long (8 bytes).static String
Read a String.static int
Read a variable size integer.static long
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.
-
Method Details
-
readFully
Try to read the given number of bytes to the buffer. This method reads until the maximum number of bytes have been read or until the end of file.- Parameters:
in
- the input streambuffer
- the output bufferoff
- the offset in the buffermax
- the number of bytes to read at most- Returns:
- the number of bytes read, 0 meaning EOF or no space in buffer
- Throws:
IOException
- If an error occurs.
-
readFully
public static int readFully(FileChannel channel, int position, ByteBuffer buffer) throws IOException Try to read the given number of bytes starting at the specified position into the buffer. This method reads until the maximum number of bytes have been read or until the end of the channel.- Parameters:
channel
- the input channelposition
- the position to start reading from the channelbuffer
- the output buffer- Returns:
- the number of bytes read, 0 meaning EOF or no space in buffer
- Throws:
IOException
- If an error occurs.
-
skipFully
Skip a number of bytes in an input stream.- Parameters:
in
- the input streamskip
- the number of bytes to skip- Throws:
EOFException
- if the end of file has been reached before all bytes could be skippedIOException
- if an IO exception occurred while skipping
-
writeString
Write a String. This will first write the length as 4 bytes, and then the UTF-8 encoded string.- Parameters:
out
- the data output streams
- the string (maximum length about 2 GB)- Throws:
IOException
- if an IO exception occurred while writing
-
readString
Read a String. This will first read the length as 4 bytes, and then the UTF-8 encoded string.- Parameters:
in
- the data input stream- Returns:
- the string
- Throws:
IOException
- if an IO exception occurred while reading
-
writeBytes
Write a byte array. This will first write the length as 4 bytes, and then the actual bytes.- Parameters:
out
- the data output streamdata
- the byte array- Throws:
IOException
- if an IO exception occurred while writing.
-
readBytes
Read a byte array. This will first read the length as 4 bytes, and then the actual bytes.- Parameters:
in
- the data input stream- Returns:
- the bytes
- Throws:
IOException
- if an IO exception occurred while reading from the stream.
-
writeVarInt
Write a variable size integer. Negative values need 5 bytes.- Parameters:
out
- the output streamx
- the value- Throws:
IOException
- if an IO exception occurred while writing.
-
readVarInt
Read a variable size integer.- Parameters:
in
- the input stream- Returns:
- the integer
- Throws:
IOException
- if an IO exception occurred while reading.
-
writeVarLong
Write a variable size long. Negative values need 10 bytes.- Parameters:
out
- the output streamx
- the value- Throws:
IOException
- if an IO exception occurred while writing.
-
writeLong
Write a long (8 bytes).- Parameters:
out
- the output streamx
- the value- Throws:
IOException
- if an IO exception occurred while writing.
-
readLong
Read a long (8 bytes).- Parameters:
in
- the input stream- Returns:
- the value
- Throws:
IOException
- if an IO exception occurred while reading.
-
writeInt
Write an integer (4 bytes).- Parameters:
out
- the output streamx
- the value- Throws:
IOException
- if an IO exception occurred while writing.
-
readInt
Read an integer (4 bytes).- Parameters:
in
- the input stream- Returns:
- the value
- Throws:
IOException
- if an IO exception occurred while reading.
-
readVarLong
Read a variable size long.- Parameters:
in
- the input stream- Returns:
- the long
- Throws:
IOException
- if an IO exception occurred while reading.
-
nextPowerOf2
public static long nextPowerOf2(int x) Get the value that is equal or higher than this value, and that is a power of two. The returned value will be in the range [0, 2^31]. If the input is less than zero, the result of 1 is returned (powers of negative numbers are not integer values).- Parameters:
x
- the original value.- Returns:
- the next power of two value. Results are always in the range [0, 2^31].
-
closeQuietly
Unconditionally close aCloseable
.Equivalent to
Closeable.close()
, except any exceptions will be ignored. This is typically used in finally blocks.- Parameters:
closeable
- the object to close, may be null or already closed
-
closeQuietly
Unconditionally close aSocket
.Equivalent to
Socket.close()
, except any exceptions will be ignored. This is typically used in finally blocks.- Parameters:
sock
- the Socket to close, may be null or already closed
-
copy
Copy bytes from anInputStream
to anOutputStream
.This method buffers the input internally, so there is no need to use a
BufferedInputStream
.- Parameters:
input
- theInputStream
to read fromoutput
- theOutputStream
to write to- Returns:
- the number of bytes copied
- Throws:
IOException
- if an I/O error occurs
-
humanReadableByteCount
Returns a human-readable version of the file size, where the input represents a specific number of bytes. Based on http://stackoverflow.com/a/3758880/1035417 -
humanReadableByteCountBin
Returns a human-readable version of this byte count. The result uses the binary system, that is, 1K = 1024. Based on https://stackoverflow.com/a/3758880/2071159.
-