Class IOUtil

java.lang.Object
de.tomatengames.util.IOUtil

public class IOUtil extends Object
Provides utilities for file system interactions and streams.
Since:
1.0
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    Deletes all files and directories inside the specified directory recursively.
    static void
    Deletes files and directories inside the specified directory recursively.
    static void
    delete(Path path)
    Deletes the specified path.
    static boolean
    isFileInside(File parent, File child)
    Returns whether the specified child file is inside the specified parent file.
    static boolean
    isFileInside(File parent, File child, boolean allowSame)
    Returns whether the specified child file is inside the specified parent file.
    static boolean
    isPathInside(Path parent, Path child)
    Returns whether the specified child path is inside the specified parent path.
    static boolean
    isPathInside(Path parent, Path child, boolean allowSame)
    Returns whether the specified child path is inside the specified parent path.
    static Path[]
    Return an array that contains the entries in the directory.
    static void
    Reads all data from the InputStream and writes it to the OutputStream.
    static byte[]
    Reads alls bytes of the specified file and returns them as a byte array.
    static boolean
    readBoolean(byte[] input, int offset)
    Returns false if and only if input[offset] is 0x00.
    static boolean
    Reads a single byte and returns false if and only if the byte is 0x00.
    static byte[]
    readFully(InputStream in, byte[] arr)
    Reads exactly arr.length bytes from the InputStream into the specified byte array.
    static byte[]
    readFully(InputStream in, byte[] arr, int off, int len)
    Reads exactly len bytes from the InputStream into the specified byte array.
    static int
    readInt(byte[] input, int offset)
    Reads 4 bytes from the byte array starting at the specified offset and returns them as int.
    static int
    Reads 4 bytes from the input stream and returns them as int.
    static long
    readLong(byte[] input, int offset)
    Reads 8 bytes from the byte array starting at the specified offset and returns them as long.
    static long
    Reads 8 bytes from the input stream and returns them as long.
    static int
    readShort(byte[] input, int offset)
    Reads 2 bytes from the byte array starting at the specified offset and returns them as the 2 lower-order bytes of the returned int.
    static int
    Reads 2 bytes from the input stream and returns them as the 2 lower-order bytes of the returned int.
    static String
    Reads a String from the specified InputStream.
    static String
    readString(InputStream in, int byteLengthLimit)
    Reads a String from the specified InputStream.
    static String
    Reads the specified text file into a String.
    static int
    Returns a single byte from the specified input stream.
    static File
    resolveInside(File parent, String subpath)
    Returns the file inside the specified parent file resolved from the specified subpath.
    static File
    resolveInside(File parent, String subpath, boolean allowSame)
    Returns the file inside the specified parent file resolved from the specified subpath.
    static Path
    resolveInside(Path parent, String subpath)
    Returns the path inside the specified parent path resolved from the specified subpath.
    static Path
    resolveInside(Path parent, String subpath, boolean allowSame)
    Returns the path inside the specified parent path resolved from the specified subpath.
    static void
    writeBinaryFile(Path path, byte[] bytes)
    Writes the specified bytes to the specified file.
    static void
    writeBoolean(boolean value, byte[] output, int offset)
    Writes a byte 0x01 if the value is true or 0x00 if the value is false.
    static void
    writeBoolean(boolean value, OutputStream out)
    Writes a byte 0x01 if the value is true or 0x00 if the value is false.
    static void
    writeInt(int value, byte[] output, int offset)
    Writes the 4 bytes of the specified value into the byte array.
    static void
    writeInt(int value, OutputStream out)
    Writes the 4 bytes of the specified value into the output stream.
    static void
    writeLong(long value, byte[] output, int offset)
    Writes the 8 bytes of the specified value into the byte array.
    static void
    writeLong(long value, OutputStream out)
    Writes the 8 bytes of the specified value into the output stream.
    static void
    writeShort(int value, byte[] output, int offset)
    Writes the 2 lower-order bytes of the specified value into the byte array.
    static void
    writeShort(int value, OutputStream out)
    Writes the 2 lower-order bytes of the specified value into the output stream.
    static void
    Writes the specified String to the OutputStream.
    static void
    writeTextFile(Path path, String text)
    Writes the specified text to the specified file.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • writeInt

      public static void writeInt(int value, byte[] output, int offset)
      Writes the 4 bytes of the specified value into the byte array.
      Parameters:
      value - The value.
      output - The byte array into which the value should be written.
      offset - The index of the first written byte.
      Throws:
      ArrayIndexOutOfBoundsException - If offset is negative or the array is too small.
      See Also:
    • writeInt

      public static void writeInt(int value, OutputStream out) throws IOException
      Writes the 4 bytes of the specified value into the output stream.
      Parameters:
      value - The value.
      out - The output stream.
      Throws:
      IOException - If an I/O error occurs.
      See Also:
    • readInt

      public static int readInt(byte[] input, int offset)
      Reads 4 bytes from the byte array starting at the specified offset and returns them as int.
      Parameters:
      input - The byte array.
      offset - The index of the first read byte.
      Returns:
      The 4 read bytes.
      Throws:
      ArrayIndexOutOfBoundsException - If offset is negative or the array is too small.
      See Also:
    • readInt

      public static int readInt(InputStream in) throws IOException
      Reads 4 bytes from the input stream and returns them as int.
      Parameters:
      in - The input stream.
      Returns:
      The read 4 bytes.
      Throws:
      IOException - If an I/O error occurs.
      EOFException - If the end of the stream is reached.
      See Also:
    • writeLong

      public static void writeLong(long value, byte[] output, int offset)
      Writes the 8 bytes of the specified value into the byte array.
      Parameters:
      value - The value.
      output - The byte array into which the value should be written.
      offset - The index of the first written byte.
      Throws:
      ArrayIndexOutOfBoundsException - If offset is negative or the array is too small.
      See Also:
    • writeLong

      public static void writeLong(long value, OutputStream out) throws IOException
      Writes the 8 bytes of the specified value into the output stream.
      Parameters:
      value - The value.
      out - The output stream.
      Throws:
      IOException - If an I/O error occurs.
      See Also:
    • readLong

      public static long readLong(byte[] input, int offset)
      Reads 8 bytes from the byte array starting at the specified offset and returns them as long.
      Parameters:
      input - The byte array.
      offset - The index of the first read byte.
      Returns:
      The 8 read bytes.
      Throws:
      ArrayIndexOutOfBoundsException - If offset is negative or the array is too small.
      See Also:
    • readLong

      public static long readLong(InputStream in) throws IOException
      Reads 8 bytes from the input stream and returns them as long.
      Parameters:
      in - The input stream.
      Returns:
      The read 8 bytes.
      Throws:
      IOException - If an I/O error occurs.
      EOFException - If the end of the stream is reached.
      See Also:
    • writeShort

      public static void writeShort(int value, byte[] output, int offset)
      Writes the 2 lower-order bytes of the specified value into the byte array.
      Parameters:
      value - The value.
      output - The byte array into which the value should be written.
      offset - The index of the first written byte.
      Throws:
      ArrayIndexOutOfBoundsException - If offset is negative or the array is too small.
      See Also:
    • writeShort

      public static void writeShort(int value, OutputStream out) throws IOException
      Writes the 2 lower-order bytes of the specified value into the output stream.
      Parameters:
      value - The value.
      out - The output stream.
      Throws:
      IOException - If an I/O error occurs.
      See Also:
    • readShort

      public static int readShort(byte[] input, int offset)
      Reads 2 bytes from the byte array starting at the specified offset and returns them as the 2 lower-order bytes of the returned int.
      Parameters:
      input - The byte array.
      offset - The index of the first read byte.
      Returns:
      The read 2 bytes.
      Throws:
      ArrayIndexOutOfBoundsException - If offset is negative or the array is too small.
      See Also:
    • readShort

      public static int readShort(InputStream in) throws IOException
      Reads 2 bytes from the input stream and returns them as the 2 lower-order bytes of the returned int.
      Parameters:
      in - The input stream.
      Returns:
      The read 2 bytes.
      Throws:
      IOException - If an I/O error occurs.
      EOFException - If the end of the stream is reached.
      See Also:
    • writeBoolean

      public static void writeBoolean(boolean value, byte[] output, int offset)
      Writes a byte 0x01 if the value is true or 0x00 if the value is false.
      Parameters:
      value - The value.
      output - The byte array into which the value should be written.
      offset - The index of the written byte.
      Throws:
      ArrayIndexOutOfBoundsException - If offset is negative or the array is too small.
      See Also:
    • writeBoolean

      public static void writeBoolean(boolean value, OutputStream out) throws IOException
      Writes a byte 0x01 if the value is true or 0x00 if the value is false.
      Parameters:
      value - The value.
      out - The output stream.
      Throws:
      IOException - If an I/O error occurs.
      See Also:
    • readBoolean

      public static boolean readBoolean(byte[] input, int offset)
      Returns false if and only if input[offset] is 0x00.
      Parameters:
      input - The byte array.
      offset - The index of the read byte.
      Returns:
      If the read byte is not 0x00.
      Throws:
      ArrayIndexOutOfBoundsException - If offset is negative or the array is too small.
      See Also:
    • readBoolean

      public static boolean readBoolean(InputStream in) throws IOException
      Reads a single byte and returns false if and only if the byte is 0x00.
      Parameters:
      in - The input stream.
      Returns:
      If the read byte is not 0x00.
      Throws:
      IOException - If an I/O error occurs.
      EOFException - If the end of the stream is reached.
      See Also:
    • readUByte

      public static int readUByte(InputStream in) throws IOException
      Returns a single byte from the specified input stream.
      Parameters:
      in - The input stream.
      Returns:
      A single byte of data. Only the 8 least significant bits may be set.
      Throws:
      IOException - If an I/O error occurs.
      EOFException - If the end of the stream is reached.
    • readFully

      public static byte[] readFully(InputStream in, byte[] arr, int off, int len) throws IOException
      Reads exactly len bytes from the InputStream into the specified byte array. off is the first byte array index that is written.
      Parameters:
      in - The input stream. Must not be null.
      arr - The array to write the read data in. Must not be null.
      off - The first index of the byte array that should be written.
      len - The amount of bytes to read. If negative, no bytes are read.
      Returns:
      arr
      Throws:
      IOException - If an I/O error occurs.
      EOFException - If the input stream reaches the end before reading len bytes.
      Since:
      1.1
    • readFully

      public static byte[] readFully(InputStream in, byte[] arr) throws IOException
      Reads exactly arr.length bytes from the InputStream into the specified byte array.
      Parameters:
      in - The input stream. Must not be null.
      arr - The array to write the read data in. Must not be null.
      Returns:
      arr
      Throws:
      IOException - If an I/O error occurs.
      EOFException - If the input stream reaches the end before reading arr.length bytes.
      Since:
      1.1
    • writeString

      public static void writeString(String str, OutputStream out) throws IOException
      Writes the specified String to the OutputStream.

      The first 4 bytes represent the amount of bytes of the string. These bytes are followed by the UTF-8 byte representation of the string. If the string is null, the first 4 bytes represent -1 and no further bytes are written.

      Parameters:
      str - The string that should be written. May be null.
      out - The output stream. Must not be null.
      Throws:
      IOException - If an I/O error occurs.
      Since:
      1.1
      See Also:
    • readString

      public static String readString(InputStream in, int byteLengthLimit) throws IOException
      Reads a String from the specified InputStream. This method uses the protocol specified by writeString(String, OutputStream).
      Parameters:
      in - The input stream. Must not be null.
      byteLengthLimit - The maximum amount of bytes that represent the string. This parameter should be used if the input stream is not trusted.
      Returns:
      The read string. May be null.
      Throws:
      IOException - If an I/O error occurs or the byte length is exceeded.
      EOFException - If the input streams ends before the string is fully read.
      Since:
      1.1
      See Also:
    • readString

      public static String readString(InputStream in) throws IOException
      Reads a String from the specified InputStream. This method uses the protocol specified by writeString(String, OutputStream).
      Parameters:
      in - The input stream. Must not be null.
      Returns:
      The read string. May be null.
      Throws:
      IOException - If an I/O error occurs.
      EOFException - If the input streams ends before the string is fully read.
      Since:
      1.1
      See Also:
    • pipeStream

      public static void pipeStream(InputStream in, OutputStream out) throws IOException
      Reads all data from the InputStream and writes it to the OutputStream.
      Parameters:
      in - The input stream.
      out - Th output stream.
      Throws:
      IOException - If an I/O error occurs.
    • delete

      public static void delete(Path path) throws IOException
      Deletes the specified path. If the path represents a file, the file is deleted. If the path represents a directory, the directory is deleted recursively.

      Symbolic links are not followed, but the symbolic links themselves are deleted.

      If the path represents a directory, all inner files and directories must be on the same FileStore as the specified directory. That means that this method should not be used to delete mount points.

      Parameters:
      path - The path that should be deleted. If null, nothing happens.
      Throws:
      IOException - If an error occurs. For example, if the path does not exist or cannot be accessed.
    • cleanDirectory

      public static void cleanDirectory(Path dir, Predicate<Path> filter) throws IOException
      Deletes files and directories inside the specified directory recursively. The specified directory itself is not deleted.

      Files inside the specified directory that do not match the predicate are not deleted. This only applies for the direct file list of the directory. The predicate is not checked recursively.

      Symbolic links are not followed, but they may be deleted themselves.

      All inner files and directories must be on the same FileStore as the specified directory. That means that this method should not be used to delete mount points.

      Parameters:
      dir - The directory. If null, a symbolic link or not a directory, nothing happens.
      filter - A predicate to filter the entries in the file list to delete. If null, all files are deleted.
      Throws:
      IOException - If an error occurs.
    • cleanDirectory

      public static void cleanDirectory(Path dir) throws IOException
      Deletes all files and directories inside the specified directory recursively. The specified directory itself is not deleted.

      Symbolic links are not followed.

      Parameters:
      dir - The directory. If null, a symbolic link or not a directory, nothing happens.
      Throws:
      IOException - If an error occurs.
    • writeTextFile

      public static void writeTextFile(Path path, String text) throws IOException
      Writes the specified text to the specified file. The UTF-8 encoding is used. If the file does not exist, it is created. If the file does already exist, it is overwritten.
      Parameters:
      path - The path to the file that should be written. If null, nothing happens.
      text - The text that should be written to the file. If null, the empty string "" is written.
      Throws:
      IOException - If an I/O error occurs.
    • readTextFile

      public static String readTextFile(Path path) throws IOException
      Reads the specified text file into a String. The UTF-8 encoding is used.
      Parameters:
      path - The path to the file that should be read. If null, the empty string "" is returned.
      Returns:
      The read text. Not null.
      Throws:
      IOException - If an I/O error occurs. For example, the file does not exist, cannot be read or contains malformed byte sequences.
    • writeBinaryFile

      public static void writeBinaryFile(Path path, byte[] bytes) throws IOException
      Writes the specified bytes to the specified file. If the file does not exist, it is created. If the file does already exist, it is overwritten.
      Parameters:
      path - The path to the file that should be written. If null, nothing happens.
      bytes - The bytes that should be written to the file. If null, the file will be empty.
      Throws:
      IOException - If an I/O error occurs.
      Since:
      1.1
    • readBinaryFile

      public static byte[] readBinaryFile(Path path) throws IOException
      Reads alls bytes of the specified file and returns them as a byte array.
      Parameters:
      path - The path to the file that should be read. If null, an empty byte array is returned.
      Returns:
      The read bytes. Not null.
      Throws:
      IOException - If an I/O error occurs.
      Since:
      1.1
    • listDirectory

      public static Path[] listDirectory(Path dir) throws IOException
      Return an array that contains the entries in the directory. This method behaves like Files.list(Path), but all entries are read and the stream is closed.
      Parameters:
      dir - The path to the directory.
      Returns:
      An array that contains the entries in the directory.
      Throws:
      IOException - See Files.list(Path).
      Since:
      1.2
    • isFileInside

      public static boolean isFileInside(File parent, File child) throws IOException
      Returns whether the specified child file is inside the specified parent file.
      Parameters:
      parent - the parent file
      child - the child file
      Returns:
      whether the specified child file is inside the specified parent file
      Throws:
      IOException - if an IOException occurs from constructing the related AbsoluteFiles
      Since:
      1.6
    • isFileInside

      public static boolean isFileInside(File parent, File child, boolean allowSame) throws IOException
      Returns whether the specified child file is inside the specified parent file.
      Parameters:
      parent - the parent file
      child - the child file
      allowSame - whether to return true if the specified parent and child files are equal
      Returns:
      whether the specified child file is inside the specified parent file
      Throws:
      IOException - if an IOException occurs from constructing the related AbsoluteFiles
      Since:
      1.6
    • resolveInside

      public static File resolveInside(File parent, String subpath) throws IOException
      Returns the file inside the specified parent file resolved from the specified subpath.
      Parameters:
      parent - the parent file
      subpath - the subpath to resolve
      Returns:
      the resolved file
      Throws:
      IOException - if an IOException occurs from constructing the related AbsoluteFiles
      FileNotFoundException - if the resolved file is not inside the specified parent file
      Since:
      1.6
    • resolveInside

      public static File resolveInside(File parent, String subpath, boolean allowSame) throws IOException
      Returns the file inside the specified parent file resolved from the specified subpath.
      Parameters:
      parent - the parent file
      subpath - the subpath to resolve
      allowSame - whether to accept the resolved file if it equals the specified parent file
      Returns:
      the resolved file
      Throws:
      IOException - if an IOException occurs from constructing the related AbsoluteFiles
      FileNotFoundException - if the resolved file is not inside the specified parent file
      Since:
      1.6
    • isPathInside

      public static boolean isPathInside(Path parent, Path child)
      Returns whether the specified child path is inside the specified parent path.
      Parameters:
      parent - the parent path
      child - the child path
      Returns:
      whether this specified child path is inside the specified parent path
      Since:
      1.6
    • isPathInside

      public static boolean isPathInside(Path parent, Path child, boolean allowSame)
      Returns whether the specified child path is inside the specified parent path.
      Parameters:
      parent - the parent path
      child - the child path
      allowSame - whether to return true if the specified parent and child files are equal
      Returns:
      whether this specified child path is inside the specified parent path
      Since:
      1.6
    • resolveInside

      public static Path resolveInside(Path parent, String subpath)
      Returns the path inside the specified parent path resolved from the specified subpath.
      Parameters:
      parent - the parent path
      subpath - the subpath to resolve
      Returns:
      the resolved path
      Throws:
      InvalidPathException - if the resolved path is not inside the specified parent path
      Since:
      1.6
    • resolveInside

      public static Path resolveInside(Path parent, String subpath, boolean allowSame)
      Returns the path inside the specified parent path resolved from the specified subpath.
      Parameters:
      parent - the parent path
      subpath - the subpath to resolve
      allowSame - whether to accept the resolved path if it equals the specified parent path
      Returns:
      the resolved path
      Throws:
      InvalidPathException - if the resolved path is not inside the specified parent path
      Since:
      1.6