com.Ostermiller.util Java Utilities


com.Ostermiller.util
Class CircularCharBuffer

java.lang.Object
  extended by com.Ostermiller.util.CircularCharBuffer

public class CircularCharBuffer
extends Object

Implements the Circular Buffer producer/consumer model for characters. More information about this class is available from ostermiller.org.

Using this class is a simpler alternative to using a PipedReader and a PipedWriter. PipedReaders and PipedWriters don't support the mark operation, don't allow you to control buffer sizes that they use, and have a more complicated API that requires instantiating two classes and connecting them.

This class is thread safe.

Since:
ostermillerutils 1.00.00
Author:
Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
See Also:
CircularByteBuffer, CircularObjectBuffer

Field Summary
static int INFINITE_SIZE
          A buffer that will grow as things are added.
 
Constructor Summary
CircularCharBuffer()
          Create a new buffer with a default capacity.
CircularCharBuffer(boolean blockingWrite)
          Create a new buffer with a default capacity and given blocking behavior.
CircularCharBuffer(int size)
          Create a new buffer with given capacity.
CircularCharBuffer(int size, boolean blockingWrite)
          Create a new buffer with the given capacity and blocking behavior.
 
Method Summary
 void clear()
          Make this buffer ready for reuse.
 int getAvailable()
          Get number of characters that are available to be read.
 Reader getReader()
          Retrieve a Reader that can be used to empty this buffer.
 int getSize()
          Get the capacity of this buffer.
 int getSpaceLeft()
          Get the number of characters this buffer has free for writing.
 Writer getWriter()
          Retrieve a Writer that can be used to fill this buffer.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

INFINITE_SIZE

public static final int INFINITE_SIZE
A buffer that will grow as things are added.

Since:
ostermillerutils 1.00.00
See Also:
Constant Field Values
Constructor Detail

CircularCharBuffer

public CircularCharBuffer()
Create a new buffer with a default capacity. Writing to a full buffer will block until space is available rather than throw an exception.

Since:
ostermillerutils 1.00.00

CircularCharBuffer

public CircularCharBuffer(int size)
Create a new buffer with given capacity. Writing to a full buffer will block until space is available rather than throw an exception.

Note that the buffer may reserve some characters for special purposes and capacity number of characters may not be able to be written to the buffer.

Note that if the buffer is of INFINITE_SIZE it will neither block or throw exceptions, but rather grow without bound.

Parameters:
size - desired capacity of the buffer in characters or CircularCharBuffer.INFINITE_SIZE
Since:
ostermillerutils 1.00.00

CircularCharBuffer

public CircularCharBuffer(boolean blockingWrite)
Create a new buffer with a default capacity and given blocking behavior.

Parameters:
blockingWrite - true writing to a full buffer should block until space is available, false if an exception should be thrown instead.
Since:
ostermillerutils 1.00.00

CircularCharBuffer

public CircularCharBuffer(int size,
                          boolean blockingWrite)
Create a new buffer with the given capacity and blocking behavior.

Note that the buffer may reserve some characters for special purposes and capacity number of characters may not be able to be written to the buffer.

Note that if the buffer is of CircularCharBuffer.INFINITE_SIZE it will neither block or throw exceptions, but rather grow without bound.

Parameters:
size - desired capacity of the buffer in characters or CircularCharBuffer.INFINITE_SIZE
blockingWrite - true writing to a full buffer should block until space is available, false if an exception should be thrown instead.
Since:
ostermillerutils 1.00.00
Method Detail

clear

public void clear()
Make this buffer ready for reuse. The contents of the buffer will be cleared and the streams associated with this buffer will be reopened if they had been closed.

Since:
ostermillerutils 1.00.00

getWriter

public Writer getWriter()
Retrieve a Writer that can be used to fill this buffer.

Write methods may throw a BufferOverflowException if the buffer is not large enough. A large enough buffer size must be chosen so that this does not happen or the caller must be prepared to catch the exception and try again once part of the buffer has been consumed.

Returns:
the producer for this buffer.
Since:
ostermillerutils 1.00.00

getReader

public Reader getReader()
Retrieve a Reader that can be used to empty this buffer.

This Reader supports marks at the expense of the buffer size.

Returns:
the consumer for this buffer.
Since:
ostermillerutils 1.00.00

getAvailable

public int getAvailable()
Get number of characters that are available to be read.

Note that the number of characters available plus the number of characters free may not add up to the capacity of this buffer, as the buffer may reserve some space for other purposes.

Returns:
the size in characters of this buffer
Since:
ostermillerutils 1.00.00

getSpaceLeft

public int getSpaceLeft()
Get the number of characters this buffer has free for writing.

Note that the number of characters available plus the number of characters free may not add up to the capacity of this buffer, as the buffer may reserve some space for other purposes.

Returns:
the available space in characters of this buffer
Since:
ostermillerutils 1.00.00

getSize

public int getSize()
Get the capacity of this buffer.

Note that the number of characters available plus the number of characters free may not add up to the capacity of this buffer, as the buffer may reserve some space for other purposes.

Returns:
the size in characters of this buffer
Since:
ostermillerutils 1.00.00

com.Ostermiller.util Java Utilities


Copyright © 2001-2012 by Stephen Ostermiller