Top Description Inners Fields Constructors Methods
org.apache.tools.ant.taskdefs

public Class StreamPumper

extends Object
implements Runnable
Class Inheritance
All Implemented Interfaces
java.lang.Runnable
Imports
org.apache.tools.ant.util.FileUtils, java.io.IOException, .InputStream, .OutputStream, java.util.concurrent.CountDownLatch, .TimeUnit

Copies all data from an input stream to an output stream.
Since
Ant 1.2

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv class
StreamPumper.PostStopHandle

A handle that can be used after stop() has been invoked to check if the StreamPumper is in the process of do some post-stop tasks (like flushing of streams), before finishing.

Field Summary

Modifier and TypeField and Description
private volatile boolean
private boolean
private int
private final boolean
private Exception
private volatile boolean
private final InputStream
private final OutputStream
private static final long
private StreamPumper.PostStopHandle
private static final int
private boolean
private final boolean

Constructor Summary

AccessConstructor and Description
public
StreamPumper(InputStream
input stream to read data from
is
,
OutputStream
output stream to write data to.
os
,
boolean
if true, the output stream will be closed when the input is exhausted.
closeWhenExhausted
)

Create a new StreamPumper.

public
StreamPumper(InputStream
input stream to read data from
is
,
OutputStream
output stream to write data to.
os
,
boolean
if true, the output stream will be closed when the input is exhausted.
closeWhenExhausted
,
boolean
whether the pumper should use available to determine whether input is ready, thus trying to emulate non-blocking behavior.
useAvailable
)

Create a new StreamPumper.

public
StreamPumper(InputStream
input stream to read data from
is
,
OutputStream
output stream to write data to.
os
)

Create a new StreamPumper.

Method Summary

Modifier and TypeMethod and Description
private void
public synchronized int

Returns:

the int size of the read buffer.
getBufferSize
()

Get the size in bytes of the read buffer.

public synchronized Exception

Returns:

the Exception encountered.
getException
()

Get the exception encountered, if any.

public boolean

Returns:

true is the stream has been exhausted.
isFinished
()

Tells whether the end of the stream has been reached.

public void
run()

Implements java.lang.Runnable.run.

Copies data from the input stream to the output stream.
pack-priv void
setAutoflush(boolean
if true, push through data; if false, let it be buffered
autoflush
)

Set whether data should be flushed through to the output stream.

public synchronized void
setBufferSize(int
the buffer size to use.
bufferSize
)

Set the size in bytes of the read buffer.

pack-priv synchronized StreamPumper.PostStopHandle

Returns:

Returns a PostStopHandle for the callers to know if the status of post-stop activities, that happen, before this StreamPumper is actually finished
stop
()

Stop the pumper as soon as possible.

public synchronized void
waitFor()

This method blocks until the StreamPumper finishes.

private void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

askedToStopback to summary
private volatile boolean askedToStop
autoflushback to summary
private boolean autoflush
bufferSizeback to summary
private int bufferSize
closeWhenExhaustedback to summary
private final boolean closeWhenExhausted
exceptionback to summary
private Exception exception
finishedback to summary
private volatile boolean finished
isback to summary
private final InputStream is
osback to summary
private final OutputStream os
POLL_INTERVALback to summary
private static final long POLL_INTERVAL
postStopHandleback to summary
private StreamPumper.PostStopHandle postStopHandle
SMALL_BUFFER_SIZEback to summary
private static final int SMALL_BUFFER_SIZE
startedback to summary
private boolean started
useAvailableback to summary
private final boolean useAvailable

Constructor Detail

StreamPumperback to summary
public StreamPumper(InputStream is, OutputStream os, boolean closeWhenExhausted)

Create a new StreamPumper.

Parameters
is:InputStream

input stream to read data from

os:OutputStream

output stream to write data to.

closeWhenExhausted:boolean

if true, the output stream will be closed when the input is exhausted.

StreamPumperback to summary
public StreamPumper(InputStream is, OutputStream os, boolean closeWhenExhausted, boolean useAvailable)

Create a new StreamPumper.

Note

If you set useAvailable to true, you must explicitly invoke stop or interrupt the corresponding Thread when you are done or the run method will never finish on some JVMs (namely those where available returns 0 on a closed stream). Setting it to true may also impact performance negatively. This flag should only be set to true if you intend to stop the pumper before the input stream gets closed.

