eneter.messaging.dataprocessing.streaming
Class DynamicInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by eneter.messaging.dataprocessing.streaming.DynamicInputStream
All Implemented Interfaces:
java.io.Closeable

public class DynamicInputStream
extends java.io.InputStream

Stream which can be written and read at the same time. The dynamic stream supports writing of data by one thread and reading by another. The reading operation is blocked until the requested amount of data is not available.


Constructor Summary
DynamicInputStream()
           
 
Method Summary
 void close()
          Closes the stream and releases the reading thread waiting for data.
 boolean getBlockingMode()
          Gets the blocking mode.
 int read()
          Reads one byte from the stream.
 int read(byte[] buffer, int offset, int count)
          Reads data from the stream to the specified buffer.
 void setBlockingMode(boolean isBlocking)
          Gets the blocking mode.
 void write(byte[] buffer, int offset, int count)
          Writes the data to the stream.
 void writeWithoutCopying(byte[] data, int offset, int count)
          Writes data to the stream the way that it just stores the reference to the input data.
 
Methods inherited from class java.io.InputStream
available, mark, markSupported, read, reset, skip
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DynamicInputStream

public DynamicInputStream()
Method Detail

setBlockingMode

public void setBlockingMode(boolean isBlocking)
Gets the blocking mode. If blocking mode then Read method blocks until data is available.

Parameters:
isBlocking - true if the reading shall be blocking until data is available.

getBlockingMode

public boolean getBlockingMode()
Gets the blocking mode. If blocking mode then Read method blocks until data is available.

Returns:
true if the reading is blocked until required amount of data is available.

read

public int read()
         throws java.io.IOException
Reads one byte from the stream. If the requested byte is not available the thread is blocked until byte is put to the stream = from another thread.

Specified by:
read in class java.io.InputStream
Throws:
java.io.IOException

read

public int read(byte[] buffer,
                int offset,
                int count)
         throws java.io.IOException
Reads data from the stream to the specified buffer. If the requested amount of data is not available the thread is blocked until required amount of data is available - until data is written by another thread.

Overrides:
read in class java.io.InputStream
Throws:
java.io.IOException

write

public void write(byte[] buffer,
                  int offset,
                  int count)
Writes the data to the stream.

Parameters:
buffer - Buffer to be written to the stream
offset - Starting podition in the buffer from where data will be read.
count - Amount of data to be read from the buffer and written to the stream.

writeWithoutCopying

public void writeWithoutCopying(byte[] data,
                                int offset,
                                int count)
Writes data to the stream the way that it just stores the reference to the input data. It does not copy the incoming data to the stream but instead of that it just stores the reference. This approach is very fast but the input byte[] array should not be modified after calling this method.

Parameters:
data - data to be written to the stream.
offset - Starting position in the buffer from where data will be read.
count - Amount of data to be read from the buffer and written to the stream.

close

public void close()
Closes the stream and releases the reading thread waiting for data.

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.InputStream