java.io

Class PipedInputStream

Implemented Interfaces:
Closeable

public class PipedInputStream
extends InputStream

A piped input stream should be connected to a piped output stream; the piped input stream then provides whatever data bytes are written to the piped output stream. Typically, data is read from a PipedInputStream object by one thread and data is written to the corresponding PipedOutputStream by some other thread. Attempting to use both objects from a single thread is not recommended, as it may deadlock the thread. The piped input stream contains a buffer, decoupling read operations from write operations, within limits.
Since:
JDK1.0
See Also:
PipedOutputStream

Field Summary

protected static int
PIPE_SIZE
The size of the pipe's circular input buffer.
protected byte[]
buffer
The circular buffer into which incoming data is placed.
protected int
in
The index of the position in the circular buffer at which the next byte of data will be stored when received from the connected piped output stream.
protected int
out
The index of the position in the circular buffer at which the next byte of data will be read by this piped input stream.

Constructor Summary

PipedInputStream()
Creates a PipedInputStream so that it is not yet connected.
PipedInputStream(PipedOutputStream src)
Creates a PipedInputStream so that it is connected to the piped output stream src.

Method Summary

int
available()
Returns the number of bytes that can be read from this input stream without blocking.
void
close()
Closes this piped input stream and releases any system resources associated with the stream.
void
connect(PipedOutputStream src)
Causes this piped input stream to be connected to the piped output stream src.
int
read()
Reads the next byte of data from this piped input stream.
int
read(b[] , int off, int len)
Reads up to len bytes of data from this piped input stream into an array of bytes.
protected void
receive(int b)
Receives a byte of data.

Methods inherited from class java.io.InputStream

available, close, mark, markSupported, read, read, read, reset, skip

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Details

PIPE_SIZE

protected static final int PIPE_SIZE
The size of the pipe's circular input buffer.
Field Value:
1024
Since:
JDK1.1

buffer

protected byte[] buffer
The circular buffer into which incoming data is placed.
Since:
JDK1.1

in

protected int in
The index of the position in the circular buffer at which the next byte of data will be stored when received from the connected piped output stream. in<0 implies the buffer is empty, in==out implies the buffer is full
Since:
JDK1.1

out

protected int out
The index of the position in the circular buffer at which the next byte of data will be read by this piped input stream.
Since:
JDK1.1

Constructor Details

PipedInputStream

public PipedInputStream()
Creates a PipedInputStream so that it is not yet connected. It must be connected to a PipedOutputStream before being used.
Usages and Demos :

View More Examples of PipedInputStream()
   1:      byte aByteData1 = 123, aByteData2 = 111;
   2:      PipedInputStream pis = new PipedInputStream();
   3:      PipedOutputStream pos = new  PipedOutputStream(pis);
   4:      System.out.println("PipedInputStream");
   5:      try

View Full Code Here
   1:   
   2:     pis = new PipedInputStream();
   3:     pos = new PipedOutputStream(pis);
   4:     dos = new DataOutputStream(pos);
   5:     dis = new DataInputStream(pis);