Parameters
is:InputStream

input stream to read data from

os:OutputStream

output stream to write data to.

closeWhenExhausted:boolean

if true, the output stream will be closed when the input is exhausted.

useAvailable:boolean

whether the pumper should use available to determine whether input is ready, thus trying to emulate non-blocking behavior.

Since
Ant 1.8.0
StreamPumperback to summary
public StreamPumper(InputStream is, OutputStream os)

Create a new StreamPumper.

Parameters
is:InputStream

input stream to read data from

os:OutputStream

output stream to write data to.

Method Detail

doPostStopback to summary
private void doPostStop() throws IOException
getBufferSizeback to summary
public synchronized int getBufferSize()

Get the size in bytes of the read buffer.

Returns:int

the int size of the read buffer.

getExceptionback to summary
public synchronized Exception getException()

Get the exception encountered, if any.

Returns:Exception

the Exception encountered.

isFinishedback to summary
public boolean isFinished()

Tells whether the end of the stream has been reached.

Returns:boolean

true is the stream has been exhausted.

runback to summary
public void run()

Implements java.lang.Runnable.run.

Copies data from the input stream to the output stream. Terminates as soon as the input stream is closed or an error occurs.

Annotations
@Override
setAutoflushback to summary
pack-priv void setAutoflush(boolean autoflush)

Set whether data should be flushed through to the output stream.

Parameters
autoflush:boolean

if true, push through data; if false, let it be buffered

Since
Ant 1.6.3
setBufferSizeback to summary
public synchronized void setBufferSize(int bufferSize)

Set the size in bytes of the read buffer.

Parameters
bufferSize:int

the buffer size to use.

Exceptions
IllegalStateException:
if the StreamPumper is already running.
stopback to summary
pack-priv synchronized StreamPumper.PostStopHandle stop()

Stop the pumper as soon as possible. Note that it may continue to block on the input stream but it will really stop the thread as soon as it gets EOF or any byte, and it will be marked as finished.

Returns:StreamPumper.PostStopHandle

Returns a PostStopHandle for the callers to know if the status of post-stop activities, that happen, before this StreamPumper is actually finished

Since
Ant 1.6.3, Ant 10.2.0 this method returns a PostStopHandle
waitForback to summary
public synchronized void waitFor() throws InterruptedException

This method blocks until the StreamPumper finishes.

Exceptions
InterruptedException:
if interrupted.
See Also
isFinished()
waitForInputback to summary
private void waitForInput(InputStream is) throws IOException, InterruptedException
org.apache.tools.ant.taskdefs back to summary

pack-priv final Class StreamPumper.PostStopHandle

extends Object
Class Inheritance

A handle that can be used after stop() has been invoked to check if the StreamPumper is in the process of do some post-stop tasks (like flushing of streams), before finishing.

Field Summary

Modifier and TypeField and Description
private boolean
private final CountDownLatch

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
pack-priv boolean

Returns:

Returns true if the post-stop activities completed within the specified timeout. Else returns false
awaitPostStopCompletion
(final long
The maximum amount of time to wait for the post-stop activities to complete
timeout
,
final TimeUnit
The unit of timeout
timeUnit
)

Waits for a maximum of timeout time for the post-stop activities to complete.

pack-priv boolean
isInPostStopTasks()

Returns true if the StreamPumper is doing post-stop tasks (like flushing of streams).

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

inPostStopTasksback to summary
private boolean inPostStopTasks
latchback to summary
private final CountDownLatch latch

Constructor Detail

PostStopHandleback to summary
pack-priv PostStopHandle()

Method Detail

awaitPostStopCompletionback to summary
pack-priv boolean awaitPostStopCompletion(final long timeout, final TimeUnit timeUnit) throws InterruptedException

Waits for a maximum of timeout time for the post-stop activities to complete.

Parameters
timeout:long

The maximum amount of time to wait for the post-stop activities to complete

timeUnit:TimeUnit

The unit of timeout

Returns:boolean

Returns true if the post-stop activities completed within the specified timeout. Else returns false

Exceptions
InterruptedException:
If the current thread was interrupted while waiting
isInPostStopTasksback to summary
pack-priv boolean isInPostStopTasks()

Returns true if the StreamPumper is doing post-stop tasks (like flushing of streams). Else returns false.