public final class IoUtil
extends java.lang.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 java.io.InputStream |
EMPTY_INPUT_STREAM
An
InputStream that produces exactly 0 bytes. |
static java.io.OutputStream |
NULL_OUTPUT_STREAM
An
OutputStream that discards all bytes written to it. |
static java.io.InputStream |
ZERO_INPUT_STREAM
An input stream that reads an endless stream of zeros.
|
| Modifier and Type | Method and Description |
|---|---|
static <EX extends java.lang.Throwable> |
asFile(java.io.InputStream inputStream,
boolean closeInputStream,
java.lang.String prefix,
java.lang.String suffix,
java.io.File directory,
ConsumerWhichThrows<? super java.io.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 java.io.Reader |
asReader(java.lang.CharSequence cs)
Wraps the given
CharSequence in a Reader - much more efficient than "new
StringReader(cs.toString)". |
static java.io.OutputStream |
byteConsumerOutputStream(ConsumerWhichThrows<? super java.lang.Byte,? extends java.io.IOException> delegate) |
static java.io.InputStream |
byteProducerInputStream(Producer<? extends java.lang.Byte> delegate) |
static java.io.InputStream |
byteProducerInputStream(ProducerWhichThrows<? extends java.lang.Byte,? extends java.io.IOException> delegate) |
static java.io.OutputStream[] |
compareOutput(int n,
java.lang.Runnable whenIdentical,
java.lang.Runnable whenNotIdentical)
Creates and returns an array of n
OutputStreams. |
static java.io.InputStream |
constantInputStream(byte b) |
static long |
copy(java.io.File inputFile,
java.io.File outputFile)
Copies the contents of the inputFile to the outputFile.
|
static long |
copy(java.io.File inputFile,
java.io.File outputFile,
IoUtil.CollisionStrategy collisionStrategy)
Copies the contents of the inputFile to the outputFile.
|
static long |
copy(java.io.File inputFile,
java.io.OutputStream outputStream,
boolean closeOutputStream)
Copies the contents of the inputFile to the outputStream.
|
static long |
copy(java.io.InputStream inputStream,
boolean closeInputStream,
java.io.File outputFile)
Copies the contents of the inputStream to the outputFile.
|
static long |
copy(java.io.InputStream inputStream,
boolean closeInputStream,
java.io.File outputFile,
boolean append)
Copies the contents of the inputStream to the outputFile.
|
static long |
copy(java.io.InputStream inputStream,
boolean closeInputStream,
java.io.OutputStream outputStream,
boolean closeOutputStream)
Copies the contents of the inputStream to the outputStream.
|
static long |
copy(java.io.InputStream inputStream,
java.io.OutputStream outputStream)
Reads the input stream until end-of-input and writes all data to the output stream.
|
static long |
copy(java.io.InputStream inputStream,
java.io.OutputStream outputStream,
long n)
Reads at most n bytes from the inputStream and writes all data to the
outputStream.
|
static long |
copy(java.lang.Readable r,
java.lang.Appendable a)
Reads the
Readable until end-of-input and writes all data to the Appendable. |
static long |
copy(java.io.Reader reader,
boolean closeReader,
java.io.File outputFile,
boolean append,
java.nio.charset.Charset outputCharset)
Copies the contents of the reader to the
outputFile, encoded with the given
outputCharset. |
static long |
copy(java.io.Reader reader,
boolean closeReader,
java.io.Writer writer,
boolean closeWriter)
Copies the contents of the reader to the writer.
|
static long |
copy(java.io.Reader reader,
java.io.OutputStream outputStream,
java.nio.charset.Charset charset)
Reads the reader until end-of-input and writes all data to the output stream.
|
static long |
copy(java.io.Reader reader,
java.io.Writer writer)
Reads the reader until end-of-input and writes all data to the writer.
|
static long |
copyAvailable(java.io.InputStream inputStream,
java.io.OutputStream outputStream)
Reads data from the input stream and writes it to the output stream.
|
static long |
copyAvailable(java.io.InputStream inputStream,
java.io.OutputStream outputStream,
long n)
Reads at most n bytes from the input stream and writes all data to the output stream.
|
static ConsumerWhichThrows<java.io.OutputStream,java.io.IOException> |
copyFrom(java.io.InputStream inputStream) |
static void |
copyResource(java.lang.Class<?> clasS,
java.lang.String resourceName,
java.io.File toFile,
boolean createMissingParentDirectories)
Copies the contents a resource to the given toFile.
|
static void |
copyResource(java.lang.Class<?> clasS,
java.lang.String resourceName,
java.io.OutputStream outputStream,
boolean closeOutputStream)
Copies the contents a resource to the given outputStream.
|
static void |
copyResource(java.lang.ClassLoader classLoader,
java.lang.String resourceName,
java.io.File toFile,
boolean createMissingParentDirectories)
Copies the contents a resource to the given toFile.
|
static void |
copyResource(java.lang.ClassLoader classLoader,
java.lang.String resourceName,
java.io.OutputStream outputStream,
boolean closeOutputStream)
Copies the contents of a resource to the given outputStream.
|
static RunnableWhichThrows<java.io.IOException> |
copyRunnable(java.io.InputStream in,
java.io.OutputStream out)
Creates and returns a
RunnableWhichThrows that copies bytes from in to out until
end-of-input. |
static void |
copyTree(java.io.File source,
java.io.File destination,
IoUtil.CollisionStrategy collisionStrategy)
Copies a directory tree.
|
static void |
createMissingParentDirectoriesFor(java.io.File file)
Creates any missing parent directories for the given file.
|
protected static java.io.InputStream |
deleteOnClose(java.io.InputStream delegate,
java.io.File file) |
static void |
fill(java.io.OutputStream outputStream,
byte b,
long count)
Writes count bytes of value b to the given outputStream.
|
static boolean |
isContentIdentical(java.io.File file1,
java.io.File file2) |
static java.io.OutputStream |
lengthWritten(Consumer<? super java.lang.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 java.io.InputStream |
onEndOfInput(java.io.InputStream delegate,
java.lang.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 java.lang.Throwable> |
outputFile(java.io.File file,
ConsumerWhichThrows<? super java.io.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 java.lang.Throwable> |
outputFileOutputStream(java.io.File file,
ConsumerWhichThrows<? super java.io.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 java.lang.Throwable> |
outputFilePrintWriter(java.io.File file,
java.nio.charset.Charset charset,
ConsumerWhichThrows<? super java.io.PrintWriter,? extends EX> printer)
Equivalent with
printToFile(file, charset, printer, false). |
static <EX extends java.lang.Throwable> |
outputFilePrintWriter(java.io.File file,
java.nio.charset.Charset charset,
ConsumerWhichThrows<? super java.io.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,
java.io.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 java.io.InputStream |
randomInputStream(long seed) |
static byte[] |
readAll(java.io.InputStream is) |
static java.lang.String |
readAll(java.io.InputStream inputStream,
java.nio.charset.Charset charset,
boolean closeInputStream) |
static java.lang.String |
readAll(java.io.Reader reader) |
static java.lang.String |
readAll(java.io.Reader reader,
boolean closeReader) |
static long |
skip(java.io.InputStream inputStream,
long n)
Skips n bytes on the inputStream.
|
static long |
skipAll(java.io.InputStream inputStream)
Skips all remaining data on the inputStream.
|
static java.io.OutputStream |
split(ProducerWhichThrows<? extends java.io.OutputStream,? extends java.io.IOException> delegates,
Producer<? extends java.lang.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 java.io.OutputStream |
tee(java.io.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 java.io.InputStream |
unclosableInputStream(java.io.InputStream delegate) |
static java.io.OutputStream |
unclosableOutputStream(java.io.OutputStream delegate) |
static long |
writeAndCount(ConsumerWhichThrows<? super java.io.OutputStream,? extends java.io.IOException> writeContents,
java.io.OutputStream outputStream)
Invokes writeContents
.consume() with an output stream subject that writes the data through to
the given outputStream. |
static java.io.InputStream |
wye(java.io.InputStream in,
java.io.OutputStream out)
Creates and returns a
FilterInputStream that duplicates all bytes read through it and writes them to
an OutputStream. |
public static final java.io.InputStream EMPTY_INPUT_STREAM
InputStream that produces exactly 0 bytes.public static final java.io.OutputStream NULL_OUTPUT_STREAM
OutputStream that discards all bytes written to it.public static final java.io.InputStream ZERO_INPUT_STREAM
public static long copy(java.io.InputStream inputStream,
java.io.OutputStream outputStream)
throws java.io.IOException
java.io.IOExceptionpublic static long copy(java.io.InputStream inputStream,
java.io.OutputStream outputStream,
long n)
throws java.io.IOException
java.io.IOExceptionpublic static long copy(java.io.InputStream inputStream,
boolean closeInputStream,
java.io.OutputStream outputStream,
boolean closeOutputStream)
throws java.io.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)java.io.IOExceptionpublic static RunnableWhichThrows<java.io.IOException> copyRunnable(java.io.InputStream in, java.io.OutputStream out)
RunnableWhichThrows that copies bytes from in to out until
end-of-input.public static long copy(java.io.Reader reader,
java.io.Writer writer)
throws java.io.IOException
java.io.IOExceptionpublic static long copy(java.io.Reader reader,
boolean closeReader,
java.io.Writer writer,
boolean closeWriter)
throws java.io.IOException
java.io.IOExceptionpublic static long copy(java.io.Reader reader,
java.io.OutputStream outputStream,
java.nio.charset.Charset charset)
throws java.io.IOException
java.io.IOExceptionpublic static long copy(java.lang.Readable r,
java.lang.Appendable a)
throws java.io.IOException
Readable until end-of-input and writes all data to the Appendable.java.io.IOExceptionpublic static long copy(java.io.InputStream inputStream,
boolean closeInputStream,
java.io.File outputFile,
boolean append)
throws java.io.IOException
java.io.IOExceptionpublic static long copy(java.io.Reader reader,
boolean closeReader,
java.io.File outputFile,
boolean append,
java.nio.charset.Charset outputCharset)
throws java.io.IOException
outputFile, encoded with the given
outputCharset. Attempts to delete a partially written output file if the operation fails.java.io.IOExceptionpublic static long copy(java.io.File inputFile,
java.io.OutputStream outputStream,
boolean closeOutputStream)
throws java.io.IOException
closeOutputStream - Whether to close the outputStream when execution completesjava.io.IOExceptionpublic static long copy(java.io.InputStream inputStream,
boolean closeInputStream,
java.io.File outputFile)
throws java.io.IOException
The parent directory of the outputFile must already exist.
java.io.IOExceptionpublic static long copy(java.io.File inputFile,
java.io.File outputFile)
throws java.io.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.
java.io.IOExceptionpublic static long copy(java.io.File inputFile,
java.io.File outputFile,
IoUtil.CollisionStrategy collisionStrategy)
throws java.io.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_DIFFERENTjava.io.IOExceptionIoUtil.CollisionStrategypublic static void copyTree(java.io.File source,
java.io.File destination,
IoUtil.CollisionStrategy collisionStrategy)
throws java.io.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 destinationy must already exist.
java.io.IOExceptionpublic static ConsumerWhichThrows<java.io.OutputStream,java.io.IOException> copyFrom(java.io.InputStream inputStream)
Comsumer<OutputStream> which copies inputStream to its subjectpublic static boolean isContentIdentical(java.io.File file1,
java.io.File file2)
throws java.io.IOException
java.io.IOExceptionpublic static java.io.OutputStream tee(java.io.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 java.io.InputStream wye(java.io.InputStream in,
java.io.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 java.io.OutputStream,? extends java.io.IOException> writeContents, java.io.OutputStream outputStream) throws java.io.IOException
.consume() with an output stream subject that writes the data through to
the given outputStream.java.io.IOExceptionpublic static void parallel(IoUtil.WritingRunnable[] writingRunnables, java.io.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(java.io.InputStream is)
throws java.io.IOException
InputStream producesjava.io.IOExceptionpublic static java.lang.String readAll(java.io.InputStream inputStream,
java.nio.charset.Charset charset,
boolean closeInputStream)
throws java.io.IOException
InputStream produces, decoded into a stringjava.io.IOExceptionpublic static long skip(java.io.InputStream inputStream,
long n)
throws java.io.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.java.io.IOExceptionInputStream.skip(long)public static long skipAll(java.io.InputStream inputStream)
throws java.io.IOException
java.io.IOExceptionInputStream.skip(long)public static java.io.OutputStream split(ProducerWhichThrows<? extends java.io.OutputStream,? extends java.io.IOException> delegates, Producer<? extends java.lang.Long> byteCountLimits) throws java.io.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 Longsjava.io.IOExceptionpublic static java.io.InputStream constantInputStream(byte b)
public static java.io.InputStream unclosableInputStream(java.io.InputStream delegate)
InputStream which ignores all invocations of InputStream.close()public static java.io.OutputStream unclosableOutputStream(java.io.OutputStream delegate)
OutputStream which ignores all invocations of OutputStream.close()public static void fill(java.io.OutputStream outputStream,
byte b,
long count)
throws java.io.IOException
java.io.IOExceptionpublic static java.io.InputStream byteProducerInputStream(ProducerWhichThrows<? extends java.lang.Byte,? extends java.io.IOException> delegate)
null
products are returned as 'end-of-input'public static java.io.InputStream byteProducerInputStream(Producer<? extends java.lang.Byte> delegate)
null
products are returned as 'end-of-input'public static java.io.InputStream randomInputStream(long seed)
public static java.io.OutputStream byteConsumerOutputStream(ConsumerWhichThrows<? super java.lang.Byte,? extends java.io.IOException> delegate)
public static java.lang.String readAll(java.io.Reader reader)
throws java.io.IOException
Reader producesjava.io.IOExceptionpublic static java.lang.String readAll(java.io.Reader reader,
boolean closeReader)
throws java.io.IOException
closeReader - Whether the reader should be closed before the method returnsReader producesjava.io.IOExceptionprotected static java.io.InputStream deleteOnClose(java.io.InputStream delegate,
java.io.File file)
InputStream which first closes the delegate, and then attempts to delete the
filepublic static java.io.OutputStream[] compareOutput(int n,
java.lang.Runnable whenIdentical,
java.lang.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 java.io.OutputStream lengthWritten(Consumer<? super java.lang.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 java.io.Reader asReader(java.lang.CharSequence cs)
CharSequence in a Reader - much more efficient than "new
StringReader(cs.toString)".public static void copyResource(java.lang.ClassLoader classLoader,
java.lang.String resourceName,
java.io.OutputStream outputStream,
boolean closeOutputStream)
throws java.io.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 copiedjava.io.IOExceptionpublic static void copyResource(java.lang.Class<?> clasS,
java.lang.String resourceName,
java.io.OutputStream outputStream,
boolean closeOutputStream)
throws java.io.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 copiedjava.io.IOExceptionpublic static void copyResource(java.lang.ClassLoader classLoader,
java.lang.String resourceName,
java.io.File toFile,
boolean createMissingParentDirectories)
throws java.io.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 toFilejava.io.IOExceptionpublic static void copyResource(java.lang.Class<?> clasS,
java.lang.String resourceName,
java.io.File toFile,
boolean createMissingParentDirectories)
throws java.io.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 toFilejava.io.IOExceptionpublic static <EX extends java.lang.Throwable> void asFile(java.io.InputStream inputStream,
boolean closeInputStream,
@Nullable
java.lang.String prefix,
@Nullable
java.lang.String suffix,
@Nullable
java.io.File directory,
ConsumerWhichThrows<? super java.io.File,? extends EX> delegate)
throws java.io.IOException,
EX extends java.lang.Throwable
java.io.IOExceptionEX extends java.lang.ThrowableFile.createTempFile(String, String, File)public static <EX extends java.lang.Throwable> void outputFilePrintWriter(java.io.File file,
java.nio.charset.Charset charset,
ConsumerWhichThrows<? super java.io.PrintWriter,? extends EX> printer)
throws java.io.IOException,
EX extends java.lang.Throwable
printToFile(file, charset, printer, false).java.io.IOExceptionEX extends java.lang.Throwablepublic static <EX extends java.lang.Throwable> void outputFilePrintWriter(java.io.File file,
java.nio.charset.Charset charset,
ConsumerWhichThrows<? super java.io.PrintWriter,? extends EX> delegate,
boolean createMissingParentDirectories)
throws java.io.IOException,
EX extends java.lang.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 filejava.io.IOException - Creating the temporary file failedjava.io.IOException - Closing the temporary file failedjava.io.IOException - Deleting the original file (immediately before renaming the temporary
file) failedjava.io.IOException - Renaming the temporary file failedEX - The throwable that the delegate may throwEX extends java.lang.Throwablepublic static <EX extends java.lang.Throwable> void outputFileOutputStream(java.io.File file,
ConsumerWhichThrows<? super java.io.OutputStream,? extends EX> delegate,
boolean createMissingParentDirectories)
throws java.io.IOException,
EX extends java.lang.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.
java.io.IOException - Creating the temporary file failedjava.io.IOException - Closing the temporary file failedjava.io.IOException - Deleting the original file (immediately before renaming the temporary file) failedjava.io.IOException - Renaming the temporary file failedEX - The delegate threw an EXEX extends java.lang.Throwablepublic static <EX extends java.lang.Throwable> void outputFile(java.io.File file,
ConsumerWhichThrows<? super java.io.File,? extends EX> delegate,
boolean createMissingParentDirectories)
throws java.io.IOException,
EX extends java.lang.Throwable
In case anthing goes wrong, the temporary file is deleted, and a possibly existing "original" file remains unchanged.
java.io.IOException - Deleting the original file (immediately before renaming the temporary file) failedjava.io.IOException - Renaming the temporary file failedEX - The delegate threw an EXEX extends java.lang.Throwablepublic static void createMissingParentDirectoriesFor(java.io.File file)
throws java.io.IOException
java.io.IOException - A directory is missing, but could not be createdjava.io.IOException - A non-directory (e.g. a "normal" file) is in the waypublic static long copyAvailable(java.io.InputStream inputStream,
java.io.OutputStream outputStream)
throws java.io.IOException
available on the inputStream.java.io.IOExceptionpublic static long copyAvailable(java.io.InputStream inputStream,
java.io.OutputStream outputStream,
long n)
throws java.io.IOException
available on the
inputStream.java.io.IOExceptionpublic static java.io.InputStream onEndOfInput(java.io.InputStream delegate,
java.lang.Runnable runnable)
InputStream that reads from the delegate and invokes the
runnable on the first end-of-input condition.