jsync
Class Conveyer

java.lang.Object
  extended byjsync.Conveyer

public abstract class Conveyer
extends java.lang.Object

Class for parallel input data reading and processing. Two concurrent threads are started: one of them reads data from input stream in cyclic buffer and second thread performs processing of data, previously placed in another part of the buffer.

Processing of data is performed by overridden method process() of class derived from Conveyer. Processing of data continues until end of stream is reached or process() method returns false.


Constructor Summary
Conveyer(java.io.InputStream stream, int blockSize)
          Create Conveyer object with specified block size.
Conveyer(java.io.InputStream stream, int blockSize, int bufferSize)
          Create Conveyer object with specified size of cyclic buffer and block size.
 
Method Summary
abstract  boolean process(byte[] buffer, int offset, int length)
          Abstract method to be implemented in derived class.
 void setInputStream(java.io.InputStream stream)
          Set new input stream for conveyer.
 void start()
          Start execution of reading and processing threads.
 boolean waitTermination()
          Wait termination of data processing thread.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Conveyer

public Conveyer(java.io.InputStream stream,
                int blockSize,
                int bufferSize)
Create Conveyer object with specified size of cyclic buffer and block size.

Parameters:
stream - input stream from which data is extracted
blockSize - size of block for reading and processing data. Read and data processing operation will not take more than blockSize bytes of data.
bufferSize - sizeof of cyclic buffer. Should be not less than blockSize.

Conveyer

public Conveyer(java.io.InputStream stream,
                int blockSize)
Create Conveyer object with specified block size. Size of buffer is set by multiplying block size by two. So read and process operations can be executed in parallel for different parts of the buffer.

Parameters:
stream - input stream from which data is extracted
blockSize - size of block for reading and processing data. Input stream read and data processing operation will not take more than blockSize bytes of data.
Method Detail

setInputStream

public void setInputStream(java.io.InputStream stream)
Set new input stream for conveyer.

Parameters:
stream - new input stream

start

public void start()
Start execution of reading and processing threads. This method doesn't wait termination of these threads.


waitTermination

public boolean waitTermination()
Wait termination of data processing thread. Reading thread is terminated before data processing thread.

Returns:
true if all data from input stream is processed, false if processing of data is interrupted because process() method returns false.

process

public abstract boolean process(byte[] buffer,
                                int offset,
                                int length)
Abstract method to be implemented in derived class. This method should perform processing of data placed in specified segment of cyclic buffer.

Parameters:
buffer - cyclic buffer maintained by Conveyer class
offset - position in buffer of data to be processed
length - number of bytes of data available for processing. Value of this parameter never exceeds blockSize.
Returns:
if this method returns false processing of data is finished, and both threads are stopped.