com.davisor.data.stream
Class StreamDataSource

java.lang.Object
  extended bycom.davisor.data.VoidData
      extended bycom.davisor.data.stream.StreamDataSource
All Implemented Interfaces:
Data, DataBuffer, DataSource
Direct Known Subclasses:
ChartData

public class StreamDataSource
extends VoidData
implements DataBuffer

A StreamDataSource makes a reuseable data source out of a data stream that can be read only once. The data stream is therefore consumed entirely, and the data is stored in a buffer from which it can be read multiple times.

The exact moment, when the source data stream is consumed, can be controlled during the stream data source construction time. The stream can be consumed immediately at construction time, or the buffer creation can be delayed until explicitly needed. If the buffer construction is delayed, the original, unbuffered stream remains available for reading. In this case the stream can, however, be read only once.

Delayed stream buffering is useful when subclasses need to modify the original stream content before it is buffered. The subclasses need only override the getBuffer method, or provide some other way to alter the stream content before it is read and buffered.

Since:
JDK1.1
See Also:
DataBuffer, DataStream, Type

Field Summary
protected  DataBuffer M_buffer
          Data source buffer (re-readable, may initially be null).
protected  DataStream M_stream
          Data source stream (readable only once, null when unavailable).
 
Fields inherited from class com.davisor.data.VoidData
M_type
 
Constructor Summary
  StreamDataSource(DataBuffer buffer)
          Creates a stream data stream source out of the given buffer.
  StreamDataSource(DataStream stream)
          Creates a stream data stream source out of the given stream.
  StreamDataSource(DataStream stream, boolean buffer)
          Creates a stream data stream source out of the given stream.
protected StreamDataSource(StreamDataSource sds)
          Shallow copy constructor.
 
Method Summary
 boolean close()
          This method frees resources for cargabe collection.
 DataBuffer getBuffer()
          Gets the underlying data buffer.
 DataBuffer getBuffer(Type type)
          Gets the underlying data buffer.
 DataValue getData(int index)
          Gets the specific data element from the data buffer, if available.
 int getSize()
          Gets the number of data elements in the data buffer, if available.
 DataStream getStream()
          Gets a data stream from the source.
 DataStream getStream(boolean reversed)
          Gets a new data stream from the data buffer, if available.
 boolean isBuffered()
          Tests if the stream has been buffered.
 void setBuffer(DataBuffer buffer)
          Sets the current data buffer.
 void setStream(DataStream stream)
          Sets the current data stream.
 java.lang.String toString()
          Gets the data source string representation.
 java.lang.String toString(java.lang.String id, java.lang.String dataName)
          See the DataSource documentation
 
Methods inherited from class com.davisor.data.VoidData
equals, getDataValue, getType, hashCode, setType
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface com.davisor.data.Data
getType
 

Field Detail

M_buffer

protected DataBuffer M_buffer
Data source buffer (re-readable, may initially be null).


M_stream

protected DataStream M_stream
Data source stream (readable only once, null when unavailable).

Constructor Detail

StreamDataSource

protected StreamDataSource(StreamDataSource sds)
                    throws InvalidDataException
Shallow copy constructor. The new object will share the data buffer with the original object. Furthermore, the original object will consume it's source stream at this time, unless it has not already done so.

Parameters:
sds - source stream data source (may be null)
Throws:
InvalidDataException - if the stream contains invalid data
See Also:
getBuffer()

StreamDataSource

public StreamDataSource(DataStream stream)
                 throws InvalidDataException
Creates a stream data stream source out of the given stream. The stream is not consumed nor buffered at this time.

Throws:
InvalidDataException - if data strem type resolvation fails
See Also:
getBuffer()

StreamDataSource

public StreamDataSource(DataStream stream,
                        boolean buffer)
                 throws InvalidDataException
Creates a stream data stream source out of the given stream. The stream may optionally be consumed and stored in a re-readable buffer. In that case, the stream is consumed immediately, and it's content is placed in a new buffer.

Throws:
InvalidDataException - if the stream contains invalid data

StreamDataSource

public StreamDataSource(DataBuffer buffer)
                 throws InvalidDataException
Creates a stream data stream source out of the given buffer.

Throws:
InvalidDataException - if data buffer type resolvation fails
Method Detail

getData

public DataValue getData(int index)
                  throws com.davisor.core.InvalidIndexException
Gets the specific data element from the data buffer, if available. If no data buffer is available, null is returned. If buffer is available, but requested element is not, NotFoundException is thrown.

Specified by:
getData in interface DataBuffer
Parameters:
index - data element index
Throws:
com.davisor.core.InvalidIndexException - if no data is bound to given index

getSize

public int getSize()
Gets the number of data elements in the data buffer, if available. If no data buffer is available, -1 is returned.

Specified by:
getSize in interface DataBuffer

getStream

public DataStream getStream(boolean reversed)
                     throws InvalidDataException
Gets a new data stream from the data buffer, if available.

Specified by:
getStream in interface DataBuffer
Parameters:
reversed - controls if stream reads buffer forwards or backwards
Throws:
NullDataException - if no data buffer was available
InvalidDataException - if a new stream can not be created

close

public boolean close()
This method frees resources for cargabe collection.

Specified by:
close in interface DataSource
Returns:
true

getStream

public DataStream getStream()
                     throws InvalidDataException
Gets a data stream from the source. If the stream is not buffered, the stream can be fetched and consumed only once.

Specified by:
getStream in interface DataSource
Throws:
NullDataException - if there is no stream or buffer.
InvalidDataException

toString

public java.lang.String toString()
Gets the data source string representation. If the source is buffered, it's entire content is returned enclosed in "()". If the source is not buffered, the source is not consumed for mere displaying purposes. Instead, the stream data type string representation is returned.

Overrides:
toString in class VoidData

getBuffer

public DataBuffer getBuffer()
                     throws InvalidDataException
Gets the underlying data buffer. If the stream has not yet been buffered, or requested, a new buffer is created and the current data stream is consumed and stored in it.

Throws:
InvalidDataException - if the stream contains invalid data

getBuffer

public DataBuffer getBuffer(Type type)
                     throws InvalidDataException
Gets the underlying data buffer. If the stream has not yet been buffered, or if the requested buffer type does not match the current buffer type, then a new buffer is created and the current data stream is consumed and rearranged to match the given type.

Throws:
InvalidDataException - if the stream contains invalid data

isBuffered

public boolean isBuffered()
Tests if the stream has been buffered.


setBuffer

public void setBuffer(DataBuffer buffer)
Sets the current data buffer.


setStream

public void setStream(DataStream stream)
Sets the current data stream.


toString

public java.lang.String toString(java.lang.String id,
                                 java.lang.String dataName)
See the DataSource documentation

Specified by:
toString in interface DataSource


Copyright © 2001-2004 Davisor Oy. All Rights Reserved.