public final class IoUtil
extends java.lang.Object
java.io-related utility methods.| Modifier and Type | Class and Description |
|---|---|
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.Exception> |
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 outFile)
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 input stream and writes all data to the output stream. |
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 is) |
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)
|
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 output stream. |
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 <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 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 os)
Invokes
writeContents.consume() with an output stream subject that writes the data through to the
given os. |
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
n bytes from the input stream and writes all data to the output stream. Closes none of
the two streams.java.io.IOExceptionpublic static long copy(java.io.InputStream inputStream,
boolean closeInputStream,
java.io.OutputStream outputStream,
boolean closeOutputStream)
throws java.io.IOException
inputStream to the outputStream.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)
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
reader to the writer.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
inputStream to the outputFile. Attempts to delete a partially written
output file if the operation fails.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
reader to the 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
inputFile to the outputStream.java.io.IOExceptionpublic static long copy(java.io.InputStream inputStream,
boolean closeInputStream,
java.io.File outputFile)
throws java.io.IOException
inputStream to the outputFile.java.io.IOExceptionpublic static long copy(java.io.File inputFile,
java.io.File outFile)
throws java.io.IOException
inputFile to the outputFile.java.io.IOExceptionpublic static ConsumerWhichThrows<java.io.OutputStream,java.io.IOException> copyFrom(java.io.InputStream is)
Comsumer<OutputStream> which copies is to its subjectpublic 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 os) throws java.io.IOException
writeContents.consume() with an output stream subject that writes the data through to the
given os.java.io.IOExceptionpublic static void parallel(IoUtil.WritingRunnable[] writingRunnables, java.io.Writer writer)
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.
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 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)
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
count bytes of value b to the given output stream.java.io.IOExceptionpublic static java.io.InputStream byteProducerInputStream(ProducerWhichThrows<? extends java.lang.Byte,? extends java.io.IOException> delegate)
delegate byte producer; null
products are returned as 'end-of-input'public static java.io.InputStream byteProducerInputStream(Producer<? extends java.lang.Byte> delegate)
delegate byte producer; null
products are returned as 'end-of-input'public static java.io.InputStream randomInputStream(long seed)
delegate byte producer; null
products are returned as 'end-of-input'public static java.io.OutputStream byteConsumerOutputStream(ConsumerWhichThrows<? super java.lang.Byte,? extends java.io.IOException> delegate)
delegate byte consumerpublic 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)
n 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.Exception> 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.Exception
inputStream into it, closes the
file, invokes the delegate with the file, and eventually deletes the file.java.io.IOExceptionEX extends java.lang.ExceptionFile.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
n bytes from the input stream and writes all data to the output stream. Closes none of
the two streams. Returns when no data is available on the
inputStream.java.io.IOException