edu.rice.cs.plt.io
Class ExpandingCharBuffer

java.lang.Object
  extended by edu.rice.cs.plt.io.ExpandingBuffer<char[]>
      extended by edu.rice.cs.plt.io.ExpandingCharBuffer
All Implemented Interfaces:
java.io.Serializable

public class ExpandingCharBuffer
extends ExpandingBuffer<char[]>

A character buffer of arbitrary size to be used with Readers and Writers. The buffer is a FIFO queue of characters. It provides a DirectWriter for adding characters to the end and a DirectReader for pulling characters from the front. This allows behavior similar to that of a PipedWriter and PipedReader, but without any assumptions about access from different threads, without any restrictions on the size of the buffer (so writing will never block), and with support for multiple readers or writers connected to the same source. (If access is restricted to a single thread, care must be taken to never read when the buffer is empty.)

While an attempt at thread safety has been made, at least one exception is evident: if the result of writer() attempts to write from the result of reader(), and the reader blocks, a write from another thread will be necessary to unblock the reader. At that point, the original write() will have already instructed the reader to copy its data into an incorrect location. In general, connecting a reader and a writer from the same buffer is not recommended.

See Also:
Serialized Form

Field Summary
 
Fields inherited from class edu.rice.cs.plt.io.ExpandingBuffer
BUFFER_SIZE
 
Constructor Summary
ExpandingCharBuffer()
           
 
Method Summary
protected  char[] allocateBuffer(int size)
          Create a fixed-size sub-buffer
 void end()
          Place an "end of file" at the end of the buffer.
 boolean isEnded()
           
 DirectReader reader()
          Create a reader providing read access to the buffer.
 DirectWriter writer()
          Create a writer providing write access to the buffer.
 
Methods inherited from class edu.rice.cs.plt.io.ExpandingBuffer
allocate, deallocate, elementsInFirstBuffer, firstBuffer, firstIndex, isEmpty, lastBuffer, lastIndex, recordRead, recordWrite, size
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ExpandingCharBuffer

public ExpandingCharBuffer()
Method Detail

end

public void end()
Place an "end of file" at the end of the buffer. No further writes will be allowed, and when the buffer is emptied, reads will see an end of file.


isEnded

public boolean isEnded()

allocateBuffer

protected char[] allocateBuffer(int size)
Description copied from class: ExpandingBuffer
Create a fixed-size sub-buffer

Specified by:
allocateBuffer in class ExpandingBuffer<char[]>

writer

public DirectWriter writer()
Create a writer providing write access to the buffer. Invocations of write will atomically add characters directly to the buffer. Writer.close() will have no effect.


reader

public DirectReader reader()
Create a reader providing read access to the buffer. Invocations of read will atomically remove characters from the buffer. Reader.close() will have no effect.