Class IOUtils


  • public final class IOUtils
    extends java.lang.Object
    Input/output utility methods.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method 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.io.InputStream in, byte[] buffer, int off, int max)
      Try to read the given number of bytes to the buffer.
      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 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.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • readFully

        public static int readFully​(java.io.InputStream in,
                                    byte[] buffer,
                                    int off,
                                    int max)
                             throws java.io.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:
        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.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:
        java.io.IOException - If an error occurs.
      • skipFully

        public static void skipFully​(java.io.InputStream in,
                                     long skip)
                              throws java.io.IOException
        Skip a number of bytes in an input stream.
        Parameters:
        in - the input stream
        skip - the number of bytes to skip
        Throws:
        java.io.EOFException - if the end of file has been reached before all bytes could be skipped
        java.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.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:
        java.io.IOException - if an IO exception occurred while writing
      • readString

        public static java.lang.String readString​(java.io.InputStream in)
                                           throws java.io.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:
        java.io.IOException - if an IO exception occurred while reading
      • writeBytes

        public static void writeBytes​(java.io.OutputStream out,
                                      byte[] data)
                               throws java.io.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:
        java.io.IOException - if an IO exception occurred while writing.
      • readBytes

        public static byte[] readBytes​(java.io.InputStream in)
                                throws java.io.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:
        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.IOException
        Write a variable size integer. Negative values need 5 bytes.
        Parameters:
        out - the output stream
        x - 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.IOException
        Read 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.IOException
        Write a variable size long. Negative values need 10 bytes.
        Parameters:
        out - the output stream
        x - 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.IOException
        Write a long (8 bytes).
        Parameters:
        out - the output stream
        x - 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.IOException
        Read 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.IOException
        Write an integer (4 bytes).
        Parameters:
        out - the output stream
        x - 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.IOException
        Read 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.IOException
        Read 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 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​(java.net.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​(java.io.InputStream input,
                                java.io.OutputStream output)
                         throws java.io.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:
        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