java.io

Class PipedInputStream

Implemented Interfaces:
AutoCloseable, Closeable

public class PipedInputStream
extends InputStream

An input stream that reads its bytes from an output stream to which it is connected.

Data is read and written to an internal buffer. It is highly recommended that the PipedInputStream and connected PipedOutputStream be part of different threads. If they are not, the read and write operations could deadlock their thread.

Field Summary

protected static int
PIPE_SIZE
The size of the internal buffer used for input/output.
protected byte[]
buffer
This is the internal circular buffer used for storing bytes written to the pipe and from which bytes are read by this stream
protected int
in
The index into buffer where the next byte from the connected PipedOutputStream will be written.
protected int
out
This index into the buffer where bytes will be read from.

Constructor Summary

PipedInputStream()
Creates a new PipedInputStream that is not connected to a PipedOutputStream.
PipedInputStream(int pipeSize)
Creates a new PipedInputStream of the given size that is not connected to a PipedOutputStream.
PipedInputStream(PipedOutputStream source)
This constructor creates a new PipedInputStream and connects it to the passed in PipedOutputStream.
PipedInputStream(PipedOutputStream source, int pipeSize)
This constructor creates a new PipedInputStream of the given size and connects it to the passed in PipedOutputStream.

Method Summary

int
available()
This method returns the number of bytes that can be read from this stream before blocking could occur.
void
close()
This methods closes the stream so that no more data can be read from it.
void
connect(PipedOutputStream source)
This method connects this stream to the passed in PipedOutputStream.
int
read()
This method reads one byte from the stream.
int
read(byte[] buf, int offset, int len)
This method reads bytes from the stream into a caller supplied buffer.
protected void
receive(int val)
This method receives a byte of input from the source PipedOutputStream.

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 internal buffer used for input/output.
Field Value:
1024

buffer

protected byte[] buffer
This is the internal circular buffer used for storing bytes written to the pipe and from which bytes are read by this stream

in

protected int in
The index into buffer where the next byte from the connected PipedOutputStream will be written. If this variable is equal to out, then the buffer is full. If set to <320, the buffer is empty.

out

protected int out
This index into the buffer where bytes will be read from.

Constructor Details

PipedInputStream

public PipedInputStream()
Creates a new PipedInputStream that is not connected to a PipedOutputStream. It must be connected before bytes can be read from this stream.

PipedInputStream

public PipedInputStream(int pipeSize)
            throws IllegalArgumentException
Creates a new PipedInputStream of the given size that is not connected to a PipedOutputStream. It must be connected before bytes can be read from this stream.
Since:
1.6
IllegalArgumentException If pipeSize <= 0.

PipedInputStream

public PipedInputStream(PipedOutputStream source)
            throws IOException
This constructor creates a new PipedInputStream and connects it to the passed in PipedOutputStream. The stream is then ready for reading.
Parameters:
source - The PipedOutputStream to connect this stream to
Throws:
IOException - If source is already connected.

PipedInputStream

public PipedInputStream(PipedOutputStream source,
                        int pipeSize)
            throws IOException
This constructor creates a new PipedInputStream of the given size and connects it to the passed in PipedOutputStream. The stream is then ready for reading.
Parameters:
source - The PipedOutputStream to connect this stream to
Throws:
IOException - If source is already connected.
Since:
1.6

Method Details

available

public int available()
            throws IOException
This method returns the number of bytes that can be read from this stream before blocking could occur. This is the number of bytes that are currently unread in the internal circular buffer. Note that once this many additional bytes are read, the stream may block on a subsequent read, but it not guaranteed to block.
Overrides:
available in interface InputStream
Returns:
The number of bytes that can be read before blocking might occur
Throws:
IOException - If an error occurs

close

public void close()
            throws IOException
This methods closes the stream so that no more data can be read from it.
Specified by:
close in interface Closeable
close in interface AutoCloseable
Overrides:
close in interface InputStream
Throws:
IOException - If an error occurs

connect

public void connect(PipedOutputStream source)
            throws IOException
This method connects this stream to the passed in PipedOutputStream. This stream is then ready for reading. If this stream is already connected or has been previously closed, then an exception is thrown
Parameters:
source - The PipedOutputStream to connect this stream to
Throws:
IOException - If this PipedInputStream or source has been connected already.

read

public int read()
            throws IOException
This method reads one byte from the stream. -1 is returned to indicated that no bytes can be read because the end of the stream was reached. If the stream is already closed, a -1 will again be returned to indicate the end of the stream.

This method will block if no byte is available to be read.

Overrides:
read in interface InputStream
Returns:
the value of the read byte value, or -1 of the end of the stream was reached
Throws:
IOException - if an error occured

read

public int read(byte[] buf,
                int offset,
                int len)
            throws IOException
This method reads bytes from the stream into a caller supplied buffer. It starts storing bytes at position offset into the buffer and reads a maximum of len bytes. Note that this method can actually read fewer than len bytes. The actual number of bytes read is returned. A -1 is returned to indicated that no bytes can be read because the end of the stream was reached - ie close() was called on the connected PipedOutputStream.

This method will block if no bytes are available to be read.

Overrides:
read in interface InputStream
Parameters:
buf - The buffer into which bytes will be stored
offset - The index into the buffer at which to start writing.
len - The maximum number of bytes to read.
Throws:
IOException - If close() was called on this Piped InputStream.

receive

protected void receive(int val)
            throws IOException
This method receives a byte of input from the source PipedOutputStream. If the internal circular buffer is full, this method blocks.
Parameters:
val - The byte to write to this stream
Throws:
IOException - if error occurs

PipedInputStream.java -- Read portion of piped streams. Copyright (C) 1998, 1999, 2000, 2001, 2003, 2005 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.