biz.c24.io.api.presentation.stream
Class DataReader

java.lang.Object
  extended by biz.c24.io.api.presentation.stream.DataReader
Direct Known Subclasses:
BorderedCharDataReader, ByteAlignedBitDataReader

public abstract class DataReader
extends Object

Data reader abstraction layer.

Provides the common API and general functional for an any incoming data reader.

Version:
$Revision: 10302 $ $Date: 2008-04-02 14:08:09 -0400 (Wed, 02 Apr 2008) $

Nested Class Summary
 class DataReader.Locator
          The location representation class.
 
Field Summary
protected  int count
          The index one greater than the index of the last valid byte in the buffer.
protected  boolean eof
           
protected  DataReader.Locator locator
          The location representation instance.
protected  int pos
          The current position in the buffer.
 
Constructor Summary
DataReader()
           
 
Method Summary
 int available()
          Returns the number of bytes that can be read from this input stream without blocking.
abstract  void close()
          Closes this reader and releases any system resources associated with it.
protected abstract  void compact(int from, int requested)
          Compacts the data in the internal buffer.
protected  void ensureOpen(int length)
          Check to make sure that this reader has enough data to read the length bytes.
 boolean eof()
           
protected abstract  int fill(int requested)
          Fills out the internal buffer by next data portion.
 InputStream getDataStream()
          Returns the underlied incoming data stream.
 DataReader.Locator getLocator()
          Current locator getter.
 int getLookAhead()
          Returns the look ahead limit.
 int getLookBehind()
          Returns the look behind limit.
 DataReader.Locator mark()
          Returns the snapshot of current location within data stream that can be used on rollback.
 void rollback(DataReader.Locator locator)
          Rollback on specified location.
 void setLookAhead(int lookAhead)
          Sets the look ahead limit.
 void setLookBehind(int lookBehind)
          Sets the look behind limit.
 int skip(int n)
          See the general contract of the skip method of InputStream.
protected  void updateLocator(int skipped)
          Updates the location with specified changes of position.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

count

protected int count
The index one greater than the index of the last valid byte in the buffer. This value is always in the range 0 through buffer.length; elements buffer[0] through buffer[count-1] contain buffered input data obtained from the underlying input stream.


pos

protected int pos
The current position in the buffer. This is the index of the next character to be read from the buffer array.

This value is always in the range 0 through count. If it is less than count, then buffer[pos] is the next byte to be supplied as input; if it is equal to count, then the next read or skip operation will require more bytes to be read from the contained input stream.

See Also:
ByteAlignedBitDataReader.buffer

locator

protected DataReader.Locator locator
The location representation instance. Needed for visualisation, diagnostic and debug purposes.


eof

protected boolean eof
Constructor Detail

DataReader

public DataReader()
Method Detail

getLookAhead

public final int getLookAhead()
Returns the look ahead limit.

Returns:
the look ahead limit (in bytes).

getLookBehind

public final int getLookBehind()
Returns the look behind limit.

Returns:
the look behind limit (in bytes).

setLookAhead

public final void setLookAhead(int lookAhead)
Sets the look ahead limit.

Parameters:
lookAhead - the new look ahead limit.

setLookBehind

public final void setLookBehind(int lookBehind)
Sets the look behind limit.

Parameters:
lookBehind - the new look behind limit.

getLocator

public final DataReader.Locator getLocator()
Current locator getter.

Provides client by locator instance. Returnad instance shouldn't be changed.

Returns:
the current locator instance.

skip

public int skip(int n)
         throws IOException
See the general contract of the skip method of InputStream.

Parameters:
n - the number of bytes to be skipped.
Returns:
the actual number of bytes skipped.
Throws:
IOException - if an I/O error occurs.

mark

public final DataReader.Locator mark()
Returns the snapshot of current location within data stream that can be used on rollback.

Returns:
the current location snapshot.
See Also:
rollback(biz.c24.io.api.presentation.stream.DataReader.Locator)

rollback

public void rollback(DataReader.Locator locator)
Rollback on specified location.

Doing rollback to the specified locator position within incoming data stream. If that location stands beside the allowed limit the exception is throwned.

Parameters:
locator - the location of point within data stream to where location should be rolled back.
Throws:
IllegalStateException - If location is out from allowed range.
See Also:
mark()

available

public int available()
Returns the number of bytes that can be read from this input stream without blocking.

The available method of BufferedInputStream returns the sum of the the number of bytes remaining to be read in the buffer (count - pos) and the result of calling the available method of the underlying input stream.

Returns:
the number of bytes that can be read from this input stream without blocking.
See Also:
FilterInputStream.in

getDataStream

public InputStream getDataStream()
Returns the underlied incoming data stream.

The implementation may provide this. By default throws the exception.

Returns:
The incoming data stream.
Throws:
UnsupportedOperationException - If implementation doesn't provided that ability.

close

public abstract void close()
                    throws IOException
Closes this reader and releases any system resources associated with it.

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

fill

protected abstract int fill(int requested)
                     throws IOException
Fills out the internal buffer by next data portion.

The facility that should be implemnted by concrete implementation for consequent data fetching on this reader client demand. May be called at any time when internal buffer is near to empty.

Parameters:
requested - the number of byte that were requested.
Returns:
the number of bytes that were actually readed into internal buffer.
Throws:
IOException - If unexpected I/O error happends.

compact

protected abstract void compact(int from,
                                int requested)
                         throws IOException
Compacts the data in the internal buffer.

Provides the efficient heap using. It is called any time when internal buffer is near to filled and may be compacted due current limit values.

Parameters:
from - The pointer in buffer up to which data may be marked as invalidated.
requested - the number of bytes that should stay in buffer in any way.
Throws:
IOException - If unexpected I/O error happends.

eof

public final boolean eof()

ensureOpen

protected final void ensureOpen(int length)
                         throws IOException
Check to make sure that this reader has enough data to read the length bytes.

Parameters:
length - the length of data that should be available.
Throws:
IOException - If underlied stream is corrupted.

updateLocator

protected void updateLocator(int skipped)
Updates the location with specified changes of position.

Parameters:
skipped - the number of bytes on which location has been moved.


C24 Technologies © 2002-2012: All Rights Reserved.