Class IOUtil
- Since:
- 1.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidcleanDirectory(Path dir) Deletes all files and directories inside the specified directory recursively.static voidcleanDirectory(Path dir, Predicate<Path> filter) Deletes files and directories inside the specified directory recursively.static voidDeletes the specified path.static booleanisFileInside(File parent, File child) Returns whether the specified child file is inside the specified parent file.static booleanisFileInside(File parent, File child, boolean allowSame) Returns whether the specified child file is inside the specified parent file.static booleanisPathInside(Path parent, Path child) Returns whether the specified child path is inside the specified parent path.static booleanisPathInside(Path parent, Path child, boolean allowSame) Returns whether the specified child path is inside the specified parent path.static Path[]listDirectory(Path dir) Return an array that contains the entries in the directory.static voidpipeStream(InputStream in, OutputStream out) Reads all data from theInputStreamand writes it to theOutputStream.static byte[]readBinaryFile(Path path) Reads alls bytes of the specified file and returns them as a byte array.static booleanreadBoolean(byte[] input, int offset) Returnsfalseif and only ifinput[offset]is0x00.static booleanReads a single byte and returnsfalseif and only if the byte is0x00.static byte[]readFully(InputStream in, byte[] arr) Reads exactlyarr.lengthbytes from theInputStreaminto the specified byte array.static byte[]readFully(InputStream in, byte[] arr, int off, int len) Reads exactlylenbytes from theInputStreaminto the specified byte array.static intreadInt(byte[] input, int offset) Reads 4 bytes from the byte array starting at the specified offset and returns them asint.static intreadInt(InputStream in) Reads 4 bytes from the input stream and returns them asint.static longreadLong(byte[] input, int offset) Reads 8 bytes from the byte array starting at the specified offset and returns them aslong.static longreadLong(InputStream in) Reads 8 bytes from the input stream and returns them aslong.static intreadShort(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 returnedint.static intreadShort(InputStream in) Reads 2 bytes from the input stream and returns them as the 2 lower-order bytes of the returnedint.static StringReads aStringfrom the specifiedInputStream.static StringreadString(InputStream in, int byteLengthLimit) Reads aStringfrom the specifiedInputStream.static StringreadTextFile(Path path) Reads the specified text file into aString.static intreadUByte(InputStream in) Returns a single byte from the specified input stream.static FileresolveInside(File parent, String subpath) Returns the file inside the specified parent file resolved from the specified subpath.static FileresolveInside(File parent, String subpath, boolean allowSame) Returns the file inside the specified parent file resolved from the specified subpath.static PathresolveInside(Path parent, String subpath) Returns the path inside the specified parent path resolved from the specified subpath.static PathresolveInside(Path parent, String subpath, boolean allowSame) Returns the path inside the specified parent path resolved from the specified subpath.static voidwriteBinaryFile(Path path, byte[] bytes) Writes the specified bytes to the specified file.static voidwriteBoolean(boolean value, byte[] output, int offset) Writes a byte0x01if the value istrueor0x00if the value isfalse.static voidwriteBoolean(boolean value, OutputStream out) Writes a byte0x01if the value istrueor0x00if the value isfalse.static voidwriteInt(int value, byte[] output, int offset) Writes the 4 bytes of the specified value into the byte array.static voidwriteInt(int value, OutputStream out) Writes the 4 bytes of the specified value into the output stream.static voidwriteLong(long value, byte[] output, int offset) Writes the 8 bytes of the specified value into the byte array.static voidwriteLong(long value, OutputStream out) Writes the 8 bytes of the specified value into the output stream.static voidwriteShort(int value, byte[] output, int offset) Writes the 2 lower-order bytes of the specified value into the byte array.static voidwriteShort(int value, OutputStream out) Writes the 2 lower-order bytes of the specified value into the output stream.static voidwriteString(String str, OutputStream out) Writes the specifiedStringto theOutputStream.static voidwriteTextFile(Path path, String text) Writes the specified text to the specified file.
-
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
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 asint.- 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
Reads 4 bytes from the input stream and returns them asint.- 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
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 aslong.- 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
Reads 8 bytes from the input stream and returns them aslong.- 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
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 returnedint.- 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
Reads 2 bytes from the input stream and returns them as the 2 lower-order bytes of the returnedint.- 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 byte0x01if the value istrueor0x00if the value isfalse.- 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
Writes a byte0x01if the value istrueor0x00if the value isfalse.- 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) Returnsfalseif and only ifinput[offset]is0x00.- 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
Reads a single byte and returnsfalseif and only if the byte is0x00.- 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
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
Reads exactlylenbytes from theInputStreaminto the specified byte array.offis the first byte array index that is written.- Parameters:
in- The input stream. Must not benull.arr- The array to write the read data in. Must not benull.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 readinglenbytes.- Since:
- 1.1
-
readFully
Reads exactlyarr.lengthbytes from theInputStreaminto the specified byte array.- Parameters:
in- The input stream. Must not benull.arr- The array to write the read data in. Must not benull.- Returns:
arr- Throws:
IOException- If an I/O error occurs.EOFException- If the input stream reaches the end before readingarr.lengthbytes.- Since:
- 1.1
-
writeString
Writes the specifiedStringto theOutputStream.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-1and no further bytes are written.- Parameters:
str- The string that should be written. May benull.out- The output stream. Must not benull.- Throws:
IOException- If an I/O error occurs.- Since:
- 1.1
- See Also:
-
readString
Reads aStringfrom the specifiedInputStream. This method uses the protocol specified bywriteString(String, OutputStream).- Parameters:
in- The input stream. Must not benull.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
Reads aStringfrom the specifiedInputStream. This method uses the protocol specified bywriteString(String, OutputStream).- Parameters:
in- The input stream. Must not benull.- 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
Reads all data from theInputStreamand writes it to theOutputStream.- Parameters:
in- The input stream.out- Th output stream.- Throws:
IOException- If an I/O error occurs.
-
delete
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
FileStoreas the specified directory. That means that this method should not be used to delete mount points.- Parameters:
path- The path that should be deleted. Ifnull, nothing happens.- Throws:
IOException- If an error occurs. For example, if the path does not exist or cannot be accessed.
-
cleanDirectory
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
FileStoreas the specified directory. That means that this method should not be used to delete mount points.- Parameters:
dir- The directory. Ifnull, a symbolic link or not a directory, nothing happens.filter- A predicate to filter the entries in the file list to delete. Ifnull, all files are deleted.- Throws:
IOException- If an error occurs.
-
cleanDirectory
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. Ifnull, a symbolic link or not a directory, nothing happens.- Throws:
IOException- If an error occurs.
-
writeTextFile
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. Ifnull, nothing happens.text- The text that should be written to the file. Ifnull, the empty string""is written.- Throws:
IOException- If an I/O error occurs.
-
readTextFile
Reads the specified text file into aString. The UTF-8 encoding is used.- Parameters:
path- The path to the file that should be read. Ifnull, 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
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. Ifnull, nothing happens.bytes- The bytes that should be written to the file. Ifnull, the file will be empty.- Throws:
IOException- If an I/O error occurs.- Since:
- 1.1
-
readBinaryFile
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. Ifnull, an empty byte array is returned.- Returns:
- The read bytes. Not
null. - Throws:
IOException- If an I/O error occurs.- Since:
- 1.1
-
listDirectory
Return an array that contains the entries in the directory. This method behaves likeFiles.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- SeeFiles.list(Path).- Since:
- 1.2
-
isFileInside
Returns whether the specified child file is inside the specified parent file.- Parameters:
parent- the parent filechild- the child file- Returns:
- whether the specified child file is inside the specified parent file
- Throws:
IOException- if anIOExceptionoccurs from constructing the relatedAbsoluteFiles- Since:
- 1.6
-
isFileInside
Returns whether the specified child file is inside the specified parent file.- Parameters:
parent- the parent filechild- the child fileallowSame- 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 anIOExceptionoccurs from constructing the relatedAbsoluteFiles- Since:
- 1.6
-
resolveInside
Returns the file inside the specified parent file resolved from the specified subpath.- Parameters:
parent- the parent filesubpath- the subpath to resolve- Returns:
- the resolved file
- Throws:
IOException- if anIOExceptionoccurs from constructing the relatedAbsoluteFilesFileNotFoundException- if the resolved file is not inside the specified parent file- Since:
- 1.6
-
resolveInside
Returns the file inside the specified parent file resolved from the specified subpath.- Parameters:
parent- the parent filesubpath- the subpath to resolveallowSame- whether to accept the resolved file if it equals the specified parent file- Returns:
- the resolved file
- Throws:
IOException- if anIOExceptionoccurs from constructing the relatedAbsoluteFilesFileNotFoundException- if the resolved file is not inside the specified parent file- Since:
- 1.6
-
isPathInside
-
isPathInside
Returns whether the specified child path is inside the specified parent path.- Parameters:
parent- the parent pathchild- the child pathallowSame- 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
Returns the path inside the specified parent path resolved from the specified subpath.- Parameters:
parent- the parent pathsubpath- 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
Returns the path inside the specified parent path resolved from the specified subpath.- Parameters:
parent- the parent pathsubpath- the subpath to resolveallowSame- 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
-