de.unkrig.commons.io
Class IoUtil

java.lang.Object
  extended by de.unkrig.commons.io.IoUtil

public final class IoUtil
extends java.lang.Object

Various java.io-related utility methods.


Nested Class Summary
static interface IoUtil.WritingRunnable
          An entity which writes characters to a Writer.
 
Field Summary
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.
 
Method Summary
static java.io.OutputStream byteConsumerOutputStream(ConsumerWhichThrows<java.lang.Byte,java.io.IOException> delegate)
           
static java.io.InputStream byteProducerInputStream(Producer<java.lang.Byte> delegate)
           
static java.io.InputStream byteProducerInputStream(ProducerWhichThrows<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 ConsumerWhichThrows<java.io.OutputStream,java.io.IOException> copyFrom(java.io.InputStream is)
           
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.
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 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 r)
           
static java.io.OutputStream split(ProducerWhichThrows<java.io.OutputStream,java.io.IOException> delegates, Producer<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 long writeAndCount(ConsumerWhichThrows<java.io.OutputStream,java.io.IOException> writeContents, java.io.OutputStream os)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EMPTY_INPUT_STREAM

public static final java.io.InputStream EMPTY_INPUT_STREAM
An InputStream that produces exactly 0 bytes.


NULL_OUTPUT_STREAM

public static final java.io.OutputStream NULL_OUTPUT_STREAM
An OutputStream that discards all bytes written to it.


ZERO_INPUT_STREAM

public static final java.io.InputStream ZERO_INPUT_STREAM
An input stream that reads an endless stream of zeros.

Method Detail

copy

public static long copy(java.io.InputStream inputStream,
                        java.io.OutputStream outputStream)
                 throws java.io.IOException
Reads the input stream until end-of-input and writes all data to the output stream. Closes neither of the two streams.

Returns:
The number of bytes copied
Throws:
java.io.IOException

copy

public static long copy(java.io.InputStream inputStream,
                        java.io.OutputStream outputStream,
                        long n)
                 throws java.io.IOException
Reads at most n bytes from the input stream and writes all data to the output stream. Closes neither of the two streams.

Returns:
The number of bytes copied
Throws:
java.io.IOException

copy

public static long copy(java.io.InputStream inputStream,
                        boolean closeInputStream,
                        java.io.OutputStream outputStream,
                        boolean closeOutputStream)
                 throws java.io.IOException
Copies the contents of the inputStream to the outputStream.

Parameters:
closeInputStream - Whether to close the inputStream (also if an IOException is thrown)
closeOutputStream - Whether to close the outputStream (also if an IOException is thrown)
Returns:
The number of bytes copied
Throws:
java.io.IOException

copyRunnable

public 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.


copy

public static long copy(java.io.Reader reader,
                        java.io.Writer writer)
                 throws java.io.IOException
Reads the reader until end-of-input and writes all data to the writer. Closes neither the reader nor the writer.

Returns:
The number of characters copied
Throws:
java.io.IOException

copy

public static long copy(java.io.Reader reader,
                        boolean closeReader,
                        java.io.Writer writer,
                        boolean closeWriter)
                 throws java.io.IOException
Copies the contents of the reader to the writer.

Returns:
The number of characters copied
Throws:
java.io.IOException

copy

public static long copy(java.io.Reader reader,
                        java.io.OutputStream outputStream,
                        java.nio.charset.Charset charset)
                 throws java.io.IOException
Reads the reader until end-of-input and writes all data to the output stream. Closes neither the reader nor the output stream.

Returns:
The number of characters copied
Throws:
java.io.IOException

copy

public static long copy(java.lang.Readable r,
                        java.lang.Appendable a)
                 throws java.io.IOException
Reads the Readable until end-of-input and writes all data to the Appendable.

Returns:
The number of characters copied
Throws:
java.io.IOException

copy

public static long copy(java.io.InputStream inputStream,
                        boolean closeInputStream,
                        java.io.File outputFile,
                        boolean append)
                 throws java.io.IOException
Copies the contents of the inputStream to the outputFile. Attempts to delete a partially written output file if the operation fails.

Returns:
The number of bytes copied
Throws:
java.io.IOException

copy

public static long copy(java.io.Reader reader,
                        boolean closeReader,
                        java.io.File outputFile,
                        boolean append,
                        java.nio.charset.Charset outputCharset)
                 throws java.io.IOException
Copies the contents of the reader to the outputFile, encoded with the given outputCharset. Attempts to delete a partially written output file if the operation fails.

Returns:
The number of characters copied
Throws:
java.io.IOException

copy

public static long copy(java.io.File inputFile,
                        java.io.OutputStream outputStream,
                        boolean closeOutputStream)
                 throws java.io.IOException
Copies the contents of the inputFile to the outputStream.

Returns:
The number of bytes copied
Throws:
java.io.IOException

copy

public static long copy(java.io.InputStream inputStream,
                        boolean closeInputStream,
                        java.io.File outputFile)
                 throws java.io.IOException
Copies the contents of the inputStream to the outputFile.

Returns:
The number of bytes copied
Throws:
java.io.IOException

copy

public static long copy(java.io.File inputFile,
                        java.io.File outFile)
                 throws java.io.IOException
Copies the contents of the inputFile to the outputFile.

Returns:
The number of bytes copied
Throws:
java.io.IOException

copyFrom

public static ConsumerWhichThrows<java.io.OutputStream,java.io.IOException> copyFrom(java.io.InputStream is)
Returns:
A Comsumer<OutputStream> which copies is to its subject

tee

public static java.io.OutputStream tee(java.io.OutputStream... delegates)
Creates and returns an OutputStream that delegates all work to the given delegates:


writeAndCount

public static long writeAndCount(ConsumerWhichThrows<java.io.OutputStream,java.io.IOException> writeContents,
                                 java.io.OutputStream os)
                          throws java.io.IOException
Returns:
The number of bytes that writeContents had written to its subject
Throws:
java.io.IOException

parallel

public 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.

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.


readAll

public static byte[] readAll(java.io.InputStream is)
                      throws java.io.IOException
Returns:
All bytes that the given InputStream produces
Throws:
java.io.IOException

readAll

public static java.lang.String readAll(java.io.InputStream inputStream,
                                       java.nio.charset.Charset charset,
                                       boolean closeInputStream)
                                throws java.io.IOException
Returns:
All bytes that the given InputStream produces, decoded into a string
Throws:
java.io.IOException

split

public static java.io.OutputStream split(ProducerWhichThrows<java.io.OutputStream,java.io.IOException> delegates,
                                         Producer<java.lang.Long> byteCountLimits)
                                  throws java.io.IOException
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.

Parameters:
delegates - Must produce a non-null series of OutputStreams
byteCountLimits - Must produce a non-null series of Longs
Throws:
java.io.IOException

constantInputStream

public static java.io.InputStream constantInputStream(byte b)
Returns:
An input stream that reads an endless stream bytes of value b.

fill

public static void fill(java.io.OutputStream outputStream,
                        byte b,
                        long count)
                 throws java.io.IOException
Writes count bytes of value b to the given output stream.

Throws:
java.io.IOException

byteProducerInputStream

public static java.io.InputStream byteProducerInputStream(ProducerWhichThrows<java.lang.Byte,? extends java.io.IOException> delegate)
Returns:
An input stream which reads the data produced by the delegate byte producer; null products are returned as 'end-of-input'

byteProducerInputStream

public static java.io.InputStream byteProducerInputStream(Producer<java.lang.Byte> delegate)
Returns:
An input stream which reads the data produced by the delegate byte producer; null products are returned as 'end-of-input'

randomInputStream

public static java.io.InputStream randomInputStream(long seed)
Returns:
An input stream which reads the data produced by the delegate byte producer; null products are returned as 'end-of-input'

byteConsumerOutputStream

public static java.io.OutputStream byteConsumerOutputStream(ConsumerWhichThrows<java.lang.Byte,java.io.IOException> delegate)
Returns:
An output stream which feeds the data to the delegate byte consumer

readAll

public static java.lang.String readAll(java.io.Reader r)
                                throws java.io.IOException
Returns:
All characters that the given Reader produces
Throws:
java.io.IOException

deleteOnClose

protected static java.io.InputStream deleteOnClose(java.io.InputStream delegate,
                                                   java.io.File file)
Returns:
An InputStream which first closes the delegate, and then attempts to delete the file

compareOutput

public static java.io.OutputStream[] compareOutput(int n,
                                                   java.lang.Runnable whenIdentical,
                                                   java.lang.Runnable whenNotIdentical)
Creates and returns an array of 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).