ewe.io
Class InputStream

java.lang.Object
  extended byewe.io.InputStream
All Implemented Interfaces:
Streamable
Direct Known Subclasses:
ByteArrayInputStream, DecryptedInputStream, FileInputStream, FilterInputStream, SocketBase.SocketInputStream, ZipInputStream

public class InputStream
extends Object
implements Streamable

An InputStream is used to read bytes - however all read() operations will block the current thread. This is not meant to be a replacement for a ewe.io.Stream - rather it is meant to make it easier to port Java applications.

This can be used either as the base class for creating your own InputStream objects, OR it can be used to adapt a Stream object into an InputStream, or adapt a RandomStream object into an InputStream.


Field Summary
protected  boolean atEOF
           
protected  boolean closed
           
protected  InputStream in
          The underlying InputStream object - if any.
protected  RandomStream rs
          The underlying InputStream object - if any.
protected  Stream stream
          The underlying Stream object - if any.
 
Constructor Summary
protected InputStream()
           
protected InputStream(InputStream in)
           
  InputStream(RandomStream rs)
           
  InputStream(Stream stream)
           
 
Method Summary
 int available()
          Returns the number of bytes that can be read without blocking.
 void close()
           
 FastStream getFastStream()
          If the underlying Stream object implements FastStream this method will return that Stream object.
 String getName()
          Get the name associated with this Streamable object.
 void mark(int readLimit)
           
 boolean markSupported()
           
 int read()
          Reads the next byte of data from this input stream.
 int read(byte[] buffer)
          Read in a number of bytes of data from the input stream equal to the length of the provided buffer.
 int read(byte[] buffer, int start, int length)
          Read in a number of bytes of data from the input stream.
protected  int readSingleByteFromMultiByteRead()
          This reads a single byte using the InputStream read(byte[] buffer,int start,int lengh) method.
 void reset()
           
 void shutdown()
          This prevents further reads from the InputStream but does not close any underlying IO stream or resource (e.g.
 long skip(long toSkip)
          Skip over a certain number of bytes.
 Stream toReadableStream()
          Convert this InputStream to a Stream implementation that allows reading.
 Handle toStream(boolean randomStream, String mode)
          Requests the possibly asynchronous creation of a Stream object for reading or writing.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, toString
 

Field Detail

closed

protected boolean closed

atEOF

protected boolean atEOF

stream

protected Stream stream
The underlying Stream object - if any.


in

protected InputStream in
The underlying InputStream object - if any.


rs

protected RandomStream rs
The underlying InputStream object - if any.

Constructor Detail

InputStream

protected InputStream()

InputStream

public InputStream(Stream stream)

InputStream

public InputStream(RandomStream rs)

InputStream

protected InputStream(InputStream in)
Method Detail

toStream

public Handle toStream(boolean randomStream,
                       String mode)
Description copied from interface: Streamable
Requests the possibly asynchronous creation of a Stream object for reading or writing.

Specified by:
toStream in interface Streamable
Parameters:
randomStream - if this is true then a request is being made for a RandomAccessStream, otherwise either a readable or writable Stream will be returned.
mode - For RandomAccessStreams this can be "r" or "rw". For writable Streams this should be "w" or "a" (for append) and for readable Streams it should be "r".
Returns:
A Handle used to form monitoring the creation process and for retrieving the final value.

The calling code should wait on the Handle.Success flag to be set. If this happens the returnValue of the Handle will be set to the acquired Stream. If the Handle fails then the errorObject of the Handle will be set to an IOException.


getName

public String getName()
Description copied from interface: Streamable
Get the name associated with this Streamable object.

Specified by:
getName in interface Streamable

close

public void close()
           throws IOException
Throws:
IOException

readSingleByteFromMultiByteRead

protected int readSingleByteFromMultiByteRead()
                                       throws IOException
This reads a single byte using the InputStream read(byte[] buffer,int start,int lengh) method. If you override the multi-byte read operation instead of the single byte read operation, then override int read() to call this method.

Throws:
IOException

read

public int read()
         throws IOException
Reads the next byte of data from this 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.

Returns:
the byte read or -1 on end of stream.
Throws:
IOException - if an I/O error occured.

read

public int read(byte[] buffer,
                int start,
                int length)
         throws IOException
Read in a number of bytes of data from the input stream.

Parameters:
buffer - a destination buffer for the data.
start - The start offset in the destination buffer.
length - The number of bytes to 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 during reading.

read

public int read(byte[] buffer)
         throws IOException
Read in a number of bytes of data from the input stream equal to the length of the provided buffer. Additional verbose

Parameters:
buffer - a destination buffer for the data.
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 during reading.

markSupported

public boolean markSupported()

mark

public void mark(int readLimit)

reset

public void reset()
           throws IOException
Throws:
IOException

skip

public long skip(long toSkip)
          throws IOException
Skip over a certain number of bytes.

Parameters:
toSkip - the number of bytes to skip over.
Returns:
the actual number of bytes skipped - which may be less than toSkip.
Throws:
IOException - if an I/O error occurs while skipping.

available

public int available()
              throws IOException
Returns the number of bytes that can be read without blocking.

Returns:
0 in the default implementation of InputStream.
Throws:
IOException - if an I/O error occurs.

shutdown

public void shutdown()
              throws IOException
This prevents further reads from the InputStream but does not close any underlying IO stream or resource (e.g. a Socket). Any further reads will return end-of-file.

Throws:
IOException

toReadableStream

public Stream toReadableStream()
Convert this InputStream to a Stream implementation that allows reading. This will never return null.


getFastStream

public FastStream getFastStream()
If the underlying Stream object implements FastStream this method will return that Stream object. If this InputStream also happens to implement FastStream, then this InputStream will be returned. Otherwise the method will return null.