- All Implemented Interfaces:
IFileUtils
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidcheckFileIsWithinDirectory(String path, String directoryPath) This method is to provide assurance that the file specified by the path parameter is within the directory specified by directoryPath.static voidChecks that the path string avoids bad patterns and meets our whitelist for acceptable characters.voidA wrapper aroundFiles.delete(Path)voidDeletes a directory, deleting everything inside it recursively afterwards.booleandeleteIfExists(Path path) A wrapper aroundFiles.deleteIfExists(Path)booleanexists(Path path, LinkOption... options) A wrapper aroundFiles.exists(Path, LinkOption...)booleanisRegularFile(Path path, LinkOption... options) A wrapper aroundFiles.isRegularFile(Path, LinkOption...)A wrapper aroundFiles.lines(Path, Charset)A wrapper aroundFiles.list(Path)voidmakeDirectory(Path directory) Creates a directory if it doesn't already exist.voidmove(Path source, Path target, CopyOption... options) A wrapper aroundFiles.move(Path, Path, CopyOption...)newBufferedReader(Path path, Charset cs) A wrapper aroundFiles.newBufferedReader(Path, Charset)newBufferedWriter(Path path, Charset cs, OpenOption... options) A wrapper aroundFiles.newBufferedWriter(Path, Charset, OpenOption...)readAllLines(Path path) A wrapper aroundFiles.readAllLines(Path)byte[]readBinaryFile(String path) Read a binary file, return as a byte arrayreadString(Path path) A wrapper aroundFiles.readString(Path)readTextFile(String path) Read a text file from the given path, return as a string.safeResolve(String parentDirectory, String path) This helper method will ensure that the requested path is within the parent directory and using safe characterslongA wrapper aroundFiles.size(Path)walk(Path start, FileVisitOption... options) A wrapper aroundFiles.walk(Path, FileVisitOption...)write(Path path, Iterable<? extends CharSequence> lines, Charset cs, OpenOption... options) A wrapper aroundFiles.write(Path, Iterable, Charset, OpenOption...)voidwriteString(Path path, String content, OpenOption... options) A wrapper aroundFiles.writeString(Path, CharSequence, OpenOption...)
-
Constructor Details
-
FileUtils
-
-
Method Details
-
writeString
Description copied from interface:IFileUtilsA wrapper aroundFiles.writeString(Path, CharSequence, OpenOption...)
Note: This does *not* protect against untrusted data on its own. Call
IFileUtils.safeResolve(String, String)first against the path to ensure it uses valid characters and prevent it escaping the expected directory.- Specified by:
writeStringin interfaceIFileUtils- Throws:
IOException
-
write
public Path write(Path path, Iterable<? extends CharSequence> lines, Charset cs, OpenOption... options) throws IOException Description copied from interface:IFileUtilsA wrapper aroundFiles.write(Path, Iterable, Charset, OpenOption...)- Specified by:
writein interfaceIFileUtils- Throws:
IOException
-
readString
Description copied from interface:IFileUtilsA wrapper aroundFiles.readString(Path)- Specified by:
readStringin interfaceIFileUtils- Returns:
- the value of the file at the path parameter, as a string, presuming UTF-8 encoding
- Throws:
IOException
-
deleteDirectoryRecursivelyIfExists
Description copied from interface:IFileUtilsDeletes a directory, deleting everything inside it recursively afterwards. A more dangerous method than many others, take care.
Note: This does *not* protect against untrusted data on its own. Call
IFileUtils.safeResolve(String, String)first against the path to ensure it uses valid characters and prevent it escaping the expected directory.- Specified by:
deleteDirectoryRecursivelyIfExistsin interfaceIFileUtils- Throws:
IOException
-
makeDirectory
Description copied from interface:IFileUtilsCreates a directory if it doesn't already exist.
Note: This does *not* protect against untrusted data on its own. Call
IFileUtils.safeResolve(String, String)first against the path to ensure it uses valid characters and prevent it escaping the expected directory.- Specified by:
makeDirectoryin interfaceIFileUtils- Throws:
IOException
-
readBinaryFile
Description copied from interface:IFileUtilsRead a binary file, return as a byte array
Note: This does *not* protect against untrusted data on its own. Call
IFileUtils.safeResolve(String, String)first against the path to ensure it uses valid characters and prevent it escaping the expected directory.- Specified by:
readBinaryFilein interfaceIFileUtils- Throws:
IOException
-
readAllLines
Description copied from interface:IFileUtilsA wrapper aroundFiles.readAllLines(Path)- Specified by:
readAllLinesin interfaceIFileUtils- Throws:
IOException
-
readTextFile
Description copied from interface:IFileUtilsRead a text file from the given path, return as a string.
Note: This does *not* protect against untrusted data on its own. Call
IFileUtils.safeResolve(String, String)first against the path to ensure it uses valid characters and prevent it escaping the expected directory.- Specified by:
readTextFilein interfaceIFileUtils- Throws:
IOException
-
checkFileIsWithinDirectory
Description copied from interface:IFileUtilsThis method is to provide assurance that the file specified by the path parameter is within the directory specified by directoryPath. Use this for any code that reads from files where the user provides untrusted input.- Specified by:
checkFileIsWithinDirectoryin interfaceIFileUtils- Throws:
IOException
-
checkForBadFilePatterns
Checks that the path string avoids bad patterns and meets our whitelist for acceptable characters.- Throws:
IllegalArgumentException- if the input is blankForbiddenUseException- if the path parameter contains known bad patterns or includes characters other than the set of characters we will allow for filenames. It is a small set of ascii characters - alphanumerics, underscore, dash, period, forward and backward slash.
-
safeResolve
Description copied from interface:IFileUtilsThis helper method will ensure that the requested path is within the parent directory and using safe characters- Specified by:
safeResolvein interfaceIFileUtils- Throws:
IOException
-
delete
Description copied from interface:IFileUtilsA wrapper aroundFiles.delete(Path)- Specified by:
deletein interfaceIFileUtils- Throws:
IOException
-
move
Description copied from interface:IFileUtilsA wrapper aroundFiles.move(Path, Path, CopyOption...)- Specified by:
movein interfaceIFileUtils- Throws:
IOException
-
exists
Description copied from interface:IFileUtilsA wrapper aroundFiles.exists(Path, LinkOption...)- Specified by:
existsin interfaceIFileUtils
-
newBufferedWriter
public BufferedWriter newBufferedWriter(Path path, Charset cs, OpenOption... options) throws IOException Description copied from interface:IFileUtilsA wrapper aroundFiles.newBufferedWriter(Path, Charset, OpenOption...)- Specified by:
newBufferedWriterin interfaceIFileUtils- Throws:
IOException
-
newBufferedReader
Description copied from interface:IFileUtilsA wrapper aroundFiles.newBufferedReader(Path, Charset)- Specified by:
newBufferedReaderin interfaceIFileUtils- Throws:
IOException
-
walk
Description copied from interface:IFileUtilsA wrapper aroundFiles.walk(Path, FileVisitOption...)- Specified by:
walkin interfaceIFileUtils- Throws:
IOException
-
isRegularFile
Description copied from interface:IFileUtilsA wrapper aroundFiles.isRegularFile(Path, LinkOption...)- Specified by:
isRegularFilein interfaceIFileUtils
-
lines
Description copied from interface:IFileUtilsA wrapper aroundFiles.lines(Path, Charset)- Specified by:
linesin interfaceIFileUtils- Throws:
IOException
-
deleteIfExists
Description copied from interface:IFileUtilsA wrapper aroundFiles.deleteIfExists(Path)- Specified by:
deleteIfExistsin interfaceIFileUtils- Throws:
IOException
-
size
Description copied from interface:IFileUtilsA wrapper aroundFiles.size(Path)- Specified by:
sizein interfaceIFileUtils- Throws:
IOException
-
list
Description copied from interface:IFileUtilsA wrapper aroundFiles.list(Path)- Specified by:
listin interfaceIFileUtils- Throws:
IOException
-