cnrg.itx.datax
Class ChannelBuffer

java.lang.Object
  |
  +--cnrg.itx.datax.Channel
        |
        +--cnrg.itx.datax.ChannelBuffer

class ChannelBuffer
extends Channel

Looks like a channel, actually just a buffer. These are returned on a call to MixerChannel.getNewInput(). They contain all of the channel functionality that a source expects, and buffer data to be read out by the mixer.


Inner Class Summary
private  class ChannelBuffer.BigPipedInputStream
           
 
Field Summary
private  int behind
          The number of bytes our reader dropped from us that we need to eliminate
private  int BUFFER_SIZE
          The size of the internal buffer.
private  long firstPushTime
          The time when the first push was executed
private  int iMyInstance
          Instance number of the current channelbuffer; used for stat output numbering.
(package private) static int instances
          Number of instances of the channelbuffer; used for statistics output numbering.
private  boolean isDelinquent
          True if the last attempt to read from this channel met an empty buffer
private  int MAX_CATCHUP
          How many consecutive bytes are we willing to cull from a sample to try to catch up
private  java.io.PipedInputStream pis
          The input stream used to buffer the data.
private  java.io.PipedOutputStream pos
          The output stream used for buffering data.
private  java.io.PrintWriter pw
           
private  int SAMPLE_RATE
          Samples/second
private  boolean started
          Has our source started sending yet?
private  int totalRead
          The total number of bytes read out of the buffer
 
Fields inherited from class cnrg.itx.datax.Channel
closed, destinations, INPUT, mode, OTHER, OUTPUT, pipedInput, pipedOutput, readerThread, running, SAMPLE_SIZE, sampleSize, source
 
Constructor Summary
ChannelBuffer()
          Creates a new ChannelBuffer.
 
Method Summary
 int available()
          Returns the number of bytes left in the buffer.
 void close()
          Closes the BufferChannel.
 void fellBehind(int by)
          lets the reading channel inform the bufferChannel that it dropped bytes when our buffer was empty.
 boolean getDelinquent()
          Gets the contents of isDelinquent.
 long getEarlyTime()
          Returns the number of ms early we are.
 Stats getStatistics()
          Returns the statistics for this ChannelBuffer, and its destination.
 boolean hasStarted()
          Returns true if the source has sent data to this channel.
 void open()
          Starts the channel's source
 void push(byte[] b)
          Method for the source to feed data to the BufferChannel.
 int read()
          Method for the mixer to read data out of the buffer.
 int read(byte[] b)
          Method for the mixer to read data out of the buffer.
 int read(byte[] b, int p1, int p2)
          Method for the mixer to read data out of the buffer.
 void setDelinquent(boolean delinq)
          Sets the contents of isDelinquent.
 
Methods inherited from class cnrg.itx.datax.Channel
addDestination, getMode, getProperties, getSource, mute, removeAllDestinations, removeDestination, run, setMode, setPeerProperties, setProperties, setSource
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

started

private boolean started
Has our source started sending yet?

firstPushTime

private long firstPushTime
The time when the first push was executed

totalRead

private int totalRead
The total number of bytes read out of the buffer

behind

private int behind
The number of bytes our reader dropped from us that we need to eliminate

pis

private java.io.PipedInputStream pis
The input stream used to buffer the data.

pos

private java.io.PipedOutputStream pos
The output stream used for buffering data.

BUFFER_SIZE

private int BUFFER_SIZE
The size of the internal buffer. Currently, BigPipedInputStream has a buffer of 6400 bytes. We can use up to this amount.

SAMPLE_RATE

private int SAMPLE_RATE
Samples/second

MAX_CATCHUP

private int MAX_CATCHUP
How many consecutive bytes are we willing to cull from a sample to try to catch up

isDelinquent

private boolean isDelinquent
True if the last attempt to read from this channel met an empty buffer

instances

static int instances
Number of instances of the channelbuffer; used for statistics output numbering.

iMyInstance

private int iMyInstance
Instance number of the current channelbuffer; used for stat output numbering.

pw

private java.io.PrintWriter pw
Constructor Detail

ChannelBuffer

public ChannelBuffer()
Creates a new ChannelBuffer. Allocates the internal buffer.
Method Detail

open

public void open()
Starts the channel's source
Overrides:
open in class Channel

push

public void push(byte[] b)
Method for the source to feed data to the BufferChannel. When the buffer runs out of space for a new push() we throw away just enough old data to make room for the new.
Parameters:
b - the byte array to be buffered
Overrides:
push in class Channel

read

public int read()
         throws java.io.IOException
Method for the mixer to read data out of the buffer. Reads out only one byte

read

public int read(byte[] b)
         throws java.io.IOException
Method for the mixer to read data out of the buffer. Trys to fill the given byte array with data from the buffer.
Parameters:
b - the buffer to grab data into

read

public int read(byte[] b,
                int p1,
                int p2)
         throws java.io.IOException
Method for the mixer to read data out of the buffer.
Parameters:
b - The array where the data will go
p1 - the starting index in b
p2 - the number of bytes to read
See Also:
java.io.PipedInputStream.read

available

public int available()
              throws java.io.IOException
Returns the number of bytes left in the buffer.
Throws:
java.io.IOException - thrown when the PipedOutputStream that forms the buffer is broken.

close

public void close()
Closes the BufferChannel. Closes the channel's source and buffer.
Overrides:
close in class Channel

getDelinquent

public boolean getDelinquent()
Gets the contents of isDelinquent.
Returns:
boolean the current state of isDelinquent.

setDelinquent

public void setDelinquent(boolean delinq)
Sets the contents of isDelinquent. This is called with true whenever we need to read, but the buffer does not have enough data.
Parameters:
delinq - the value setDelinquent will set isDelinquent to.

hasStarted

public boolean hasStarted()
Returns true if the source has sent data to this channel.

getEarlyTime

public long getEarlyTime()
Returns the number of ms early we are. If this is negative, we are late. This method assumes that we are late if our samples read by read() method/time elapsed is < SAMPLE_RATE.

fellBehind

public void fellBehind(int by)
lets the reading channel inform the bufferChannel that it dropped bytes when our buffer was empty.

getStatistics

public Stats getStatistics()
Returns the statistics for this ChannelBuffer, and its destination.
Returns:
Stats the stats for this ChannelBuffer -- currently, the number of bytes in the buffer.
Overrides:
getStatistics in class Channel