one.util
Class RubberPipedInputStream

java.lang.Object
  |
  +--java.io.InputStream
        |
        +--one.util.RubberPipedInputStream

public class RubberPipedInputStream
extends InputStream

RubberPipedInputStream and RubberPipedOutputStream allow data to be piped between two or more threads, similar to PipedInputStream and PipedOutputStream. However, RubberPipedInputStream grows its internal buffer rather than blocking when the buffer is full. It also supports mark(int) and reset().

A RubberPipedInputStream is useful when data must be written to a stream in response to one event and read from a stream in response to another event, for instance, when reassembling the contents of Chunks into a byte stream.

Version:
$Revision: 1.2 $
See Also:
RubberPipedOutputStream

Field Summary
protected  byte[] buffer
          The circular buffer into which incoming data is placed.
protected  boolean closed
          Is this rubber piped input stream closed?
protected  boolean connected
          Is this rubber piped input stream connected?
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 static int INITIAL_SIZE
          The initial buffer size.
protected  int mark
          The mark position.
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
RubberPipedInputStream()
          Constructs a new rubber piped input stream.
RubberPipedInputStream(RubberPipedOutputStream out)
          Constructs a new rubber piped input stream connected to the specified rubber piped output stream.
 
Method Summary
 int available()
          Returns the number of bytes that can be read from this input stream without blocking.
protected  void checkConnected()
          Check to see if the pipe is connected.
 void close()
          Closes this rubber piped input stream.
 void connect(RubberPipedOutputStream out)
          Connects this rubber piped input stream to the specified rubber piped output stream.
protected  int freespace()
          Computes the amount of free space in the buffer.
protected  void grow()
          Doubles the size of the internal buffer.
static void main(String[] args)
          A simple test program.
 void mark(int readlimit)
          Marks the current position in this input stream.
 boolean markSupported()
          Tests if this input stream supports the mark and reset methods.
 int read()
          Reads one byte of data from this rubber piped input stream.
 int read(byte[] b, int off, int len)
          Reads up to len bytes of data from this rubber piped input stream into an array of bytes.
protected  void receive1(byte[] b, int off, int len)
          Receives data from an array of bytes.
protected  void receive1(int b)
          Receives a single byte.
 void reset()
          Repositions this stream to the position at the time the mark method was last called on this input stream.
 
Methods inherited from class java.io.InputStream
read, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

INITIAL_SIZE

protected static final int INITIAL_SIZE
The initial buffer size.

buffer

protected byte[] buffer
The circular buffer into which incoming data is placed.

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.

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

mark

protected int mark
The mark position.

connected

protected boolean connected
Is this rubber piped input stream connected?

closed

protected boolean closed
Is this rubber piped input stream closed?
Constructor Detail

RubberPipedInputStream

public RubberPipedInputStream()
Constructs a new rubber piped input stream. It must be connected using connect(RubberPipedOutputStream) or RubberPipedOutputStream.RubberPipedOutputStream(RubberPipedInputStream) before it can be used; otherwise, an IOException will result.

RubberPipedInputStream

public RubberPipedInputStream(RubberPipedOutputStream out)
                       throws IOException
Constructs a new rubber piped input stream connected to the specified rubber piped output stream.
Parameters:
out - The output stream to connect to.
Throws:
IOException - if out is already connected.
Method Detail

connect

public void connect(RubberPipedOutputStream out)
             throws IOException
Connects this rubber piped input stream to the specified rubber piped output stream. This is equivalent to
   out.connect(this)
 
Parameters:
out - The output stream to connect to.
Throws:
IOException - if either endpoint is already connected.

checkConnected

protected void checkConnected()
                       throws IOException
Check to see if the pipe is connected.
Throws:
IOException - if the pipe is closed or is not connected.

grow

protected void grow()
Doubles the size of the internal buffer.

freespace

protected int freespace()
Computes the amount of free space in the buffer.

available

public int available()
Returns the number of bytes that can be read from this input stream without blocking.
Overrides:
available in class InputStream
Returns:
The number of bytes that can be read without blocking.

receive1

protected void receive1(int b)
                 throws IOException
Receives a single byte. If no space is available, the buffer will be thrown.
Parameters:
b - The byte to receive.
Throws:
IOException - if the pipe is closed or is not connected.

receive1

protected void receive1(byte[] b,
                        int off,
                        int len)
                 throws IOException
Receives data from an array of bytes. If no space is available, the buffer will be grown.
Parameters:
b - The buffer from which the data is received.
off - The start offset of the data.
len - The number of bytes to receive.
Throws:
IOException - if the pipe is closed or is not connected.

read

public int read()
         throws IOException
Reads one byte of data from this rubber piped input stream. This method blocks until at least one byte of input is available.
Overrides:
read in class InputStream
Throws:
IOException - if the pipe is closed or is not connected.

read

public int read(byte[] b,
                int off,
                int len)
         throws IOException
Reads up to len bytes of data from this rubber 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.
Overrides:
read in class InputStream
Parameters:
b - the buffer into which the data is read.
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.

mark

public void mark(int readlimit)
Marks the current position in this input stream. A subsequent call to the reset method repositions this stream at the last marked position so that subsequent reads re-read the same bytes.
Overrides:
mark in class InputStream
Parameters:
readlimit - The maximum limit of bytes that can be read before the mark position becomes invalid.

reset

public void reset()
           throws IOException
Repositions this stream to the position at the time the mark method was last called on this input stream.
Overrides:
reset in class InputStream
Throws:
IOException - if no mark has been set.

markSupported

public boolean markSupported()
Tests if this input stream supports the mark and reset methods. The markSupported method of RubberPipedInputStream returns true.
Overrides:
markSupported in class InputStream

close

public void close()
Closes this rubber piped input stream.
Overrides:
close in class InputStream

main

public static void main(String[] args)
                 throws IOException
A simple test program.


(C) Copyright 2001 UW CSE