public final class IoUtil extends Object
java.io-related utility methods.| Modifier and Type | Class and Description |
|---|---|
static class |
IoUtil.CollisionStrategy
Determines the behavior of the
copyTree(File, File, CollisionStrategy) and copyTree(File, File, CollisionStrategy) methods when files collide while copying. |
static interface |
IoUtil.WritingRunnable
An entity which writes characters to a
Writer. |
| Modifier and Type | Field and Description |
|---|---|
static InputStream |
EMPTY_INPUT_STREAM
An
InputStream that produces exactly 0 bytes. |
static OutputStream |
NULL_OUTPUT_STREAM
An
OutputStream that discards all bytes written to it. |
static InputStream |
ZERO_INPUT_STREAM
An input stream that reads an endless stream of zeros.
|
| Modifier and Type | Method and Description |
|---|---|
static <EX extends Throwable> |
asFile(InputStream inputStream,
boolean closeInputStream,
String prefix,
String suffix,
File directory,
ConsumerWhichThrows<? super File,? extends EX> delegate)
Creates a temporary file, stores all data that can be read from the inputStream into it, closes the
file, invokes the delegate with the file, and eventually deletes the file.
|
static Reader |
asReader(CharSequence cs)
Wraps the given
CharSequence in a Reader - much more efficient than "new
StringReader(cs.toString)". |
static OutputStream |
byteConsumerOutputStream(ConsumerWhichThrows<? super Byte,? extends IOException> delegate) |
static InputStream |
byteProducerInputStream(Producer<? extends Byte> delegate) |
static InputStream |
byteProducerInputStream(ProducerWhichThrows<? extends Byte,? extends IOException> delegate) |
static OutputStream[] |
compareOutput(int n,
Runnable whenIdentical,
Runnable whenNotIdentical)
Creates and returns an array of n
OutputStreams. |
static InputStream |
constantInputStream(byte b) |
static long |
copy(File inputFile,
File outputFile)
Copies the contents of the inputFile to the outputFile.
|
static long |
copy(File inputFile,
File outputFile,
IoUtil.CollisionStrategy collisionStrategy)
Copies the contents of the inputFile to the outputFile.
|
static long |
copy(File inputFile,
OutputStream outputStream,
boolean closeOutputStream)
Copies the contents of the inputFile to the outputStream.
|
static long |
copy(InputStream inputStream,
boolean closeInputStream,
File outputFile)
Copies the contents of the inputStream to the outputFile.
|
static long |
copy(InputStream inputStream,
boolean closeInputStream,
File outputFile,
boolean append)
Copies the contents of the inputStream to the outputFile.
|
static long |
copy(InputStream inputStream,
boolean closeInputStream,
File outputFile,
IoUtil.CollisionStrategy collisionStrategy)
Copies the contents of the inputStream to the outputFile.
|
static long |
copy(InputStream inputStream,
boolean closeInputStream,
OutputStream outputStream,
boolean closeOutputStream)
Copies the contents of the inputStream to the outputStream.
|
static long |
copy(InputStream inputStream,
OutputStream outputStream)
Reads the input stream until end-of-input and writes all data to the output stream.
|
static long |
copy(InputStream inputStream,
OutputStream outputStream,
long n)
Reads at most n bytes from the inputStream and writes all data to the
outputStream.
|
static long |
copy(Readable r,
Appendable a)
Reads the
Readable until end-of-input and writes all data to the Appendable. |
static long |
copy(Reader reader,
boolean closeReader,
File outputFile,
boolean append,
Charset outputCharset)
Copies the contents of the reader to the
outputFile, encoded with the given
outputCharset. |
static long |
copy(Reader reader,
boolean closeReader,
Writer writer,
boolean closeWriter)
Copies the contents of the reader to the writer.
|
static long |
copy(Reader reader,
OutputStream outputStream,
Charset charset)
Reads the reader until end-of-input and writes all data to the output stream.
|
static long |
copy(Reader reader,
Writer writer)
Reads the reader until end-of-input and writes all data to the writer.
|
static long |
copyAvailable(InputStream inputStream,
OutputStream outputStream)
Reads data from the input stream and writes it to the output stream.
|
static long |
copyAvailable(InputStream inputStream,
OutputStream outputStream,
long n)
Reads at most n bytes from the input stream and writes all data to the output stream.
|
static ConsumerWhichThrows<OutputStream,IOException> |
copyFrom(InputStream inputStream) |
static void |
copyResource(Class<?> clasS,
String resourceName,
File toFile,
boolean createMissingParentDirectories)
Copies the contents a resource to the given toFile.
|
static void |
copyResource(Class<?> clasS,
String resourceName,
OutputStream outputStream,
boolean closeOutputStream)
Copies the contents a resource to the given outputStream.
|
static void |
copyResource(ClassLoader classLoader,
String resourceName,
File toFile,
boolean createMissingParentDirectories)
Copies the contents a resource to the given toFile.
|
static void |
copyResource(ClassLoader classLoader,
String resourceName,
OutputStream outputStream,
boolean closeOutputStream)
Copies the contents of a resource to the given outputStream.
|
static RunnableWhichThrows<IOException> |
copyRunnable(InputStream in,
OutputStream out)
Creates and returns a
RunnableWhichThrows that copies bytes from in to out until
end-of-input. |
static void |
copyTree(File source,
File destination,
IoUtil.CollisionStrategy collisionStrategy)
Copies a directory tree.
|
static void |
copyTree(URL source,
File destination,
IoUtil.CollisionStrategy collisionStrategy)
Copies a resource tree to a directory in the file system.
|
static void |
createMissingParentDirectoriesFor(File file)
Creates any missing parent directories for the given file.
|
protected static InputStream |
deleteOnClose(InputStream delegate,
File file) |
static void |
fill(OutputStream outputStream,
byte b,
long count)
Writes count bytes of value b to the given outputStream.
|
static URL |
findOnPath(File[] path,
String resourceName)
Finds and returns a named resource along a "path", e.g. a Java "class path" or "source path".
|
static boolean |
isContentIdentical(File file1,
File file2) |
static OutputStream |
lengthWritten(Consumer<? super Integer> delegate)
Creates and returns an
OutputStream which ignores the data written to it and only honors the
number of bytes written:
Every time data is written to the OutputStream, it invokes the consume() method on the delegate with the number of bytes written (not the cumulated number
of bytes written!). |
static InputStream |
onEndOfInput(InputStream delegate,
Runnable runnable)
Creates and returns an
InputStream that reads from the delegate and invokes the
runnable on the first end-of-input condition. |
static <EX extends Throwable> |
outputFile(File file,
ConsumerWhichThrows<? super File,? extends EX> delegate,
boolean createMissingParentDirectories)
Creates a temporary file, invokes the delegate with that file, and eventually renames the
temporary file to its "real" name (replacing a possibly existing file).
|
static <EX extends Throwable> |
outputFileOutputStream(File file,
ConsumerWhichThrows<? super OutputStream,? extends EX> delegate,
boolean createMissingParentDirectories)
Lets the delegate write to an
OutputStream (effectively a temporary file), and eventually
renames the temporary file to "file" (replacing a possibly existing file). |
static <EX extends Throwable> |
outputFilePrintWriter(File file,
Charset charset,
ConsumerWhichThrows<? super PrintWriter,? extends EX> printer)
Equivalent with
printToFile(file, charset, printer, false). |
static <EX extends Throwable> |
outputFilePrintWriter(File file,
Charset charset,
ConsumerWhichThrows<? super PrintWriter,? extends EX> delegate,
boolean createMissingParentDirectories)
Lets the delegate print to a
PrintWriter (effectively a temporary file), and eventually
renames the temporary file to "file" (replacing a possibly existing file). |
static void |
parallel(IoUtil.WritingRunnable[] writingRunnables,
Writer writer)
Executes the writingRunnables in parallel, concatenates their output, and writes it to the
writer, i.e. the output of the runnables does not mix, but the complete output of the first
runnable appears before that of the second runnable, and so on. |
static InputStream |
randomInputStream(long seed) |
static byte[] |
readAll(InputStream is) |
static String |
readAll(InputStream inputStream,
Charset charset,
boolean closeInputStream) |
static String |
readAll(Reader reader) |
static String |
readAll(Reader reader,
boolean closeReader) |
static long |
skip(InputStream inputStream,
long n)
Skips n bytes on the inputStream.
|
static long |
skipAll(InputStream inputStream)
Skips all remaining data on the inputStream.
|
static OutputStream |
split(ProducerWhichThrows<? extends OutputStream,? extends IOException> delegates,
Producer<? extends Long> byteCountLimits)
Creates and returns an
OutputStream which writes at most byteCountLimits.produce()
bytes to delegates.produce() before closing it and writing the next
byteCountLimits.produce() bytes to delegates.produce(), and so on. |
static OutputStream |
tee(OutputStream... delegates)
Creates and returns an
OutputStream that delegates all work to the given delegates:
The write() methods write the given data to all the delegates;
if any of these throw an IOException, it is rethrown, and it is undefined whether all the data was
written to all the delegates. |
static InputStream |
unclosableInputStream(InputStream delegate) |
static OutputStream |
unclosableOutputStream(OutputStream delegate) |
static long |
writeAndCount(ConsumerWhichThrows<? super OutputStream,? extends IOException> writeContents,
OutputStream outputStream)
Invokes writeContents
.consume() with an output stream subject that writes the data through to
the given outputStream. |
static InputStream |
wye(InputStream in,
OutputStream out)
Creates and returns a
FilterInputStream that duplicates all bytes read through it and writes them to
an OutputStream. |
public static final InputStream EMPTY_INPUT_STREAM
InputStream that produces exactly 0 bytes.public static final OutputStream NULL_OUTPUT_STREAM
OutputStream that discards all bytes written to it.public static final InputStream ZERO_INPUT_STREAM
@Nullable public static URL findOnPath(@Nullable File[] path, String resourceName) throws IOException
path - Each element should designate either a directory or a JAR (or ZIP) filenull if path == null, or if the resource could not be foundIOExceptionpublic static long copy(InputStream inputStream, OutputStream outputStream) throws IOException
IOExceptionpublic static long copy(InputStream inputStream, OutputStream outputStream, long n) throws IOException
IOExceptionpublic static long copy(InputStream inputStream, boolean closeInputStream, OutputStream outputStream, boolean closeOutputStream) throws IOException
closeInputStream - Whether to close the inputStream (also if an IOException is thrown)closeOutputStream - Whether to close the outputStream (also if an IOException is thrown)IOExceptionpublic static RunnableWhichThrows<IOException> copyRunnable(InputStream in, OutputStream out)
RunnableWhichThrows that copies bytes from in to out until
end-of-input.public static long copy(Reader reader, Writer writer) throws IOException
IOExceptionpublic static long copy(Reader reader, boolean closeReader, Writer writer, boolean closeWriter) throws IOException
IOExceptionpublic static long copy(Reader reader, OutputStream outputStream, Charset charset) throws IOException
IOExceptionpublic static long copy(Readable r, Appendable a) throws IOException
Readable until end-of-input and writes all data to the Appendable.IOExceptionpublic static long copy(InputStream inputStream, boolean closeInputStream, File outputFile, boolean append) throws IOException
IOExceptionpublic static long copy(Reader reader, boolean closeReader, File outputFile, boolean append, Charset outputCharset) throws IOException
outputFile, encoded with the given
outputCharset. Attempts to delete a partially written output file if the operation fails.IOExceptionpublic static long copy(File inputFile, OutputStream outputStream, boolean closeOutputStream) throws IOException
closeOutputStream - Whether to close the outputStream when execution completesIOExceptionpublic static long copy(InputStream inputStream, boolean closeInputStream, File outputFile) throws IOException
The parent directory of the outputFile must already exist.
IOExceptionpublic static long copy(InputStream inputStream, boolean closeInputStream, File outputFile, IoUtil.CollisionStrategy collisionStrategy) throws IOException
The parent directory of the outputFile must already exist.
IOExceptionpublic static long copy(File inputFile, File outputFile) throws IOException
If the output file already exists, it is silently re-created (and its original content is lost).
The parent directory of the outputFile must already exist.
IOExceptionpublic static long copy(File inputFile, File outputFile, IoUtil.CollisionStrategy collisionStrategy) throws IOException
If the output file already exists, then the collisionStartegy determines what is done.
The parent directory of the outputFile must already exist.
== IoUtil.CollisionStrategy.LEAVE_OLD || collisionStrategy == IoUtil.CollisionStrategy.IO_EXCEPTION_IF_DIFFERENTIOExceptionIoUtil.CollisionStrategypublic static void copyTree(File source, File destination, IoUtil.CollisionStrategy collisionStrategy) throws IOException
Iff the source is a normal file, then copy(File, File, CollisionStrategy) is called.
Otherwise, the destination is created as a directory (if it did not exist), and all members of the source directory are recursively copied to the destination directory.
The parent directory for the destination must already exist.
IOExceptionpublic static void copyTree(URL source, File destination, IoUtil.CollisionStrategy collisionStrategy) throws IOException
Iff the source is a "content resource", then copy(InputStream, boolean, File) is
called.
Otherwise, the destination is created as a directory (if it did not exist), and all members of the source directory are recursively copied to the destination directory.
The parent directory for the destination must already exist.
IOExceptionpublic static ConsumerWhichThrows<OutputStream,IOException> copyFrom(InputStream inputStream)
Comsumer<OutputStream> which copies inputStream to its subjectpublic static boolean isContentIdentical(File file1, File file2) throws IOException
IOExceptionpublic static OutputStream tee(OutputStream... delegates)
OutputStream that delegates all work to the given delegates:
write() methods write the given data to all the delegates;
if any of these throw an IOException, it is rethrown, and it is undefined whether all the data was
written to all the delegates.
flush() flushes the delegates; throws the first IOException that any
of the delegates throws.
close() attempts to close all the delegates; if any of these
throw IOExceptions, one of them is rethrown.
public static InputStream wye(InputStream in, OutputStream out)
FilterInputStream that duplicates all bytes read through it and writes them to
an OutputStream.
The OutputStream is flushed on end-of-input and calls to InputStream.available().
public static long writeAndCount(ConsumerWhichThrows<? super OutputStream,? extends IOException> writeContents, OutputStream outputStream) throws IOException
.consume() with an output stream subject that writes the data through to
the given outputStream.IOExceptionpublic static void parallel(IoUtil.WritingRunnable[] writingRunnables, Writer writer)
writer, i.e. the output of the runnables does not mix, but the complete output of the first
runnable appears before that of the second runnable, and so on.
Since the character buffer for each IoUtil.WritingRunnable has a limited size, the runnables with higher
indexes tend to block if the runnables with lower indexes do not complete quickly enough.
public static byte[] readAll(InputStream is) throws IOException
InputStream producesIOExceptionpublic static String readAll(InputStream inputStream, Charset charset, boolean closeInputStream) throws IOException
InputStream produces, decoded into a stringIOExceptionpublic static long skip(InputStream inputStream, long n) throws IOException
InputStream.skip(long) sometimes
skips less than the requested number of bytes for no good reason, e.g. BufferedInputStream.skip(long)
is known for that. This method tries "harder" to skip exactly the requested number of bytes.IOExceptionInputStream.skip(long)public static long skipAll(InputStream inputStream) throws IOException
IOExceptionInputStream.skip(long)public static OutputStream split(ProducerWhichThrows<? extends OutputStream,? extends IOException> delegates, Producer<? extends Long> byteCountLimits) throws IOException
OutputStream which writes at most byteCountLimits.produce()
bytes to delegates.produce() before closing it and writing the next
byteCountLimits.produce() bytes to delegates.produce(), and so on.delegates - Must produce a (non-null) series of OutputStreamsbyteCountLimits - Must produce a (non-null) series of LongsIOExceptionpublic static InputStream constantInputStream(byte b)
public static InputStream unclosableInputStream(InputStream delegate)
InputStream which ignores all invocations of InputStream.close()public static OutputStream unclosableOutputStream(OutputStream delegate)
OutputStream which ignores all invocations of OutputStream.close()public static void fill(OutputStream outputStream, byte b, long count) throws IOException
IOExceptionpublic static InputStream byteProducerInputStream(ProducerWhichThrows<? extends Byte,? extends IOException> delegate)
null
products are returned as 'end-of-input'public static InputStream byteProducerInputStream(Producer<? extends Byte> delegate)
null
products are returned as 'end-of-input'public static InputStream randomInputStream(long seed)
public static OutputStream byteConsumerOutputStream(ConsumerWhichThrows<? super Byte,? extends IOException> delegate)
public static String readAll(Reader reader) throws IOException
Reader producesIOExceptionpublic static String readAll(Reader reader, boolean closeReader) throws IOException
closeReader - Whether the reader should be closed before the method returnsReader producesIOExceptionprotected static InputStream deleteOnClose(InputStream delegate, File file)
InputStream which first closes the delegate, and then attempts to delete the
filepublic static OutputStream[] compareOutput(int n, Runnable whenIdentical, Runnable whenNotIdentical)
OutputStreams.
Iff exactly the same bytes are written to all of these streams, and then all the streams are closed, then whenIdentical will be run (exactly once).
Otherwise, when the first non-identical byte is written to one of the streams, or at the latest when that stream is closed, whenNotIdentical will be run (possibly more than once).
public static OutputStream lengthWritten(Consumer<? super Integer> delegate)
OutputStream which ignores the data written to it and only honors the
number of bytes written:
Every time data is written to the OutputStream, it invokes the consume() method on the delegate with the number of bytes written (not the cumulated number
of bytes written!).
public static Reader asReader(CharSequence cs)
CharSequence in a Reader - much more efficient than "new
StringReader(cs.toString)".public static void copyResource(ClassLoader classLoader, String resourceName, OutputStream outputStream, boolean closeOutputStream) throws IOException
The resource is addressed by the classLoader and the resourceName, as described for
ClassLoader.getResourceAsStream(String).
closeOutputStream - Whether the outputStream should be closed after the content of the resource
has been copiedIOExceptionpublic static void copyResource(Class<?> clasS, String resourceName, OutputStream outputStream, boolean closeOutputStream) throws IOException
The resource is addressed by the clasS and the resourceName, as described for Class.getResourceAsStream(String).
closeOutputStream - Whether the outputStream should be closed after the content of the resource
has been copiedIOExceptionpublic static void copyResource(ClassLoader classLoader, String resourceName, File toFile, boolean createMissingParentDirectories) throws IOException
The resource is addressed by the classLoader and the resourceName, as described for
ClassLoader.getResourceAsStream(String).
createMissingParentDirectories - Whether to create any missing parent directories for the toFileIOExceptionpublic static void copyResource(Class<?> clasS, String resourceName, File toFile, boolean createMissingParentDirectories) throws IOException
The resource is addressed by the clasS and the resourceName, as described for Class.getResourceAsStream(String).
createMissingParentDirectories - Whether to create any missing parent directories for the toFileIOExceptionpublic static <EX extends Throwable> void asFile(InputStream inputStream, boolean closeInputStream, @Nullable String prefix, @Nullable String suffix, @Nullable File directory, ConsumerWhichThrows<? super File,? extends EX> delegate) throws IOException, EX extends Throwable
IOExceptionEX extends ThrowableFile.createTempFile(String, String, File)public static <EX extends Throwable> void outputFilePrintWriter(File file, Charset charset, ConsumerWhichThrows<? super PrintWriter,? extends EX> printer) throws IOException, EX extends Throwable
printToFile(file, charset, printer, false).IOExceptionEX extends Throwablepublic static <EX extends Throwable> void outputFilePrintWriter(File file, Charset charset, ConsumerWhichThrows<? super PrintWriter,? extends EX> delegate, boolean createMissingParentDirectories) throws IOException, EX extends Throwable
PrintWriter (effectively a temporary file), and eventually
renames the temporary file to "file" (replacing a possibly existing file).
In case anthing goes wrong, the temporary file is deleted, and a possibly existing "original" file remains unchanged.
delegate - Prints text to the PrintWriter it receivescharset - The charset to be used for printingcreateMissingParentDirectories - Whether to create any missing parent directories for the fileIOException - Creating the temporary file failedIOException - Closing the temporary file failedIOException - Deleting the original file (immediately before renaming the temporary
file) failedIOException - Renaming the temporary file failedEX - The throwable that the delegate may throwEX extends Throwablepublic static <EX extends Throwable> void outputFileOutputStream(File file, ConsumerWhichThrows<? super OutputStream,? extends EX> delegate, boolean createMissingParentDirectories) throws IOException, EX extends Throwable
OutputStream (effectively a temporary file), and eventually
renames the temporary file to "file" (replacing a possibly existing file).
In case anthing goes wrong, the temporary file is deleted, and a possibly existing "original" file remains unchanged.
IOException - Creating the temporary file failedIOException - Closing the temporary file failedIOException - Deleting the original file (immediately before renaming the temporary file) failedIOException - Renaming the temporary file failedEX - The delegate threw an EXEX extends Throwablepublic static <EX extends Throwable> void outputFile(File file, ConsumerWhichThrows<? super File,? extends EX> delegate, boolean createMissingParentDirectories) throws IOException, EX extends Throwable
In case anthing goes wrong, the temporary file is deleted, and a possibly existing "original" file remains unchanged.
IOException - Deleting the original file (immediately before renaming the temporary file) failedIOException - Renaming the temporary file failedEX - The delegate threw an EXEX extends Throwablepublic static void createMissingParentDirectoriesFor(File file) throws IOException
IOException - A directory is missing, but could not be createdIOException - A non-directory (e.g. a "normal" file) is in the waypublic static long copyAvailable(InputStream inputStream, OutputStream outputStream) throws IOException
available on the inputStream.IOExceptionpublic static long copyAvailable(InputStream inputStream, OutputStream outputStream, long n) throws IOException
available on the
inputStream.IOExceptionpublic static InputStream onEndOfInput(InputStream delegate, Runnable runnable)
InputStream that reads from the delegate and invokes the
runnable on the first end-of-input condition.Copyright © 2016 Arno Unkrig. All rights reserved.