de.fhg.igd.io
Class Pipe

java.lang.Object
  extended by de.fhg.igd.io.Pipe
All Implemented Interfaces:
Runnable

public class Pipe
extends Object
implements Runnable

This class provides a pipe, which transfers data from an InputStream to an OutputStream. The class can be used either in synchronous (see pipe()) or in asynchronous (see start()) mode.

Version:
"$Id: Pipe.java 1913 2007-08-08 02:41:53Z jpeters $"
Author:
Jan Peters

Field Summary
private  Object lock_
          The internal lock for synchronisation purpose.
private  long pipedBytes_
          The number of bytes currently piped in asynchronous mode.
private  InputStream pipeIn_
          The input stream of the pipe, from which data is read.
private  String pipeName_
          The name of the pipe, used to identify the corresponding thread in asynchronous mode.
private  OutputStream pipeOut_
          The output stream of the pipe, to which data is written.
private  Thread pipeThread_
          The thread responsible to transfer data in asynchronous mode.
 
Constructor Summary
private Pipe()
          Default constructor disabled.
  Pipe(String name, InputStream in, OutputStream out)
          Creates a new Pipe object with the given input and output streams and the given name..
 
Method Summary
 long getPipedBytes()
          Returns the number of bytes, already transfered from the pipe's input to its output stream in asynchronous mode.
 long pipe()
          This method pipes all data from the input stream of the pipe to its output stream, until the EOF of the input stream is reached or an exception occures during transfer of data.
static long pipe(InputStream in, OutputStream out)
          This method pipes all data from the given input stream to the given output stream, until the EOF of the input stream is reached or an exception occures during transfer of data.
 void run()
          The functionality of the pipeThread, which transfers data from the pipe's input to its output stream.
private  void start()
          Start or restart the pipeThread to transfer data from the pipe's input to its output stream in asynchronous mode.
private  void stop()
          Stops the pipeThread.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

pipeThread_

private Thread pipeThread_
The thread responsible to transfer data in asynchronous mode.


pipeIn_

private InputStream pipeIn_
The input stream of the pipe, from which data is read.


pipeOut_

private OutputStream pipeOut_
The output stream of the pipe, to which data is written.


pipeName_

private String pipeName_
The name of the pipe, used to identify the corresponding thread in asynchronous mode.


pipedBytes_

private long pipedBytes_
The number of bytes currently piped in asynchronous mode.


lock_

private Object lock_
The internal lock for synchronisation purpose.

Constructor Detail

Pipe

private Pipe()
Default constructor disabled.


Pipe

public Pipe(String name,
            InputStream in,
            OutputStream out)
Creates a new Pipe object with the given input and output streams and the given name..

Parameters:
name - the name of the pipe identifies the corresponding thread in asynchronous mode.
in - the input stream to read from.
out - the output stream to write to.
Method Detail

pipe

public static long pipe(InputStream in,
                        OutputStream out)
                 throws IOException
This method pipes all data from the given input stream to the given output stream, until the EOF of the input stream is reached or an exception occures during transfer of data.

Parameters:
in - the input stream to read from.
out - the output stream to write to.
Returns:
The number of bytes transfered.
Throws:
IOException - An exception is thrown, if an error occures during transfer of data.

pipe

public long pipe()
          throws IOException
This method pipes all data from the input stream of the pipe to its output stream, until the EOF of the input stream is reached or an exception occures during transfer of data. This method can only be invoked, when the Pipe is not currently running in asynchronous mode.

Parameters:
in - the input stream to read from.
out - the output stream to write to.
Returns:
The number of bytes transfered.
Throws:
IOException - An exception is thrown, if an error occures during transfer of data, or the pipe already runs in asynchronous mode.

start

private void start()
Start or restart the pipeThread to transfer data from the pipe's input to its output stream in asynchronous mode. The thread is automatically stopped, when the end of the input stream has been reached or an exception has occured during transfer of data. In case the method pipe() has been called and is still active, this method is blocked.


stop

private void stop()
Stops the pipeThread.


run

public void run()
The functionality of the pipeThread, which transfers data from the pipe's input to its output stream. This method should not be invoked directly.

Specified by:
run in interface Runnable

getPipedBytes

public long getPipedBytes()
Returns the number of bytes, already transfered from the pipe's input to its output stream in asynchronous mode. The counter is not reset after a stop and restart of the asynchronous mode.

Returns:
The number of bytes already transfered.


Copyright © Fraunhofer Gesellschaft. All Rights Reserved.