public class PipeRingBuffer
extends java.lang.Object
implements java.lang.Appendable
PipedReader
and PipedWriter
this class contains a method
to read a line: getLine(char[])
. It is similar BufferedReader
.
In opposite to Pipe
this class deals with simple Appendable, not with that channels.
It may be more simple for application.
append(CharSequence, int, int)
or offer(CharSequence, int, int)
and read out with getLine(char[])
.
The buffer may have a larger size, for example more as 1 MByte to hold larger data. The storing of characters
is organized in a ring strucuture.
offer(CharSequence, int, int)
routine does never block, it returns false if there is no space
in the buffer. But on writing its data it synchronizes with this class to prevent inconsistent data.
getLine(char[])
method checks whether at least on char is in the Buffer. If the buffer is emtpy,
it returns -1. If the buffer is not empty, it expects that a line may be found, because
it may be presumed that other threads writes whole lines. But it is possible that another thread has written only
a part of a line with append(CharSequence)
. In this case the other thread should work to complete.
Therefore this thread waits 20 milliseconds, enough time for thread switch, and tries to complete the line.
If this thread has a higher priority and the writing thread is very low, all other threads should work too.
Only in that case the reading thread finishes its wait, try again, wait again etc.
get(Appendable, String)
does never wait. But it synchronized to prevent inconsistent data.
Modifier and Type | Field and Description |
---|---|
private char[] |
buffer |
private int |
posr |
private int |
posw |
static java.lang.String |
sVersion
Version, history and license.
|
private int |
zchars |
Constructor and Description |
---|
PipeRingBuffer(int buffersize) |
Modifier and Type | Method and Description |
---|---|
java.lang.Appendable |
append(char c) |
java.lang.Appendable |
append(java.lang.CharSequence csq) |
java.lang.Appendable |
append(java.lang.CharSequence csq,
int start,
int end) |
int |
get(java.lang.Appendable dst,
java.lang.String charsBreak)
Gets the available number of characters from Buffer till any of the charsBreak are found.
|
(package private) int |
getLine(char[] dst) |
boolean |
offer(java.lang.CharSequence csq,
int from,
int to) |
int |
skip(java.lang.String charsBreak)
Skips over all characters which are member of charsBreak
|
public static final java.lang.String sVersion
private final char[] buffer
private int posw
private int posr
private int zchars
public java.lang.Appendable append(java.lang.CharSequence csq) throws java.io.IOException
append
in interface java.lang.Appendable
java.io.IOException
public boolean offer(java.lang.CharSequence csq, int from, int to)
public java.lang.Appendable append(char c) throws java.io.IOException
append
in interface java.lang.Appendable
java.io.IOException
public java.lang.Appendable append(java.lang.CharSequence csq, int start, int end) throws java.io.IOException
append
in interface java.lang.Appendable
java.io.IOException
int getLine(char[] dst)
public int get(java.lang.Appendable dst, java.lang.String charsBreak)
dst
- charsBreak
- public int skip(java.lang.String charsBreak)
charsBreak
- Characters to skip, for example "\r\n" to skip over both newline or return.