Errai 3.0.1-SNAPSHOT

org.jboss.errai.bus.server.io.buffers
Class TransmissionBuffer

java.lang.Object
  extended by org.jboss.errai.bus.server.io.buffers.TransmissionBuffer
All Implemented Interfaces:
Buffer

public class TransmissionBuffer
extends Object
implements Buffer

A ring-based buffer implementation that provides contention-free writing of 1..n colors. In this case, colors refer to the unique attribute that separates one topic of data from another. Global data, which is visible to all topics may also be written to this buffer.

Given the ring-buffer design, data is never de-allocated from the buffer when it is no longer needed. Instead, it is assumed that old data will be consumed from the buffer before that space in the buffer is reclaimed.

Since this is a multi-colored buffer, it has multiple tails -- one for each color.

The amount of free space available in the buffer at any time is the delta between the head and maximum physical extent of the buffer, plus the delta from the beginning of the physical buffer in memory to the closest tail.

Since:
Errai v2.0
Author:
Mike Brock
See Also:
BufferColor, BufferFilter

Field Summary
static int DEFAULT_SEGMENT_SIZE
           
static long STARTING_SEQUENCE
           
 
Method Summary
 void clear()
          Clear the current buffer.
static TransmissionBuffer create()
          Creates a transmission buffer with the default segment and buffer size, using a regular heap allocated buffer.
static TransmissionBuffer create(int segmentSize, int segments)
          Creates a heap allocated transmission buffer with a specified segment size and segments.
static TransmissionBuffer createDirect()
          Creates a transmission buffer with the default segment and buffer size, using a direct memory buffer.
static TransmissionBuffer createDirect(int segmentSize, int segments)
          Creates a direct allocated transmission buffer with a custom segment size and segments.
 void dumpSegments(PrintWriter writer)
           
 List<String> dumpSegmentsAsList()
           
 int getBufferSize()
           
 int getHeadPositionBytes()
           
 long getHeadSequence()
           
 int getSegmentSize()
           
 int getTotalSegments()
           
 boolean read(ByteWriteAdapter outputStream, BufferColor bufferColor)
          Reads all the available data of the specified color from the buffer into the provided OutputStream
 boolean read(ByteWriteAdapter outputStream, BufferColor bufferColor, BufferFilter callback)
          Reads all the available data of the specified color from the buffer into the provided OutputStream with a provided BufferFilter.
 boolean read(ByteWriteAdapter outputStream, BufferColor bufferColor, BufferFilter callback, long sequence)
          Reads all the available data of the specified color from the buffer into the provided OutputStream with a provided BufferFilter.
 boolean readWait(ByteWriteAdapter outputStream, BufferColor bufferColor)
          Reads from the buffer into the provided OutputStream, waiting indefinitely for data to arrive that is relavent to the specified BufferColor
 boolean readWait(ByteWriteAdapter outputStream, BufferColor bufferColor, BufferFilter callback)
          Reads from the buffer into the provided OutputStream, waiting indefinitely for data of the specified color to become available.
 boolean readWait(TimeUnit unit, long time, ByteWriteAdapter outputStream, BufferColor bufferColor)
          Reads from the buffer into the provided OutputStream, waiting up to the specified wait time for data of the specified color to become available.
 boolean readWait(TimeUnit unit, long time, ByteWriteAdapter outputStream, BufferColor bufferColor, BufferFilter callback)
          Reads from the buffer into the provided OutputStream, waiting indefinitely for data of the specified color to become available with the provided callback.
 void write(InputStream inputStream, BufferColor bufferColor)
          Writes from the InputStream into the buffer.
 void write(int writeSize, InputStream inputStream, BufferColor bufferColor)
          Writes from an InputStream into the buffer using the specified to allocate space in the buffer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

STARTING_SEQUENCE

public static final long STARTING_SEQUENCE
See Also:
Constant Field Values

DEFAULT_SEGMENT_SIZE

public static final int DEFAULT_SEGMENT_SIZE
See Also:
Constant Field Values
Method Detail

create

public static TransmissionBuffer create()
Creates a transmission buffer with the default segment and buffer size, using a regular heap allocated buffer.

Returns:
an instance of the transmission buffer.

createDirect

public static TransmissionBuffer createDirect()
Creates a transmission buffer with the default segment and buffer size, using a direct memory buffer.

Returns:
an instance of the tranmission buffer.

create

public static TransmissionBuffer create(int segmentSize,
                                        int segments)
Creates a heap allocated transmission buffer with a specified segment size and segments. The resulting buffer will be of size: segmentSize * segments.

Parameters:
segmentSize - the size of individual segments
segments - the total number of segments
Returns:
an instance of the transmission buffer

createDirect

public static TransmissionBuffer createDirect(int segmentSize,
                                              int segments)
Creates a direct allocated transmission buffer with a custom segment size and segments. The resulting buffer will be of size: segmentSize * segments.

Parameters:
segmentSize - the size of the individual segments
segments - the total number of segments
Returns:
an instance of the transmission buffer

write

public void write(InputStream inputStream,
                  BufferColor bufferColor)
           throws IOException
Writes from the InputStream into the buffer. Space is allocated and the data expected to be written by checking the InputStream.available() value.

Specified by:
write in interface Buffer
Parameters:
inputStream - the input stream to read into the buffer.
bufferColor - the color of the data to be inserted.
Throws:
IOException

write

public void write(int writeSize,
                  InputStream inputStream,
                  BufferColor bufferColor)
           throws IOException
