ewe.io
Class RandomStreamAdapter

java.lang.Object
  extended byewe.util.Errorable
      extended byewe.io.BasicStreamObject
          extended byewe.io.StreamObject
              extended byewe.io.RandomStreamObject
                  extended byewe.io.RandomStreamAdapter
All Implemented Interfaces:
BasicRandomAccessStream, BasicStream, BufferedStream, RandomAccessStream, Stream, Streamable, StreamCanPause

public class RandomStreamAdapter
extends RandomStreamObject
implements BufferedStream, StreamCanPause

This provides a full RandomAccessStream implementation from either a BasicRandomAccessStream implementation or from a RandomStream object.


Field Summary
protected  RandomStream rs
           
protected  BasicRandomAccessStream 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 RandomStreamAdapter()
           
  RandomStreamAdapter(BasicRandomAccessStream basicStream)
           
  RandomStreamAdapter(RandomStream rs)
           
 
Method Summary
 boolean canWrite()
          Tests if the RandomAccessStream was opened in read-write mode as opposed to read-ony mode.
 boolean closeStream()
          This is non-blocking.
 boolean flushStream()
          This is non-blocking.
 long getStreamLength()
          Gets the length of the open stream.
 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 nonBlockingRead(long location, byte[] dest, int offset, int length)
          Non-blocking read at a particular location.
 int nonBlockingWrite(byte[] buff, int start, int count)
          This calls the nonBlockingWrite() of the client BasicStream.
 int nonBlockingWrite(long location, byte[] src, int offset, int length)
          Non-blocking write at a particular location.
 int pauseUntilReady(int pauseFor, int value)
           
 boolean pushback(byte[] bytes, int start, int count)
          Push back some bytes into the input stream.
 boolean seekPosition(long where)
          Tell the Stream to move to the specific position.
 boolean setStreamLength(long length)
          Set the length of the RandomAccessStream if possible.
 long tellPosition()
          Retrieve the file position.
 
Methods inherited from class ewe.io.RandomStreamObject
getFilePosition, getLength, length, seek, seek, setLength, tell, toRandomStream
 
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

rs

protected RandomStream rs

stream

protected BasicRandomAccessStream stream
Constructor Detail

RandomStreamAdapter

protected RandomStreamAdapter()

RandomStreamAdapter

public RandomStreamAdapter(BasicRandomAccessStream basicStream)

RandomStreamAdapter

public RandomStreamAdapter(RandomStream rs)
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

canWrite

public boolean canWrite()
Description copied from interface: BasicRandomAccessStream
Tests if the RandomAccessStream was opened in read-write mode as opposed to read-ony mode.

Specified by:
canWrite in interface BasicRandomAccessStream

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

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

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

setStreamLength

public boolean setStreamLength(long length)
                        throws IOException
Description copied from interface: BasicRandomAccessStream
Set the length of the RandomAccessStream if possible. This is non-blocking, it will return false if the operation is still continuing and true if the operation completed. If the operation could not be performed at all an IOException will be thrown.

Make no assumptions about the success of this method. Not all RAS objects will support setStreamLength() or setLength() - not even all Files on all systems will support this. For example, PersonalJava/Java 1.1 does not support this feature and will throw an IOException.

What happens to the file position pointer after this method is called is unpredictable, especially if you are truncating the file. You should ALWAYS reset the file position pointer after calling this method to be where you wish it to be.

Specified by:
setStreamLength in interface BasicRandomAccessStream
Overrides:
setStreamLength in class RandomStreamObject
Throws:
IOException

getStreamLength

public long getStreamLength()
                     throws IOException
Description copied from class: RandomStreamObject
Gets the length of the open stream. This is non-blocking.

Specified by:
getStreamLength in interface BasicRandomAccessStream
Overrides:
getStreamLength in class RandomStreamObject
Returns:
the length of the stream or -1 if the length is not known yet.
Throws:
IOException - if an error occured while getting the length.

tellPosition

public long tellPosition()
                  throws IOException
Description copied from class: RandomStreamObject
Retrieve the file position. This is non-blocking

Specified by:
tellPosition in interface BasicRandomAccessStream
Overrides:
tellPosition in class RandomStreamObject
Returns:
the position of the stream or -1 if the position is not known yet.
Throws:
IOException - if an error occured while getting the position.

seekPosition

public boolean seekPosition(long where)
                     throws IOException
Description copied from class: RandomStreamObject
Tell the Stream to move to the specific position. This is a non-blocking call.

Specified by:
seekPosition in interface BasicRandomAccessStream
Overrides:
seekPosition in class RandomStreamObject
Parameters:
where - The position to seek to.
Returns:
true if the seek completed successfully, false if the seek did not complete yet.
Throws:
IOException - if an error occured during the seek.

nonBlockingRead

public int nonBlockingRead(long location,
                           byte[] dest,
                           int offset,
                           int length)
                    throws IOException
Description copied from interface: BasicRandomAccessStream
Non-blocking read at a particular location. Used if there are multiple threads reading/writing to a RandomAccessStream.

Specified by:
nonBlockingRead in interface BasicRandomAccessStream
Overrides:
nonBlockingRead in class RandomStreamObject
Throws:
IOException

nonBlockingWrite

public int nonBlockingWrite(long location,
                            byte[] src,
                            int offset,
                            int length)
                     throws IOException
Description copied from interface: BasicRandomAccessStream
Non-blocking write at a particular location. Used if there are multiple threads reading/writing to a RandomAccessStream.

Specified by:
nonBlockingWrite in interface BasicRandomAccessStream
Overrides:
nonBlockingWrite in class RandomStreamObject
Throws:
IOException