ewe.io
Class RandomStream

java.lang.Object
  extended byewe.io.RandomStream
Direct Known Subclasses:
ByteArrayRandomStream, CompressedRandomStream

public class RandomStream
extends Object


Field Summary
protected  boolean closed
           
protected  boolean noWritingAllowed
          Set this true if you are implementing a read-only RandomStream.
protected  RandomAccessStream ras
           
 
Constructor Summary
protected RandomStream()
           
  RandomStream(RandomAccessStream ras)
           
 
Method Summary
 int available()
           
protected  void cantWrite()
          Calling this method causes an IOException to be thrown indicating that writing to this RandomStream is not possible.
 boolean canWrite()
           
protected  void checkClosed()
           
 void close()
           
 void flush()
           
 FastStream getFastStream()
          If the underlying Stream object implements FastStream this method will return that Stream object.
 long length()
           
 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 seek(long position)
           
 void setLength(long newLength)
           
 void shutdown()
          Shutdown the stream but do not close any underlying IO stream.
 long skip(long toSkip)
           
 long tell()
           
 InputStream toInputStream()
           
 OutputStream toOutputStream()
           
 RandomAccessStream toRandomAccessStream()
           
 Stream toReadableStream()
           
 Stream toWritableStream()
           
 void write(byte[] buffer)
          Write a number of bytes of data to the output stream equal to the length of the provided buffer.
 void write(byte[] buffer, int start, int length)
          Write a number of bytes of data to the output stream.
 void write(int value)
          Writes a single byte to the stream.
protected  void writeSingleByteToMultiByteWrite(int value)
          This writes a single byte using the OutputStream write(byte[] buffer,int start,int lengh) method.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, toString
 

Field Detail

ras

protected RandomAccessStream ras

closed

protected boolean closed

noWritingAllowed

protected boolean noWritingAllowed
Set this true if you are implementing a read-only RandomStream.

Constructor Detail

RandomStream

public RandomStream(RandomAccessStream ras)

RandomStream

protected RandomStream()
Method Detail

cantWrite

protected void cantWrite()
                  throws IOException
Calling this method causes an IOException to be thrown indicating that writing to this RandomStream is not possible.

Throws:
IOException - always thrown.

checkClosed

protected void checkClosed()
                    throws IOException
Throws:
IOException

close

public void close()
           throws IOException
Throws:
IOException

seek

public void seek(long position)
          throws IOException
Throws:
IOException

tell

public long tell()
          throws IOException
Throws:
IOException

length

public long length()
            throws IOException
Throws:
IOException

setLength

public void setLength(long newLength)
               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.

flush

public void flush()
           throws IOException
Throws:
IOException

write

public void write(int value)
           throws IOException
Writes a single byte to the stream. This method blocks until the byte is written.

Throws:
IOException - if an I/O error occured.

write

public void write(byte[] buffer,
                  int start,
                  int length)
           throws IOException
Write a number of bytes of data to the output stream. This will block until all the bytes are written.

Parameters:
buffer - the source buffer for the data.
start - The start offset in the buffer.
length - The number of bytes to write.
Throws:
IOException - if an I/O error occurs during writing.

write

public void write(byte[] buffer)
           throws IOException
Write a number of bytes of data to the output stream equal to the length of the provided buffer.

Parameters:
buffer - the source buffer for the data.
Returns:
the total number of bytes written from the buffer - which is always equal to the length of the buffer.
Throws:
IOException - if an I/O error occurs during writing.

shutdown

public void shutdown()
              throws IOException
Shutdown the stream but do not close any underlying IO stream. Further writes will throw an IOException. For example if this OutputStream is on a Socket, this will prevent further outputs but will still allow reading on the InputStream.

Throws:
IOException - on error.

writeSingleByteToMultiByteWrite

protected void writeSingleByteToMultiByteWrite(int value)
                                        throws IOException
This writes a single byte using the OutputStream write(byte[] buffer,int start,int lengh) method. If you override the multi-byte write operation instead of the single byte write operation, then override void write(int value) to call this method.

Throws:
IOException

available

public int available()
              throws IOException
Throws:
IOException

skip

public long skip(long toSkip)
          throws IOException
Throws:
IOException

canWrite

public boolean canWrite()

toReadableStream

public Stream toReadableStream()

toWritableStream

public Stream toWritableStream()

toRandomAccessStream

public RandomAccessStream toRandomAccessStream()

toInputStream

public InputStream toInputStream()

toOutputStream

public OutputStream toOutputStream()

getFastStream

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