public class FastBufferedReader extends Reader implements WordReader
This class provides buffering for readers, but it does so with
purposes and an internal logic that are radically different from the ones
adopted in BufferedReader
.
There is no support for marking. All methods are unsychronised. All
methods returning strings do so by writing in a given MutableString
.
Note that instances of this class can wrap an array
or a mutable string. In this case,
instances of this class may be used as a lightweight, unsynchronised
alternative to CharArrayReader
providing additional services such as word and line breaking.
As any WordReader
, this class is serialisable.
The only field kept is the current buffer size, which will be used to rebuild
a fast buffered reader with the same buffer size. All other fields will be reset.
This class implements WordReader
in the simplest way: words are defined as
maximal subsequences of characters satisfying Character.isLetterOrDigit(char)
.
To alter this behaviour, you have two choices:
CharSet
of characters that will be considered word constituents
besides those accepted by Character.isLetterOrDigit(char)
;
isWordConstituent(char)
.
The second approach is of course more flexible, but the first one is particularly useful from
the command line as there is a constructor
accepting the additional word constituents as a string
.
Modifier and Type | Field and Description |
---|---|
protected int |
avail
The number of buffer bytes available starting from
pos . |
protected char[] |
buffer
The internal buffer.
|
protected int |
bufferSize
The buffer size (must be equal to
buffer.length ). |
static int |
DEFAULT_BUFFER_SIZE
The default size of the internal buffer in bytes (16Ki).
|
protected int |
pos
The current position in the buffer.
|
protected Reader |
reader
The underlying reader.
|
static long |
serialVersionUID |
protected it.unimi.dsi.fastutil.chars.CharSet |
wordConstituents
A set of additional characters that will be considered as word constituents, beside those accepted by
Character.isLetterOrDigit(int) . |
Constructor and Description |
---|
FastBufferedReader()
Creates a new fast buffered reader with a buffer of
DEFAULT_BUFFER_SIZE characters. |
FastBufferedReader(char[] array)
Creates a new fast buffered reader by wrapping a given character array.
|
FastBufferedReader(char[] array,
it.unimi.dsi.fastutil.chars.CharSet wordConstituents)
Creates a new fast buffered reader by wrapping a given character array and using a set of additional word constituents.
|
FastBufferedReader(char[] array,
int offset,
int length)
Creates a new fast buffered reader by wrapping a given fragment of a character array.
|
FastBufferedReader(char[] array,
int offset,
int length,
it.unimi.dsi.fastutil.chars.CharSet wordConstituents)
Creates a new fast buffered reader by wrapping a given fragment of a character array and using a set of additional word constituents.
|
FastBufferedReader(it.unimi.dsi.fastutil.chars.CharSet wordConstituents)
Creates a new fast buffered reader with a buffer of
DEFAULT_BUFFER_SIZE characters and given set of additional word constituents. |
FastBufferedReader(int bufferSize)
Creates a new fast buffered reader with a given buffer size.
|
FastBufferedReader(int bufferSize,
it.unimi.dsi.fastutil.chars.CharSet wordConstituents)
Creates a new fast buffered reader with a given buffer size and set of additional word constituents.
|
FastBufferedReader(MutableString s)
Creates a new fast buffered reader by wrapping a given mutable string.
|
FastBufferedReader(MutableString s,
it.unimi.dsi.fastutil.chars.CharSet wordConstituents)
Creates a new fast buffered reader by wrapping a given mutable string and using a set of additional word constituents.
|
FastBufferedReader(Reader r)
Creates a new fast buffered reader by wrapping a given reader with a buffer of
DEFAULT_BUFFER_SIZE characters. |
FastBufferedReader(Reader r,
it.unimi.dsi.fastutil.chars.CharSet wordConstituents)
Creates a new fast buffered reader by wrapping a given reader with a buffer of
DEFAULT_BUFFER_SIZE characters and using a set of additional word constituents. |
FastBufferedReader(Reader r,
int bufferSize)
Creates a new fast buffered reader by wrapping a given reader with a given buffer size.
|
FastBufferedReader(Reader r,
int bufferSize,
it.unimi.dsi.fastutil.chars.CharSet wordConstituents)
Creates a new fast buffered reader by wrapping a given reader with a given buffer size and using a set of additional word constituents.
|
FastBufferedReader(String wordConstituents)
Creates a new fast buffered reader with a buffer of
DEFAULT_BUFFER_SIZE characters and a set of additional word constituents specified by a string. |
FastBufferedReader(String bufferSize,
String wordConstituents)
Creates a new fast buffered reader with a given buffer size and a set of additional word constituents, both specified by strings.
|
Modifier and Type | Method and Description |
---|---|
void |
close() |
FastBufferedReader |
copy()
Returns a copy of this word reader.
|
protected boolean |
isWordConstituent(char c)
Returns whether the given character is a word constituent.
|
boolean |
next(MutableString word,
MutableString nonWord)
Extracts the next word and non-word.
|
protected boolean |
noMoreCharacters()
Checks whether no more characters will be returned.
|
int |
read() |
int |
read(char[] b,
int offset,
int length) |
MutableString |
readLine(MutableString s)
Reads a line into the given mutable string.
|
FastBufferedReader |
setReader(Reader reader)
Resets the internal state of this word reader, which will start again reading from the given reader.
|
long |
skip(long n) |
String |
toSpec() |
String |
toString() |
public static final long serialVersionUID
public static final int DEFAULT_BUFFER_SIZE
protected final int bufferSize
buffer.length
).protected final it.unimi.dsi.fastutil.chars.CharSet wordConstituents
Character.isLetterOrDigit(int)
.protected transient char[] buffer
protected transient int pos
protected transient int avail
pos
.protected transient Reader reader
public FastBufferedReader(int bufferSize)
setReader(Reader)
.bufferSize
- the size in bytes of the internal buffer.public FastBufferedReader(int bufferSize, it.unimi.dsi.fastutil.chars.CharSet wordConstituents)
setReader(Reader)
.bufferSize
- the size in bytes of the internal buffer.wordConstituents
- a set of characters that will be considered word constituents.public FastBufferedReader()
DEFAULT_BUFFER_SIZE
characters.
The wrapped reader will have to be set later using setReader(Reader)
.public FastBufferedReader(it.unimi.dsi.fastutil.chars.CharSet wordConstituents)
DEFAULT_BUFFER_SIZE
characters and given set of additional word constituents.
The wrapped reader will have to be set later using setReader(Reader)
.wordConstituents
- a set of characters that will be considered word constituents.public FastBufferedReader(String wordConstituents)
DEFAULT_BUFFER_SIZE
characters and a set of additional word constituents specified by a string.wordConstituents
- a string of characters that will be considered word constituents.public FastBufferedReader(String bufferSize, String wordConstituents)
bufferSize
- the size in bytes of the internal buffer.wordConstituents
- a string of characters that will be considered word constituents.public FastBufferedReader(Reader r, int bufferSize)
r
- a reader to wrap.bufferSize
- the size in bytes of the internal buffer.public FastBufferedReader(Reader r, int bufferSize, it.unimi.dsi.fastutil.chars.CharSet wordConstituents)
r
- a reader to wrap.bufferSize
- the size in bytes of the internal buffer.wordConstituents
- a set of characters that will be considered word constituents.public FastBufferedReader(Reader r)
DEFAULT_BUFFER_SIZE
characters.r
- a reader to wrap.public FastBufferedReader(Reader r, it.unimi.dsi.fastutil.chars.CharSet wordConstituents)
DEFAULT_BUFFER_SIZE
characters and using a set of additional word constituents.r
- a reader to wrap.wordConstituents
- a set of characters that will be considered word constituents.public FastBufferedReader(char[] array, int offset, int length, it.unimi.dsi.fastutil.chars.CharSet wordConstituents)
The effect of setReader(Reader)
on a buffer created with
this constructor is undefined.
array
- the array that will be wrapped by the reader.offset
- the first character to be used.length
- the number of character to be used.wordConstituents
- a set of characters that will be considered word constituents.public FastBufferedReader(char[] array, int offset, int length)
The effect of setReader(Reader)
on a buffer created with
this constructor is undefined.
array
- the array that will be wrapped by the reader.offset
- the first character to be used.length
- the number of character to be used.public FastBufferedReader(char[] array, it.unimi.dsi.fastutil.chars.CharSet wordConstituents)
The effect of setReader(Reader)
on a buffer created with
this constructor is undefined.
array
- the array that will be wrapped by the reader.wordConstituents
- a set of characters that will be considered word constituents.public FastBufferedReader(char[] array)
The effect of setReader(Reader)
on a buffer created with
this constructor is undefined.
array
- the array that will be wrapped by the reader.public FastBufferedReader(MutableString s, it.unimi.dsi.fastutil.chars.CharSet wordConstituents)
The effect of setReader(Reader)
on a buffer created with
this constructor is undefined.
s
- the mutable string that will be wrapped by the reader.wordConstituents
- a set of characters that will be considered word constituents.public FastBufferedReader(MutableString s)
The effect of setReader(Reader)
on a buffer created with
this constructor is undefined.
s
- the mutable string that will be wrapped by the reader.public FastBufferedReader copy()
WordReader
This method must return a word reader with a behaviour that matches exactly that of this word reader.
copy
in interface WordReader
protected boolean noMoreCharacters() throws IOException
IOException
public int read() throws IOException
read
in class Reader
IOException
public int read(char[] b, int offset, int length) throws IOException
read
in class Reader
IOException
public MutableString readLine(MutableString s) throws IOException
The next line of input (defined as in BufferedReader.readLine()
)
will be stored into s
. Note that if s
is
not loose
this method will be quite inefficient.
s
- a mutable string that will be used to store the next line (which could be empty).s
, or null
if the end of file was found, in which
case s
is unchanged.IOException
protected boolean isWordConstituent(char c)
The behaviour of this FastBufferedReader
as a WordReader
can
be radically changed by overwriting this method.
c
- a character.c
should be considered a word constituent.public boolean next(MutableString word, MutableString nonWord) throws IOException
WordReader
If this method returns true, a new non-empty word, and possibly
a new non-word, have been extracted. It is acceptable
that the first call to this method after creation
or after a call to WordReader.setReader(Reader)
returns an empty
word. In other words both word
and nonWord
are maximal.
next
in interface WordReader
word
- the next word returned by the underlying reader.nonWord
- the nonword following the next word returned by the underlying reader.word
and nonWord
are unchanged).IOException
public FastBufferedReader setReader(Reader reader)
WordReader
setReader
in interface WordReader
reader
- the new reader providing characters.public long skip(long n) throws IOException
skip
in class Reader
IOException
public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
close
in class Reader
IOException
public String toSpec()
Copyright © 2006–2019 SYSTAP, LLC DBA Blazegraph. All rights reserved.