View Full Code Here
   1:         {
   2:             PipedInputStream pin = new PipedInputStream();
   3:             PipedOutputStream pout = new PipedOutputStream();
   4:             pout.connect( pin);
   5:             new PipeSender(pout,"sendString").start();

View Full Code Here
   1:     public static void main(String[] args) throws Exception {
   2:         PipedInputStream in = new PipedInputStream();
   3:         final PipedOutputStream out = new PipedOutputStream(in);
   4: 
   5:         Thread t = new Thread() {

View Full Code Here
   1:     Connection() throws IOException {
   2:         PipedInputStream ci = new PipedInputStream();
   3:         ...
   4:         PipedInputStream si = new PipedInputStream();
   5:         PipedOutputStream co = new PipedOutputStream();
   6:         PipedOutputStream so = new PipedOutputStream();
   7:         ci.connect(so);

View Full Code Here

PipedInputStream

public PipedInputStream(PipedOutputStream src)
            throws IOException
Creates a PipedInputStream so that it is connected to the piped output stream src. Data bytes written to src will then be available as input from this stream.
Parameters:
src - the stream to connect to.
Throws:
IOException - if an I/O error occurs.
Usages and Demos :

View More Examples of PipedInputStream(PipedOutputStream src)
   1:         PipedOutputStream pipeOut = new PipedOutputStream();
   2:         PipedInputStream pipeIn = new PipedInputStream(pipeOut);
   3:         PrintStream out = new PrintStream(pipeOut);
   4:         ...
   5:         PipedOutputStream pipeOut = new PipedOutputStream();
   6:         PipedInputStream pipeIn = new PipedInputStream(pipeOut);
   7:         PrintStream out = new PrintStream(pipeOut);
   8: 
   9:         new SortThread(out, in).start();

View Full Code Here
   1:     public ConsumerPipe(PipedOutputStream out) throws IOException {
   2:         consumer = new PipedInputStream(out);
   3:         pipecount++;
   4:     }
   5:     

View Full Code Here
   1:       try {
   2:          inPipe = new PipedInputStream(outPipe);
   3:       } catch (IOException e) {
   4:          System.err.println("ObjPipedMessagePassing:" + e);
   5:          throw new MessagePassingException();

View Full Code Here
   1:       try {
   2:          inPipe = new PipedInputStream(outPipe);
   3:       } catch (IOException e) {
   4:          System.err.println("ObjPipedMessagePassing:" + e);
   5:          throw new MessagePassingException();

View Full Code Here
   1:         {
   2:             myStream = new PipedInputStream(myMaster);
   3:         }
   4:         public int read(byte[] data) throws IOException
   5:         {

View Full Code Here

Method Details

available

public int available()
            throws IOException
Returns the number of bytes that can be read from this input stream without blocking. This method overrides the available method of the parent class.
Overrides:
available in interface InputStream
Returns:
the number of bytes that can be read from this input stream without blocking.
Throws:
IOException - if an I/O error occurs.
Since:
JDK1.0.2
Usages and Demos :

View More Examples of available()
   1: import java.io.OutputStream;
   2: import java.io.PipedInputStream;
   3: import java.io.PipedOutputStream;
   4:         ...
   5: 
   6:     private PipedInputStream pin;
   7: 
   8:         ...
   9:         pout = new PipedOutputStream();
  10:         pin = new PipedInputStream(pout);
  11:         out = new ThrottledOutputStream(pout, throttle);
  12:         ...
  13:             assertTrue("Bad byte", (byte) pin.read() == b);
  14:             assertEquals("Too many bytes in stream", 0, pin.available());

View Full Code Here
   1:     private Thread converter;
   2:     private PipedInputStream pis;
   3:     private PipedOutputStream pos;
   4:         ...
   5:         super(in);
   6:         pis = new PipedInputStream();
   7:         pos = new PipedOutputStream();
   8:         ...
   9:     public int available() throws IOException {
  10:         return pis.available();
  11:     }

View Full Code Here
   1: import java.io.OutputStream;
   2: import java.io.PipedInputStream;
   3: import java.io.PipedOutputStream;
   4:         ...
   5:         PipedOutputStream outputPipe = new PipedOutputStream();
   6:         PipedInputStream errInPipe, inputPipe;
   7:         try {
   8:         ...
   9:             errInPipe = new PipedInputStream(errOutPipe);
  10:             inputPipe = new PipedInputStream(outputPipe);
  11:         ...
  12:             try {
  13:                 if (errInPipe.available() > 0) {

View Full Code Here

close

public void close()
            throws IOException
Closes this piped input stream and releases any system resources associated with the stream.
Specified by:
close in interface Closeable
Overrides:
close in interface InputStream
Throws:
IOException - if an I/O error occurs.
Usages and Demos :

View More Examples of close()
   1:      byte aByteData1 = 123, aByteData2 = 111;
   2:      PipedInputStream pis = new PipedInputStream();
   3:      PipedOutputStream pos = new  PipedOutputStream(pis);
   4:         ...
   5:      System.out.println("PipedInputStream");
   6:      try
   7:         ...
   8:      { 
   9:         pis.close();    
  10:         ...
  11:         pos.close();

View Full Code Here
   1: import java.io.IOException;
   2: import java.io.PipedInputStream;
   3: import java.io.PipedOutputStream;
   4:         ...
   5: {
   6:     PipedInputStream consumer = null;
   7:     String result;
   8:         ...
   9:     public ConsumerPipe(PipedOutputStream out) throws IOException {
  10:         consumer = new PipedInputStream(out);
  11:         pipecount++;
  12:         ...
  13:             try {
  14:                 consumer.close();

View Full Code Here
   1:         myInputer.closed = true;
   2:         super.close();
   3:     }
   4:         ...
   5:     {
   6:         PipedInputStream myStream;
   7:         boolean closed;
   8:         ...
   9:         {
  10:             myStream = new PipedInputStream(myMaster);
  11:         }
  12:         ...
  13:         {
  14:             myStream.close();

View Full Code Here
   1:     private static Vector messages;
   2:     private PipedInputStream m_pipedIn;
   3:     private LineNumberReader m_LineReader;
   4:         ...
   5:         try {
   6:             m_pipedIn.close();
   7:         }
   8:         catch(IOException e)  {
   9:             e.printStackTrace();

View Full Code Here

connect

public void connect(PipedOutputStream src)
            throws IOException
Causes this piped input stream to be connected to the piped output stream src. If this object is already connected to some other piped output stream, an IOException is thrown.

If src is an unconnected piped output stream and snk is an unconnected piped input stream, they may be connected by either the call:

snk.connect(src) 

or the call:

src.connect(snk) 

The two calls have the same effect.

Parameters:
src - The piped output stream to connect to.
Throws:
IOException - if an I/O error occurs.
Usages and Demos :

View More Examples of connect(PipedOutputStream src)
   1:     Connection() throws IOException {
   2:         PipedInputStream ci = new PipedInputStream();
   3:         ...
   4:         PipedInputStream si = new PipedInputStream();
   5:         PipedOutputStream co = new PipedOutputStream();
   6:         ...
   7:         PipedOutputStream so = new PipedOutputStream();
   8:         ci.connect(so);
   9:         ...
  10:         si.connect(co);

View Full Code Here
   1:     final PipedOutputStream out2  = new PipedOutputStream();
   2:     final PipedInputStream in1 = new PipedInputStream();
   3:         ...
   4:     final PipedInputStream in2 = new PipedInputStream();
   5: 
   6:         ...
   7:     in1.connect(out1);
   8:         ...
   9:     in2.connect(out2);

View Full Code Here
   1: import java.io.IOException;
   2: import java.io.PipedInputStream;
   3: import java.io.PipedOutputStream;
   4:         ...
   5:         try {
   6:             PipedInputStream pipe_in = new PipedInputStream();
   7:             PipedOutputStream pipe_out = new PipedOutputStream();
   8:         ...
   9: 
  10:             pipe_in.connect(pipe_out);
  11: 
  12:         ...
  13: 
  14:             pipe_in.connect(pipe_out);

View Full Code Here
   1:   protected final PipedOutputStream outp_;
   2:   protected final PipedInputStream inp_;
   3: 
   4:         ...
   5:       outp_ = new PipedOutputStream();
   6:       inp_ = new PipedInputStream();
   7:         ...
   8:       inp_.connect(outp_);
   9:     }

View Full Code Here
   1:     {
   2:       System.out.println("Started test of PipedInputStream and " +
   3:                          "PipedOutputStream");
   4:         ...
   5: 
   6:       PipedInputStream pis = new PipedInputStream();
   7:         ...
   8:       pis.connect(pos); 
   9:       new Thread(pstw).start();

View Full Code Here

read

public int read()
            throws IOException
Reads the next byte of data from this piped input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown. If a thread was providing data bytes to the connected piped output stream, but the thread is no longer alive, then an IOException is thrown.
Overrides:
read in interface InputStream
Returns:
the next byte of data, or -1 if the end of the stream is reached.
Throws:
IOException - if the pipe is broken.
Usages and Demos :

View More Examples of read()
   1:      byte aByteData1 = 123, aByteData2 = 111;
   2:      PipedInputStream pis = new PipedInputStream();
   3:      PipedOutputStream pos = new  PipedOutputStream(pis);
   4:         ...
   5:      System.out.println("PipedInputStream");
   6:      try
   7:         ...
   8:          pos.write(aByteData2);
   9:          System.out.println((byte)pis.read());
  10:         ...
  11:          System.out.println((byte)pis.read());

View Full Code Here
   1:         {
   2:             PipedInputStream pin = new PipedInputStream();
   3:             PipedOutputStream pout = new PipedOutputStream();
   4:         ...
   5:     {
   6:         while((n=fin.read())!=-1)
   7:         {
   8:         ...
   9: {
  10:     PipedInputStream pin;
  11:     File fFile;
  12:         ...
  13:     {
  14:         while((n=pin.read())!=-1)

View Full Code Here
   1:     {
   2:         PipedInputStream myStream;
   3:         boolean closed;
   4:         ...
   5:         {
   6:             myStream = new PipedInputStream(myMaster);
   7:         }
   8:         ...
   9:                 throw new IOException("stream has been closed");
  10:             return myStream.read();
  11:         }

View Full Code Here
   1:     private Thread converter;
   2:     private PipedInputStream pis;
   3:     private PipedOutputStream pos;
   4:         ...
   5:         super(in);
   6:         pis = new PipedInputStream();
   7:         pos = new PipedOutputStream();
   8:         ...
   9:         try {
  10:             int magic1 = in.read();
  11:         ...
  12:             int magic2 = in.read();

View Full Code Here

read

public int read(b[] ,
                int off,
                int len)
            throws IOException
Reads up to len bytes of data from this piped input stream into an array of bytes. Less than len bytes will be read if the end of the data stream is reached. This method blocks until at least one byte of input is available. If a thread was providing data bytes to the connected piped output stream, but the thread is no longer alive, then an IOException is thrown.
Overrides:
read in interface InputStream
Parameters:
off - the start offset of the data.
len - the maximum number of bytes read.
Returns:
the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
Throws:
IOException - if an I/O error occurs.
Usages and Demos :

View More Examples of read(b[] ,int off,int len)
   1:     private Thread converter;
   2:     private PipedInputStream pis;
   3:     private PipedOutputStream pos;
   4:         ...
   5:         super(in);
   6:         pis = new PipedInputStream();
   7:         pos = new PipedOutputStream();
   8:         ...
   9:     public int read(byte[] b, int off, int len) throws IOException {
  10:         return pis.read(b, off, len);
  11:     }

View Full Code Here

receive

protected void receive(int b)
            throws IOException
Receives a byte of data. This method will block if no input is available.
Parameters:
b - the byte being received
Throws:
IOException - If the pipe is broken.
Since:
JDK1.1