com.Ostermiller.util.CircularBuffer.bte Maven / Gradle / Ivy
<%bte.doc super="item.bte" %>
<%bte.tpl name=pageTitle%>Circular Buffers<%/bte.tpl%>
<%bte.tpl name=description%>Implements the circular buffer producer/consumer model for streams or Objects.<%/bte.tpl%>
<%bte.tpl name=keywords%>java circular buffers, circular buffer,circular object buffer, circularobjectbuffer, circular char buffer, circularcharbuffer, circular byte buffer, circularbytebuffer, pipedinputstream replacement, pipedoutputstream replacement, pipedwriter replacement, pipedreader replacement<%/bte.tpl%>
<%bte.tpl name=topcontent%>
The com.Ostermiller.util package contains three flavors of circular buffer. Each type
is presented here along with a simple example. For a more complex example that uses
threads and blocks input, please see the
unit test
for these classes.
<%/bte.tpl%>
<%bte.tpl name=content%>
CircularObjectBuffer | CircularCharBuffer | CircularByteBuffer
Example
// Create the buffer.
CircularObjectBuffer cob = new CircularObjectBuffer();
// Fill the buffer.
cob.write("Hello World!\n");
// Empty the buffer.
System.out.print((String)(cob.read()));
This example only works because "Hello World" is one object. If you try to write more objects than the size of
the buffer, the buffer will block until space is available.
In this single thread example, that will appear to cause a program hang. You can
get around this by making the buffer infinite size or emptying the buffer in another thread.
Circular Object Buffer
Implements the circular buffer producer/consumer model for Objects.
Example
// Create the buffer.
CircularCharBuffer ccb = new CircularCharBuffer();
// Fill the buffer.
ccb.getWriter().write("Hello World!\n");
ccb.getWriter().close();
// Empty the buffer.
int c;
while ((c = ccb.getReader().read()) != -1){
System.out.print((char)c);
}
This example only works because "Hello World" is short. If you try to write more data than the size of
the buffer, the buffer will block until space is available.
In this single thread example, that will appear to cause a program hang. You can
get around this by making the buffer infinite size or emptying the buffer in another thread.
Circular Character Buffer
Implements the circular buffer producer/consumer model for characters.
Filling and emptying the buffer is done with standard Java Readers and Writers.
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 a instantiating two
classes and connecting them.
Example
// Create the buffer.
CircularByteBuffer cbb = new CircularByteBuffer();
// Fill the buffer.
cbb.getOutputStream().write(
new byte[]{
'H','e','l','l','o',' ',
'W','o','r','l','d','!','\n'
}
);
cbb.getOutputStream().close();
// Empty the buffer.
int c;
while ((c = cbb.getInputStream().read()) != -1){
System.out.print((char)c);
}
This example only works because "Hello World" is short. If you try to write more data than the size of
the buffer, the buffer will block until space is available.
In this single thread example, that will appear to cause a program hang. You can
get around this by making the buffer infinite size or emptying the buffer in another thread.
Circular Byte Buffer
Implements the circular buffer producer/consumer model for bytes.
Filling and emptying the buffer is done with standard Java InputStreams and OutputStreams.
Using this class is a simpler alternative to using a PipedInputStream
and a PipedOutputStream. PipedInputStreams and PipedOutputStreams 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 a instantiating two
classes and connecting them.
<%/bte.tpl%>
<%bte.tpl name=linkCircularBuffer%>Circular Buffers
<%/bte.tpl%>
<%/bte.doc%>