Class FileIOUtils
- java.lang.Object
-
- org.apache.jackrabbit.oak.commons.FileIOUtils
-
public final class FileIOUtils extends Object
Simple File utils
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
FileIOUtils.TransformingComparator
Decorates the given comparator and applies the function before delegating to the decorated comparator.
-
Field Summary
Fields Modifier and Type Field Description static Comparator<String>
lexComparator
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
append(List<File> files, File appendTo, boolean delete)
Appends the contents of the list of files to the given file and deletes the files if the delete flag is enabled.static File
copy(InputStream stream)
Copies an input stream to a file.static void
copyInputStreamToFile(InputStream source, File destination)
Copy the input stream to the given file.static Comparator<String>
lineBreakAwareComparator(Comparator<String> delegate)
Composing iterator which unescapes for line breaks and delegates to the given comparator.static void
merge(List<File> files, File output)
Merges a list of files after sorting with thelexComparator
.static void
merge(List<File> files, File output, Comparator<String> comparator)
Merges a list of files after sorting with the given comparator.static Set<String>
readStringsAsSet(InputStream stream, boolean unescape)
Reads strings from the given stream into a set and optionally unescaping for line breaks.static void
sort(File file)
Sorts the given file externally using thelexComparator
and removes duplicates.static void
sort(File file, Comparator<String> comparator)
Sorts the given file externally with the given comparator and removes duplicates.static void
writeAsLine(BufferedWriter writer, String str, boolean escape)
Writes a string as a new line into the given buffered writer and optionally escapes the line for line breaks.static int
writeStrings(Iterator<String> iterator, File f, boolean escape)
Writes string from the given iterator to the given file and optionally escape the written strings for line breaks.static int
writeStrings(Iterator<String> iterator, File f, boolean escape, @NotNull Function<String,String> transformer, @Nullable Logger logger, @Nullable String message)
Writes string from the given iterator to the given file and optionally escape the written strings for line breaks.static int
writeStrings(Iterator<String> iterator, File f, boolean escape, @Nullable Logger logger, @Nullable String message)
Writes string from the given iterator to the given file and optionally escape the written strings for line breaks.
-
-
-
Field Detail
-
lexComparator
public static final Comparator<String> lexComparator
-
-
Method Detail
-
sort
public static void sort(File file) throws IOException
Sorts the given file externally using thelexComparator
and removes duplicates.- Parameters:
file
- file whose contents needs to be sorted- Throws:
IOException
-
sort
public static void sort(File file, Comparator<String> comparator) throws IOException
Sorts the given file externally with the given comparator and removes duplicates.- Parameters:
file
- file whose contents needs to be sortedcomparator
- to compare- Throws:
IOException
-
merge
public static void merge(List<File> files, File output) throws IOException
Merges a list of files after sorting with thelexComparator
.- Parameters:
files
- files to mergeoutput
- merge output file- Throws:
IOException
-
merge
public static void merge(List<File> files, File output, Comparator<String> comparator) throws IOException
Merges a list of files after sorting with the given comparator.- Parameters:
files
- files to mergeoutput
- merge output file- Throws:
IOException
-
copy
public static File copy(InputStream stream) throws IOException
Copies an input stream to a file.- Parameters:
stream
- steam to copy- Returns:
- Throws:
IOException
-
append
public static void append(List<File> files, File appendTo, boolean delete) throws IOException
Appends the contents of the list of files to the given file and deletes the files if the delete flag is enabled. If there is a scope for lines in the files containing line break characters it should be ensured that the files are written withwriteAsLine(BufferedWriter, String, boolean)
with true to escape line break characters.- Parameters:
files
-appendTo
-- Throws:
IOException
-
writeAsLine
public static void writeAsLine(BufferedWriter writer, String str, boolean escape) throws IOException
Writes a string as a new line into the given buffered writer and optionally escapes the line for line breaks.- Parameters:
writer
- to write the stringstr
- the string to writeescape
- whether to escape string for line breaks- Throws:
IOException
-
writeStrings
public static int writeStrings(Iterator<String> iterator, File f, boolean escape) throws IOException
Writes string from the given iterator to the given file and optionally escape the written strings for line breaks.- Parameters:
iterator
- the source of the stringsf
- file to write toescape
- whether to escape for line breaks- Returns:
- count
- Throws:
IOException
-
writeStrings
public static int writeStrings(Iterator<String> iterator, File f, boolean escape, @Nullable @Nullable Logger logger, @Nullable @Nullable String message) throws IOException
Writes string from the given iterator to the given file and optionally escape the written strings for line breaks.- Parameters:
iterator
- the source of the stringsf
- file to write toescape
- escape whether to escape for line breakslogger
- logger to log progressmessage
- message to log- Returns:
- Throws:
IOException
-
writeStrings
public static int writeStrings(Iterator<String> iterator, File f, boolean escape, @NotNull @NotNull Function<String,String> transformer, @Nullable @Nullable Logger logger, @Nullable @Nullable String message) throws IOException
Writes string from the given iterator to the given file and optionally escape the written strings for line breaks.- Parameters:
iterator
- the source of the stringsf
- file to write toescape
- escape whether to escape for line breakstransformer
- any transformation on the inputlogger
- logger to log progressmessage
- message to log- Returns:
- Throws:
IOException
-
readStringsAsSet
public static Set<String> readStringsAsSet(InputStream stream, boolean unescape) throws IOException
Reads strings from the given stream into a set and optionally unescaping for line breaks.- Parameters:
stream
- the source of the stringsunescape
- whether to unescape for line breaks- Returns:
- set
- Throws:
IOException
-
lineBreakAwareComparator
public static Comparator<String> lineBreakAwareComparator(Comparator<String> delegate)
Composing iterator which unescapes for line breaks and delegates to the given comparator. When using this it should be ensured that the data source has been correspondingly escaped.- Parameters:
delegate
- the actual comparison iterator- Returns:
- comparator aware of line breaks
-
copyInputStreamToFile
public static void copyInputStreamToFile(InputStream source, File destination) throws IOException
Copy the input stream to the given file. Delete the file in case of exception.- Parameters:
source
- the input stream sourcedestination
- the file to write to- Throws:
IOException
-
-