public class TeeOutputStream extends OutputStream
Copies the data that is written to this class to the array of
OutputStream
passed in the constructor, allowing to write to
multiple OutputStream
at once. It also collect statistics on the
operations done (time spent writing to the internal streams, amount of data
written).
Usage:
InputStream source=... //some data to be read. ByteArrayOutputStream destination1= new ByteArrayOutputStream(); ByteArrayOutputStream destination2= new ByteArrayOutputStream(); TeeOutputStream tee = new TeeOutputStream(destination1,destination2); org.apache.commons.io.IOUtils.copy(source,tee); tee.close(); //at this point both destination1 and destination2 contains the same bytes.
Modifier and Type | Field and Description |
---|---|
protected boolean |
closeCalled
True when close() is invoked. |
protected OutputStream[] |
destinations
The destination
OutputStream(s) where data is written. |
Constructor and Description |
---|
TeeOutputStream(OutputStream... destinations)
Creates a
TeeOutputStream and saves its arguments, the
destinations for later use. |
Modifier and Type | Method and Description |
---|---|
void |
close() |
void |
enableCopy(boolean enable)
Allow to switch off the copy to the underlying streams.
|
void |
enableCopy(boolean[] enable)
Allow to switch off the copy to the underlying streams, selectively
enabling or disabling copy on some specific stream.
|
void |
flush() |
OutputStream[] |
getDestinationStreams()
Returns the
OutputStream (s) passed in the constructor. |
long |
getSize()
This method returns the size in bytes of the data written to this
OutputStream.
|
long[] |
getWriteTime()
Return the time spent writing to the destination
OutputStream(s) in milliseconds. |
void |
write(byte[] b) |
void |
write(byte[] b,
int off,
int len) |
void |
write(int b) |
protected boolean closeCalled
protected final OutputStream[] destinations
OutputStream(s)
where data is written.public TeeOutputStream(OutputStream... destinations)
Creates a TeeOutputStream
and saves its arguments, the
destinations
for later use.
This constructor allow to specify multiple OutputStream
to
which the data will be copied.
destinations
- Data written to this OutputStream
are copied to
all the destinations
.public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
close
in class OutputStream
IOException
public final void enableCopy(boolean enable)
Allow to switch off the copy to the underlying streams. The copy is enabled by default. Setting the parameter to false will disable the copy to all the underlying streams at once.
If you need more fine grained control you should use
enableCopy(boolean[])
.
enable
- whether to copy or not the bytes to the underlying stream.public final void enableCopy(boolean[] enable)
Allow to switch off the copy to the underlying streams, selectively enabling or disabling copy on some specific stream.
The copy is enabled by default. Each element in the array correspond to
an OutputStream
passed in the constructor. If the
correspondent element in the array passed as a parameter is set to
true
the copy will be enabled.It can be invoked multiple
times.
enable
- whether to copy or not the bytes to the underlying stream.public void flush() throws IOException
flush
in interface Flushable
flush
in class OutputStream
IOException
public final OutputStream[] getDestinationStreams()
Returns the OutputStream
(s) passed in the constructor.
public final long getSize()
This method returns the size in bytes of the data written to this OutputStream. It can be used to collect statistics on the write operations.
OutputStreams
.public long[] getWriteTime()
Return the time spent writing to the destination
OutputStream(s)
in milliseconds.
The returned array has one element for each OutputStream
passed in the constructor.
OutputStreams
.public void write(byte[] b) throws IOException
write
in class OutputStream
IOException
public void write(byte[] b, int off, int len) throws IOException
write
in class OutputStream
IOException
public void write(int b) throws IOException
write
in class OutputStream
IOException
Copyright © 2008–2016. All rights reserved.