Writes from an InputStream into the buffer using the specified to allocate space in the buffer.

Specified by:
write in interface Buffer
Parameters:
writeSize - the size in bytes to be allocated.
inputStream - the input stream to read into the buffer.
bufferColor - the color of the data to be inserted.
Throws:
IOException

read

public boolean read(ByteWriteAdapter outputStream,
                    BufferColor bufferColor)
             throws IOException
Reads all the available data of the specified color from the buffer into the provided OutputStream

Specified by:
read in interface Buffer
Parameters:
outputStream - the OutputStream to read into.
bufferColor - the buffer color
Returns:
returns a boolean indicating whether or not the cursor advanced.
Throws:
IOException

read

public boolean read(ByteWriteAdapter outputStream,
                    BufferColor bufferColor,
                    BufferFilter callback)
             throws IOException
Reads all the available data of the specified color from the buffer into the provided OutputStream with a provided BufferFilter.

Specified by:
read in interface Buffer
Parameters:
outputStream - the OutputStream to read into.
bufferColor - the buffer color
callback - a callback to be used during the read operation.
Returns:
returns a boolean indicating whether or not the cursor advanced.
Throws:
IOException

read

public boolean read(ByteWriteAdapter outputStream,
                    BufferColor bufferColor,
                    BufferFilter callback,
                    long sequence)
             throws IOException
Reads all the available data of the specified color from the buffer into the provided OutputStream with a provided BufferFilter.

Specified by:
read in interface Buffer
Parameters:
outputStream - the OutputStream to read into.
bufferColor - the buffer color.
callback - a callback to be used during the read operation.
sequence - the sequence number to seek from in the buffer.
Returns:
returns a boolean indicating whether or not the cursor advanced.
Throws:
IOException

readWait

public boolean readWait(ByteWriteAdapter outputStream,
                        BufferColor bufferColor)
                 throws InterruptedException,
                        IOException
Reads from the buffer into the provided OutputStream, waiting indefinitely for data to arrive that is relavent to the specified BufferColor

Specified by:
readWait in interface Buffer
Parameters:
outputStream - the OutputStream to read into.
bufferColor - the buffer color
Returns:
returns a boolean indicating whether or not the cursor advanced.
Throws:
IOException - an IOException is thrown if there is an inability to read from the buffer or write to the specified OuputStream
InterruptedException - thrown if the monitor is interrupted while waiting to receive dta.

readWait

public boolean readWait(TimeUnit unit,
                        long time,
                        ByteWriteAdapter outputStream,
                        BufferColor bufferColor)
                 throws IOException,
                        InterruptedException
Reads from the buffer into the provided OutputStream, waiting up to the specified wait time for data of the specified color to become available. Otherwise, the method returns without error, having read nothing.

Specified by:
readWait in interface Buffer
Parameters:
unit - the unit of time that will be used as the basis for waiting
time - the amount of time to wait in the specified units
outputStream - the OutputStream to write to.
bufferColor - the buffer color
Returns:
returns a boolean indicating whether or not the cursor advanced.
Throws:
IOException - an IOException is thrown if there is an inability to read from the buffer or write to the specified OuputStream
InterruptedException - thrown if the monitor is interrupted while waiting to receive dta.

readWait

public boolean readWait(ByteWriteAdapter outputStream,
                        BufferColor bufferColor,
                        BufferFilter callback)
                 throws IOException,
                        InterruptedException
Reads from the buffer into the provided OutputStream, waiting indefinitely for data of the specified color to become available. Otherwise, the method returns without error, having read nothing.

Specified by:
readWait in interface Buffer
Parameters:
outputStream - the OutputStream to write to.
bufferColor - the buffer color
Returns:
returns a boolean indicating whether or not the cursor advanced.
Throws:
IOException - an IOException is thrown if there is an inability to read from the buffer or write to the specified OutputStream
InterruptedException - thrown if the monitor is interrupted while waiting to receive dta.

readWait

public boolean readWait(TimeUnit unit,
                        long time,
                        ByteWriteAdapter outputStream,
                        BufferColor bufferColor,
                        BufferFilter callback)
                 throws IOException,
                        InterruptedException
Reads from the buffer into the provided OutputStream, waiting indefinitely for data of the specified color to become available with the provided callback. Otherwise, the method returns without error, having read nothing.

Specified by:
readWait in interface Buffer
Parameters:
outputStream - the OutputStream to write to.
bufferColor - the buffer color
Returns:
returns a boolean indicating whether or not the cursor advanced.
Throws:
IOException - an IOException is thrown if there is an inability to read from the buffer or write to the specified OutputStream
InterruptedException - thrown if the monitor is interrupted while waiting to receive dta.

getHeadSequence

public long getHeadSequence()
Specified by:
getHeadSequence in interface Buffer

getHeadPositionBytes

public int getHeadPositionBytes()
Specified by:
getHeadPositionBytes in interface Buffer

getBufferSize

public int getBufferSize()
Specified by:
getBufferSize in interface Buffer

getTotalSegments

public int getTotalSegments()
Specified by:
getTotalSegments in interface Buffer

getSegmentSize

public int getSegmentSize()
Specified by:
getSegmentSize in interface Buffer

clear

public void clear()
Clear the current buffer.


dumpSegments

public void dumpSegments(PrintWriter writer)

dumpSegmentsAsList

public List<String> dumpSegmentsAsList()

Errai 3.0.1-SNAPSHOT

Copyright © 2013-2014 JBoss, a division of Red Hat. All Rights Reserved.