FileDocCategorySizeDatePackage
CircularBuffer.javaAPI DocJMF 2.1.1e4398Mon May 12 12:20:48 BST 2003com.sun.media

CircularBuffer

public class CircularBuffer extends Object
CircularQueue It implements a circular FIFO queue of references to Objects.

Fields Summary
private Buffer[]
buf
buffer to hold the references. Would be changed to buffer array, when Buffer stabalize
private int
head
write pointer
private int
tail
read pointer
private int
availableFramesForReading
number of frames that can be read
private int
availableFramesForWriting
number of frames that can be written
private int
lockedFramesForReading
number of frames that were checked out for reading
private int
lockedFramesForWriting
number of frames that were checked out for writing
private int
size
size of the FIFO queue
Constructors Summary
public CircularBuffer(int n)
create a queue with max number of elements

param
n the max number of elements

        size = n;
	buf = new Buffer[n];
	for (int i = 0;i < n;i++)
	    buf[i] = new ExtBuffer(); // *** generalize this
	reset();
    
Methods Summary
public synchronized booleancanRead()
returns true if read() would succeed

        return (availableFramesForReading > 0);
    
public synchronized booleancanWrite()
returns true if getEmptyObject() would succeed

        return (availableFramesForWriting>0);
    
public voiderror()
error dump

        throw new RuntimeException ("CircularQueue failure:\n head="+head+"\n tail="+tail+
                                    "\n canRead="+availableFramesForReading+"\n canWrite="+availableFramesForWriting+
                                    "\n lockedRead="+lockedFramesForReading+"\n lockedWrite="+lockedFramesForWriting);
    
public synchronized javax.media.BuffergetEmptyBuffer()
returns empty buffer object to put data in

	if (availableFramesForWriting == 0)
	    error();
	
	lockedFramesForWriting++;
	Buffer buffer = buf[tail];
	availableFramesForWriting--;
	tail++;
	if (tail >= size)
	    tail -= size;
	
	return buffer;
    
public synchronized booleanlockedRead()

	return (lockedFramesForReading > 0);
    
public synchronized booleanlockedWrite()

	return (lockedFramesForWriting > 0);
    
public synchronized javax.media.Bufferpeek()
Gets frame with valid buffer from the queue

	if (availableFramesForReading == 0)
	    error();
	
	return buf[head];
    
public voidprint()

        System.err.println("CircularQueue : head="+head+" tail="+tail+
                                    " canRead="+availableFramesForReading+" canWrite="+availableFramesForWriting+
                                    " lockedRead="+lockedFramesForReading+" lockedWrite="+lockedFramesForWriting);
    
public synchronized javax.media.Bufferread()
Gets frame with valid buffer from the queue

	if (availableFramesForReading == 0)
	    error();

	Buffer buffer = buf[head];
	lockedFramesForReading++;
	availableFramesForReading--;
	head++;
	if (head >= size)
	    head -= size;
	
	return buffer;
    
public synchronized voidreadReport()
indicates that the latest read frame is no longer in use

        if (lockedFramesForReading == 0)
	    error();
	
        lockedFramesForReading--;
        availableFramesForWriting++;
    
public synchronized voidreset()
reset the queue

        availableFramesForReading = 0;
        availableFramesForWriting = size;
        lockedFramesForReading = 0;
        lockedFramesForWriting = 0;
        head = 0;
        tail = 0;
    
public synchronized voidwriteReport()
indicates that latest Object returns to the queue
we removed writeReport(false), since we have the mechanism in javax.media.Buffer (discard).

        if (lockedFramesForWriting == 0)
	    error();
	
        lockedFramesForWriting--;
        availableFramesForReading++;  // wrote data OK
        /*
	  @@@ buffer invalid: removed since we have the mechanism in javax.media.Buffer
	  {
	  availableFramesForWriting++; // buffer not written
	  tail--;
	  if (tail<0)
	  tail += size;
	  }
        */