|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectdatasource.DataSource
public abstract class DataSource
DataSource: abstract base class for components that provide data through read() calls by their client.
A DataSource contains a thread of its own. When created, the thread is started, and goes into a loop reading data from its source and putting into a buffer. A client thread call to the read() method takes data out of the buffer, if there is any, and blocks until there is some if the buffer is currently empty. Thre read() interface operates on bytes -- the client asks for some maximum number of bytes. However, reading data a byte at a time from data sources (like files or network connections) is slow. Instead, the DataSource wants to read bigger chunks for its own source. For that reason, the internal data buffer kept by a DataSource is an array of DataRecords. Each DataRecord is a byte[], of some reasonable size. The read thread in the DataSource tries to fetch a full DataRecord from its data source whenever it does a read.
Nested Class Summary | |
---|---|
private class |
DataSource.ReceiveHelper
Helper class to implement reading thread. |
Field Summary | |
---|---|
private static int |
BUFFER_SIZE
Size of dataBuffer; |
private java.util.concurrent.Semaphore |
bufferSemaphore
A semaphore to coordinate access to the dataBuffer. |
static int |
CLOSED
|
static int |
CONNECTED
|
private java.util.concurrent.ArrayBlockingQueue<DataRecord> |
dataBuffer
The data buffer between the local, reader thread and the client thread. |
private java.lang.Thread |
reader
The reader thread (fetches data from this object's source and puts it in dataBuffer). |
private static int |
RECORD_SIZE
Size of each DataRecord in the buffer. |
private DataRecordManager |
recordManager
Object that knows how to create/cache DataRecords. |
static int |
STARTING
We need to keep track of what state the input source is in. |
protected int |
state
|
Constructor Summary | |
---|---|
protected |
DataSource()
Creates a new DataSource. |
Method Summary | |
---|---|
int |
read(byte[] buf,
int maxLength)
Reads up to maxLength bytes and returns them via argument buf, starting at position 0. |
protected abstract int |
readFromSrc(byte[] buf,
int len)
The ReceiveHelper implemented here depends on this abstract method being implemented by the instantiated class. |
void |
readyToRead()
Function to initialize (called by derived class constructor). |
abstract void |
reInit()
All derived classes must be capable of reinitializing themselves. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private java.util.concurrent.ArrayBlockingQueue<DataRecord> dataBuffer
private java.util.concurrent.Semaphore bufferSemaphore
private DataRecordManager recordManager
private static final int BUFFER_SIZE
private static final int RECORD_SIZE
private java.lang.Thread reader
public static final int STARTING
public static final int CONNECTED
public static final int CLOSED
protected int state
Constructor Detail |
---|
protected DataSource()
Method Detail |
---|
protected abstract int readFromSrc(byte[] buf, int len) throws java.io.IOException
java.io.IOException
public abstract void reInit() throws java.io.IOException, java.io.FileNotFoundException
java.io.IOException
java.io.FileNotFoundException
public void readyToRead()
public int read(byte[] buf, int maxLength) throws java.io.IOException, java.lang.InterruptedException
java.io.IOException
java.lang.InterruptedException
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |