ewe.io
Class StreamAdapter

java.lang.Object
  extended byewe.util.Errorable
      extended byewe.io.BasicStreamObject
          extended byewe.io.StreamObject
              extended byewe.io.StreamAdapter
All Implemented Interfaces:
BasicStream, BufferedStream, Stream, Streamable, StreamCanPause
Direct Known Subclasses:
DataProcessorStream, PartialInputStream, PartialOutputStream

public class StreamAdapter
extends StreamObject
implements BufferedStream, StreamCanPause

This provides a full stream from a BasicStream implementation. This allows you to define that absolute bare minimum for Stream functionality in an object (which is quite easy to do) and then use a StreamAdapter to convert it to a full stream. Use this if your object cannot extend a StreamObject - which is the preferred way to create a new Stream class.

This also provides a Stream implementation from an InputStream or OutputStream.


Field Summary
protected  InputStream in
           
protected  OutputStream out
           
protected  BasicStream stream
           
 
Fields inherited from class ewe.io.StreamObject
napIterations, napTime, READWRITE_CLOSED, READWRITE_ERROR, READWRITE_WOULDBLOCK
 
Fields inherited from class ewe.io.BasicStreamObject
closed
 
Fields inherited from class ewe.util.Errorable
error
 
Fields inherited from interface ewe.io.StreamCanPause
PAUSE_UNTIL_CAN_READ, PAUSE_UNTIL_CAN_WRITE, PAUSE_UNTIL_CLOSED, PAUSE_UNTIL_FLUSHED
 
Constructor Summary
protected StreamAdapter()
           
  StreamAdapter(BasicStream basicStream)
           
  StreamAdapter(InputStream in)
           
  StreamAdapter(InputStream in, OutputStream out)
           
  StreamAdapter(OutputStream out)
           
 
Method Summary
 boolean closeStream()
          This is non-blocking.
 boolean flushStream()
          This is non-blocking.
 boolean isOpen()
          Returns if the stream is open or not.
 int nonBlockingRead(byte[] buff, int start, int count)
          This calls the nonBlockingRead() of the client BasicStream.
 int nonBlockingWrite(byte[] buff, int start, int count)
          This calls the nonBlockingWrite() of the client BasicStream.
 int pauseUntilReady(int pauseFor, int value)
           
 boolean pushback(byte[] bytes, int start, int count)
          Push back some bytes into the input stream.
 
Methods inherited from class ewe.io.StreamObject
close, doRead, doWrite, flush, nap, nap, read, readAByte, readBytes, readBytes, write, writeAByte, writeBytes, writeBytes
 
Methods inherited from class ewe.io.BasicStreamObject
getException, getName, read, read, throwIOException, toInputStream, toOutputStream, toStream, write, write
 
Methods inherited from class ewe.util.Errorable
returnError, returnError, returnError
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, toString
 
Methods inherited from interface ewe.io.Stream
flush, read, read, read, readBytes, readBytes, toInputStream, toOutputStream, write, write, write, writeBytes, writeBytes
 
Methods inherited from interface ewe.io.BasicStream
close
 

Field Detail

stream

protected BasicStream stream

out

protected OutputStream out

in

protected InputStream in
Constructor Detail

StreamAdapter

protected StreamAdapter()

StreamAdapter

public StreamAdapter(BasicStream basicStream)

StreamAdapter

public StreamAdapter(InputStream in)

StreamAdapter

public StreamAdapter(OutputStream out)

StreamAdapter

public StreamAdapter(InputStream in,
                     OutputStream out)
Method Detail

pauseUntilReady

public int pauseUntilReady(int pauseFor,
                           int value)
Specified by:
pauseUntilReady in interface StreamCanPause

pushback

public boolean pushback(byte[] bytes,
                        int start,
                        int count)
Push back some bytes into the input stream. There is no limit on the number of bytes which can be pushed back. This can be called multiple times.

Specified by:
pushback in interface BufferedStream

isOpen

public boolean isOpen()
Description copied from interface: BasicStream
Returns if the stream is open or not.

Specified by:
isOpen in interface BasicStream
Overrides:
isOpen in class StreamObject

closeStream

public boolean closeStream()
                    throws IOException
Description copied from interface: BasicStream
This is non-blocking. It returns true if the close completed, false if it did not, or throws an exception on error.

Specified by:
closeStream in interface BasicStream
Overrides:
closeStream in class StreamObject
Throws:
IOException

nonBlockingRead

public int nonBlockingRead(byte[] buff,
                           int start,
                           int count)
This calls the nonBlockingRead() of the client BasicStream.

Specified by:
nonBlockingRead in interface BasicStream
Overrides:
nonBlockingRead in class StreamObject
Parameters:
buff - Destination byte array to hold incoming data.
start - Starting index in buff for incoming data.
count - Maximum number of bytes to read - should never be zero.
Returns:
greater than 0 = Number of bytes read.
0 = No bytes available to read at this time.
-1 = Stream end reached no further bytes to read.
-2 = IO Error.
Note that these values are different to the Stream.readBytes method.

nonBlockingWrite

public int nonBlockingWrite(byte[] buff,
                            int start,
                            int count)
This calls the nonBlockingWrite() of the client BasicStream.

Specified by:
nonBlockingWrite in interface BasicStream
Overrides:
nonBlockingWrite in class StreamObject
Parameters:
buff - Source byte array holding data to be written.
start - Starting index in buff for data to be written.
count - Number of bytes to write - should never be zero.
Returns:
greater than 0 = Number of bytes actually written.
0 = No bytes could be written yet - but the stream is still open.
-1 = Stream has been closed - no further writes are possible.
-2 = IO error - something went wrong.
Note that these values are different to the Stream.writeBytes method.

flushStream

public boolean flushStream()
                    throws IOException
Description copied from interface: BasicStream
This is non-blocking. It returns true if the flush completed, false if it did not, or throws an exception on error.

Specified by:
flushStream in interface BasicStream
Throws:
IOException