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
Deprecated.
Use
InputStreams.EMPTY instead |
static OutputStream |
NULL_OUTPUT_STREAM
Deprecated.
Use
OutputStreams.DISCARD instead |
static InputStream |
ZERO_INPUT_STREAM
Deprecated.
Use
InputStreams.ZERO instead |
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)
Deprecated.
Use
Readers.asReader(CharSequence) instead |
static OutputStream |
byteConsumerOutputStream(ConsumerWhichThrows<? super Byte,? extends IOException> delegate)
Deprecated.
|
static InputStream |
byteProducerInputStream(Producer<? extends Byte> delegate)
Deprecated.
Use
InputStreams.byteProducerInputStream(Producer) instead |
static InputStream |
byteProducerInputStream(ProducerWhichThrows<? extends Byte,? extends IOException> delegate)
Deprecated.
|
static OutputStream[] |
compareOutput(int n,
Runnable whenIdentical,
Runnable whenNotIdentical)
Deprecated.
|
static InputStream |
constantInputStream(byte b)
Deprecated.
Use
InputStreams.constantInputStream(byte) instead |
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 <EX extends Throwable> |
copyAndTransform(Reader in,
TransformerWhichThrows<? super CharSequence,? extends CharSequence,? extends EX> transformer,
Appendable out,
int bufferCapacity)
Reads chunks from in, repeatedly, (until end-of-input), transforms each chunk through the
transformer, and writes the results to out.
|
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)
Deprecated.
Use
InputStreams.deleteOnClose(InputStream, File) instead |
static void |
fill(OutputStream outputStream,
byte b,
long count)
Deprecated.
Use
OutputStreams.fill(OutputStream, byte, long) instead |
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)
Deprecated.
Use
OutputStreams.lengthWritten(Consumer) instead |
static InputStream |
onEndOfInput(InputStream delegate,
Runnable runnable)
Deprecated.
Use
InputStreams.onEndOfInput(InputStream, Runnable) instead |
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)
Deprecated.
Use
InputStreams.randomInputStream(long) instead |
static byte[] |
readAll(InputStream is)
Deprecated.
Use
InputStreams.readAll(InputStream) instead |
static String |
readAll(InputStream inputStream,
Charset charset,
boolean closeInputStream)
Deprecated.
|
static String |
readAll(Reader reader)
Deprecated.
Use
Readers.readAll(Reader) instead |
static String |
readAll(Reader reader,
boolean closeReader)
Deprecated.
Use
Readers.readAll(Reader, boolean) instead |
static InputStream |
singlingFilterInputStream(InputStream delegate)
Deprecated.
|
static Reader |
singlingFilterReader(Reader delegate)
Deprecated.
Use
Readers.singlingFilterReader(Reader) instead |
static long |
skip(InputStream inputStream,
long n)
Deprecated.
Use
InputStreams.skip(InputStream, long) instead |
static long |
skipAll(InputStream inputStream)
Deprecated.
Use
InputStreams.skipAll(InputStream) instead |
static OutputStream |
split(ProducerWhichThrows<? extends OutputStream,? extends IOException> delegates,
Producer<? extends Long> byteCountLimits)
Deprecated.
|
static OutputStream |
tee(OutputStream... delegates)
Deprecated.
Use
OutputStreams.tee(OutputStream...) instead |
static InputStream |
unclosableInputStream(InputStream delegate)
Deprecated.
Use
InputStreams.unclosable(InputStream) instead |
static OutputStream |
unclosableOutputStream(OutputStream delegate)
Deprecated.
Use
OutputStreams.unclosable(OutputStream) instead |
static long |
writeAndCount(ConsumerWhichThrows<? super OutputStream,? extends IOException> writeContents,
OutputStream outputStream)
Deprecated.
|
static InputStream |
wye(InputStream in,
OutputStream out)
Deprecated.
Use
InputStreams.wye(InputStream, OutputStream) instead |
@Deprecated public static final InputStream EMPTY_INPUT_STREAM
InputStreams.EMPTY
instead@Deprecated public static final OutputStream NULL_OUTPUT_STREAM
OutputStreams.DISCARD
instead@Deprecated public static final InputStream ZERO_INPUT_STREAM
InputStreams.ZERO
instead@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) fileresourceName
- Must not start with a "/"null
if path == null
, or if the resource could not be foundIOException
public static long copy(InputStream inputStream, OutputStream outputStream) throws IOException
IOException
public static long copy(InputStream inputStream, OutputStream outputStream, long n) throws IOException
IOException
public 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)IOException
public 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
IOException
public static long copy(Reader reader, boolean closeReader, Writer writer, boolean closeWriter) throws IOException
IOException
public static long copy(Reader reader, OutputStream outputStream, Charset charset) throws IOException
IOException
public static long copy(Readable r, Appendable a) throws IOException
Readable
until end-of-input and writes all data to the Appendable
.IOException
public static long copy(InputStream inputStream, boolean closeInputStream, File outputFile, boolean append) throws IOException
IOException
public static long copy(Reader reader, boolean closeReader, File outputFile, boolean append, Charset outputCharset) throws IOException
IOException
public static <EX extends Throwable> void copyAndTransform(Reader in, TransformerWhichThrows<? super CharSequence,? extends CharSequence,? extends EX> transformer, Appendable out, int bufferCapacity) throws IOException, EX extends Throwable
bufferCapacity
- The number of chars that are read per chunkIOException
EX extends Throwable
public static long copy(File inputFile, OutputStream outputStream, boolean closeOutputStream) throws IOException
closeOutputStream
- Whether to close the outputStream when execution completesIOException
public static long copy(InputStream inputStream, boolean closeInputStream, File outputFile) throws IOException
The parent directory of the outputFile must already exist.
IOException
public static long copy(InputStream inputStream, boolean closeInputStream, File outputFile, IoUtil.CollisionStrategy collisionStrategy) throws IOException
The parent directory of the outputFile must already exist.
IOException
public 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.
IOException
public 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_DIFFERENT
IOException
IoUtil.CollisionStrategy
public 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.
IOException
public 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.
IOException
public static ConsumerWhichThrows<OutputStream,IOException> copyFrom(InputStream inputStream)
Consumer<OutputStream>
which copies inputStream to its subjectpublic static boolean isContentIdentical(File file1, File file2) throws IOException
IOException
@Deprecated public static OutputStream tee(OutputStream... delegates)
OutputStreams.tee(OutputStream...)
instead@Deprecated public static InputStream wye(InputStream in, OutputStream out)
InputStreams.wye(InputStream, OutputStream)
instead@Deprecated public static long writeAndCount(ConsumerWhichThrows<? super OutputStream,? extends IOException> writeContents, OutputStream outputStream) throws IOException
OutputStreams.writeAndCount(ConsumerWhichThrows, OutputStream)
insteadIOException
public static void parallel(IoUtil.WritingRunnable[] writingRunnables, Writer writer)
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.
@Deprecated public static byte[] readAll(InputStream is) throws IOException
InputStreams.readAll(InputStream)
insteadIOException
@Deprecated public static String readAll(InputStream inputStream, Charset charset, boolean closeInputStream) throws IOException
InputStreams.readAll(InputStream, Charset, boolean)
insteadIOException
@Deprecated public static long skip(InputStream inputStream, long n) throws IOException
InputStreams.skip(InputStream, long)
insteadIOException
@Deprecated public static long skipAll(InputStream inputStream) throws IOException
InputStreams.skipAll(InputStream)
insteadIOException
@Deprecated public static OutputStream split(ProducerWhichThrows<? extends OutputStream,? extends IOException> delegates, Producer<? extends Long> byteCountLimits) throws IOException
OutputStreams.split(ProducerWhichThrows, Producer)
insteadIOException
@Deprecated public static InputStream constantInputStream(byte b)
InputStreams.constantInputStream(byte)
instead@Deprecated public static InputStream unclosableInputStream(InputStream delegate)
InputStreams.unclosable(InputStream)
instead@Deprecated public static OutputStream unclosableOutputStream(OutputStream delegate)
OutputStreams.unclosable(OutputStream)
instead@Deprecated public static void fill(OutputStream outputStream, byte b, long count) throws IOException
OutputStreams.fill(OutputStream, byte, long)
insteadIOException
@Deprecated public static InputStream byteProducerInputStream(ProducerWhichThrows<? extends Byte,? extends IOException> delegate)
InputStreams.byteProducerInputStream(ProducerWhichThrows)
instead@Deprecated public static InputStream byteProducerInputStream(Producer<? extends Byte> delegate)
InputStreams.byteProducerInputStream(Producer)
instead@Deprecated public static InputStream randomInputStream(long seed)
InputStreams.randomInputStream(long)
instead@Deprecated public static OutputStream byteConsumerOutputStream(ConsumerWhichThrows<? super Byte,? extends IOException> delegate)
OutputStreams.byteConsumerOutputStream(ConsumerWhichThrows)
instead@Deprecated public static String readAll(Reader reader) throws IOException
Readers.readAll(Reader)
insteadIOException
@Deprecated public static String readAll(Reader reader, boolean closeReader) throws IOException
Readers.readAll(Reader, boolean)
insteadIOException
@Deprecated protected static InputStream deleteOnClose(InputStream delegate, File file)
InputStreams.deleteOnClose(InputStream, File)
instead@Deprecated public static OutputStream[] compareOutput(int n, Runnable whenIdentical, Runnable whenNotIdentical)
OutputStreams.compareOutput(int, Runnable, Runnable)
instead@Deprecated public static OutputStream lengthWritten(Consumer<? super Integer> delegate)
OutputStreams.lengthWritten(Consumer)
instead@Deprecated public static Reader asReader(CharSequence cs)
Readers.asReader(CharSequence)
insteadpublic 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)
.
resourceName
- Must not start with a "/"closeOutputStream
- Whether the outputStream should be closed after the content of the resource
has been copiedIOException
public 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)
.
resourceName
- Must not start with a "/"closeOutputStream
- Whether the outputStream should be closed after the content of the resource
has been copiedIOException
public 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)
.
resourceName
- Must not start with a "/"createMissingParentDirectories
- Whether to create any missing parent directories for the toFileIOException
public 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)
.
resourceName
- Must not start with a "/"createMissingParentDirectories
- Whether to create any missing parent directories for the toFileIOException
public 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
IOException
EX extends Throwable
File.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)
.IOException
EX extends Throwable
public 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 Throwable
public 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 Throwable
public 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 Throwable
public 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.IOException
public static long copyAvailable(InputStream inputStream, OutputStream outputStream, long n) throws IOException
available
on the
inputStream.IOException
@Deprecated public static InputStream onEndOfInput(InputStream delegate, Runnable runnable)
InputStreams.onEndOfInput(InputStream, Runnable)
instead@Deprecated public static Reader singlingFilterReader(Reader delegate)
Readers.singlingFilterReader(Reader)
instead@Deprecated public static InputStream singlingFilterInputStream(InputStream delegate)
InputStreams.singlingFilterInputStream(InputStream)
insteadCopyright © 2018 Arno Unkrig. All rights reserved.