Class IOUtils

java.lang.Object
org.apache.jackrabbit.oak.commons.IOUtils

public final class IOUtils extends Object
Input/output utility methods.
  • Method Details

    • 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 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 channel
      position - the position to start reading from the channel
      buffer - the output buffer
      Returns:
      the number of bytes read, 0 meaning EOF or no space in buffer
      Throws:
      IOException - If an error occurs.
    • 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 - if an IO exception occurred while writing
    • 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 - if an IO exception occurred while reading
    • 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 - if an IO exception occurred while writing.
    • 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 - if an IO exception occurred while reading from the stream.
    • 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 - if an IO exception occurred while writing.
    • readVarInt

      public static int readVarInt(InputStream in) throws IOException
      Read a variable size integer.
      Parameters:
      in - the input stream
      Returns:
      the integer
      Throws:
      IOException - if an IO exception occurred while reading.
    • 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 - if an IO exception occurred while writing.
    • 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 - if an IO exception occurred while writing.
    • readLong

      public static long readLong(InputStream in) throws IOException
      Read a long (8 bytes).
      Parameters:
      in - the input stream
      Returns:
      the value
      Throws:
      IOException - if an IO exception occurred while reading.
    • 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 - if an IO exception occurred while writing.
    • readInt

      public static int readInt(InputStream in) throws IOException
      Read an integer (4 bytes).
      Parameters:
      in - the input stream
      Returns:
      the value
      Throws:
      IOException - if an IO exception occurred while reading.
    • readVarLong

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

      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
    • humanReadableByteCount

      public static 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
    • humanReadableByteCountBin

      public static String humanReadableByteCountBin(long bytes)
      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.