edu.rice.cs.plt.io
Class ExpandingByteBuffer

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

public class ExpandingByteBuffer
extends ExpandingBuffer<byte[]>

A byte buffer of arbitrary size to be used with InputStreams and OutputStreams. The buffer is a FIFO queue of bytes. It provides a DirectOutputStream for adding bytes to the end and a DirectInputStream for pulling bytes 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 outputStream() attempts to write from the result of inputStream(), and the inputStream blocks, a write from another thread will be necessary to unblock the inputStream. At that point, the original write() will have already instructed the inputStream to copy its data into an incorrect location. In general, connecting a inputStream and a outputStream 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
ExpandingByteBuffer()
           
 
Method Summary
protected  byte[] allocateBuffer(int size)
          Create a fixed-size sub-buffer
 void end()
          Place an "end of file" at the end of the buffer.
 DirectInputStream inputStream()
          Create an input stream providing read access to the buffer.
 boolean isEnded()
           
 DirectOutputStream outputStream()
          Create an output stream 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

ExpandingByteBuffer

public ExpandingByteBuffer()
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 byte[] allocateBuffer(int size)
Description copied from class: ExpandingBuffer
Create a fixed-size sub-buffer

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

outputStream

public DirectOutputStream outputStream()
Create an output stream providing write access to the buffer. Invocations of write will atomically add bytes directly to the buffer. OutputStream.close() will have no effect.


inputStream

public DirectInputStream inputStream()
Create an input stream providing read access to the buffer. Invocations of read will atomically remove bytes from the buffer. InputStream.close() will have no effect.