org.apache.jackrabbit.mk.util
Class IOUtils

java.lang.Object
  extended by org.apache.jackrabbit.mk.util.IOUtils

public class IOUtils
extends Object

Input/output utility methods.


Constructor Summary
IOUtils()
           
 
Method Summary
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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

IOUtils

public IOUtils()
Method Detail

readFully

public static int readFully(InputStream in,
                            byte[] buffer,
                            int off,
                            int max)
                     throws IOException
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 stream
buffer - the output buffer
off - the offset in the buffer
max - the number of bytes to read at most
Returns:
the number of bytes read, 0 meaning EOF
Throws:
IOException

skipFully

public static void skipFully(InputStream in,
                             long skip)
                      throws IOException
Skip a number of bytes in an input stream.

Parameters:
in - the input stream
skip - the number of bytes to skip
Throws:
EOFException - if the end of file has been reached before all bytes could be skipped
IOException - if an IO exception occurred while skipping

writeString

public static void writeString(OutputStream out,
                               String s)
                        throws IOException
Write a String. This will first write the length as 4 bytes, and then the UTF-8 encoded string.

Parameters:
out - the data output stream
s - the string (maximum length about 2 GB)
Throws:
IOException

readString

public static String readString(InputStream in)
                         throws IOException
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

writeBytes

public static void writeBytes(OutputStream out,
                              byte[] data)
                       throws IOException
Write a byte array. This will first write the length as 4 bytes, and then the actual bytes.

Parameters:
out - the data output stream
data - the byte array
Throws:
IOException

readBytes

public static byte[] readBytes(InputStream in)
                        throws IOException
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

writeVarInt

public static void writeVarInt(OutputStream out,
                               int x)
                        throws IOException
Write a variable size integer. Negative values need 5 bytes.

Parameters:
out - the output stream
x - the value
Throws:
IOException

readVarInt

public static int readVarInt(InputStream in)
                      throws IOException
Read a variable size integer.

Parameters:
in - the input stream
Returns:
the integer
Throws:
IOException

writeVarLong

public static void writeVarLong(OutputStream out,
                                long x)
                         throws IOException
Write a variable size long. Negative values need 10 bytes.

Parameters:
out - the output stream
x - the value
Throws:
IOException

writeLong

public static void writeLong(OutputStream out,
                             long x)
                      throws IOException
Write a long (8 bytes).

Parameters:
out - the output stream
x - the value
Throws:
IOException

readLong

public static long readLong(InputStream in)
                     throws IOException
Read a long (8 bytes).

Parameters:
in - the input stream
Returns:
the value
Throws:
IOException

writeInt

public static void writeInt(OutputStream out,
                            int x)
                     throws IOException
Write an integer (4 bytes).

Parameters:
out - the output stream
x - the value
Throws:
IOException

readInt

public static int readInt(InputStream in)
                   throws IOException
Read an integer (4 bytes).

Parameters:
in - the input stream
Returns:
the value
Throws:
IOException

readVarLong

public static long readVarLong(InputStream in)
                        throws IOException
Read a variable size long.

Parameters:
in - the input stream
Returns:
the long
Throws:
IOException

nextPowerOf2

public static int nextPowerOf2(int x)
Get the value that is equal or higher than this value, and that is a power of two.

Parameters:
x - the original value
Returns:
the next power of two value

closeQuietly

public static void closeQuietly(Closeable closeable)
Unconditionally close a Closeable.

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(Socket sock)
Unconditionally close a Socket.

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(InputStream input,
                        OutputStream output)
                 throws IOException
Copy bytes from an InputStream to an OutputStream.

This method buffers the input internally, so there is no need to use a BufferedInputStream.

Parameters:
input - the InputStream to read from
output - the OutputStream to write to
Returns:
the number of bytes copied
Throws:
IOException - if an I/O error occurs


Copyright © 2012 The Apache Software Foundation. All Rights Reserved.