|
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. 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 BufferImplements 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 BufferImplements 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 BufferImplements 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. |
OstermillerUtil Java Utilities Copyright (c) 2001-2020 by Stephen Ostermiller and other contributors
The OstermillerUtils library is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
License FAQs - Why GPL? How about the LGPL or something else?