Class IOUtils
- java.lang.Object
 - 
- org.apache.jackrabbit.oak.commons.IOUtils
 
 
- 
public final class IOUtils extends java.lang.ObjectInput/output utility methods. 
- 
- 
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidcloseQuietly(java.io.Closeable closeable)Unconditionally close aCloseable.static voidcloseQuietly(java.net.Socket sock)Unconditionally close aSocket.static longcopy(java.io.InputStream input, java.io.OutputStream output)Copy bytes from anInputStreamto anOutputStream.static java.lang.StringhumanReadableByteCount(long bytes)Returns a human-readable version of the file size, where the input represents a specific number of bytes.static longnextPowerOf2(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 intreadFully(java.io.InputStream in, byte[] buffer, int off, int max)Try to read the given number of bytes to the buffer.static intreadFully(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 intreadInt(java.io.InputStream in)Read an integer (4 bytes).static longreadLong(java.io.InputStream in)Read a long (8 bytes).static java.lang.StringreadString(java.io.InputStream in)Read a String.static intreadVarInt(java.io.InputStream in)Read a variable size integer.static longreadVarLong(java.io.InputStream in)Read a variable size long.static voidskipFully(java.io.InputStream in, long skip)Skip a number of bytes in an input stream.static voidwriteBytes(java.io.OutputStream out, byte[] data)Write a byte array.static voidwriteInt(java.io.OutputStream out, int x)Write an integer (4 bytes).static voidwriteLong(java.io.OutputStream out, long x)Write a long (8 bytes).static voidwriteString(java.io.OutputStream out, java.lang.String s)Write a String.static voidwriteVarInt(java.io.OutputStream out, int x)Write a variable size integer.static voidwriteVarLong(java.io.OutputStream out, long x)Write a variable size long. 
 - 
 
- 
- 
Method Detail
- 
readFully
public static int readFully(java.io.InputStream in, byte[] buffer, int off, int max) throws java.io.IOExceptionTry 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:
 java.io.IOException- If an error occurs.
 
- 
readFully
public static int readFully(java.nio.channels.FileChannel channel, int position, java.nio.ByteBuffer buffer) throws java.io.IOExceptionTry 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:
 java.io.IOException- If an error occurs.
 
- 
skipFully
public static void skipFully(java.io.InputStream in, long skip) throws java.io.IOExceptionSkip a number of bytes in an input stream.- Parameters:
 in- the input streamskip- the number of bytes to skip- Throws:
 java.io.EOFException- if the end of file has been reached before all bytes could be skippedjava.io.IOException- if an IO exception occurred while skipping
 
- 
writeString
public static void writeString(java.io.OutputStream out, java.lang.String s) throws java.io.IOExceptionWrite 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:
 java.io.IOException- if an IO exception occurred while writing
 
- 
readString
public static java.lang.String readString(java.io.InputStream in) throws java.io.IOExceptionRead 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:
 java.io.IOException- if an IO exception occurred while reading
 
- 
writeBytes
public static void writeBytes(java.io.OutputStream out, byte[] data) throws java.io.IOExceptionWrite 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:
 java.io.IOException- if an IO exception occurred while writing.
 
- 
readBytes
public static byte[] readBytes(java.io.InputStream in) throws java.io.IOExceptionRead 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:
 java.io.IOException- if an IO exception occurred while reading from the stream.
 
- 
writeVarInt
public static void writeVarInt(java.io.OutputStream out, int x) throws java.io.IOExceptionWrite a variable size integer. Negative values need 5 bytes.- Parameters:
 out- the output streamx- the value- Throws:
 java.io.IOException- if an IO exception occurred while writing.
 
- 
readVarInt
public static int readVarInt(java.io.InputStream in) throws java.io.IOExceptionRead a variable size integer.- Parameters:
 in- the input stream- Returns:
 - the integer
 - Throws:
 java.io.IOException- if an IO exception occurred while reading.
 
- 
writeVarLong
public static void writeVarLong(java.io.OutputStream out, long x) throws java.io.IOExceptionWrite a variable size long. Negative values need 10 bytes.- Parameters:
 out- the output streamx- the value- Throws:
 java.io.IOException- if an IO exception occurred while writing.
 
- 
writeLong
public static void writeLong(java.io.OutputStream out, long x) throws java.io.IOExceptionWrite a long (8 bytes).- Parameters:
 out- the output streamx- the value- Throws:
 java.io.IOException- if an IO exception occurred while writing.
 
- 
readLong
public static long readLong(java.io.InputStream in) throws java.io.IOExceptionRead a long (8 bytes).- Parameters:
 in- the input stream- Returns:
 - the value
 - Throws:
 java.io.IOException- if an IO exception occurred while reading.
 
- 
writeInt
public static void writeInt(java.io.OutputStream out, int x) throws java.io.IOExceptionWrite an integer (4 bytes).- Parameters:
 out- the output streamx- the value- Throws:
 java.io.IOException- if an IO exception occurred while writing.
 
- 
readInt
public static int readInt(java.io.InputStream in) throws java.io.IOExceptionRead an integer (4 bytes).- Parameters:
 in- the input stream- Returns:
 - the value
 - Throws:
 java.io.IOException- if an IO exception occurred while reading.
 
- 
readVarLong
public static long readVarLong(java.io.InputStream in) throws java.io.IOExceptionRead a variable size long.- Parameters:
 in- the input stream- Returns:
 - the long
 - Throws:
 java.io.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
public static void closeQuietly(java.io.Closeable closeable)
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
public static void closeQuietly(java.net.Socket sock)
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
public static long copy(java.io.InputStream input, java.io.OutputStream output) throws java.io.IOExceptionCopy bytes from anInputStreamto anOutputStream.This method buffers the input internally, so there is no need to use a
BufferedInputStream.- Parameters:
 input- theInputStreamto read fromoutput- theOutputStreamto write to- Returns:
 - the number of bytes copied
 - Throws:
 java.io.IOException- if an I/O error occurs
 
- 
humanReadableByteCount
public 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. Based on http://stackoverflow.com/a/3758880/1035417 
 - 
 
 -