|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectjava.io.Reader
asia.redact.bracket.properties.line.LineScanner
public class LineScanner
This is an augmented version of java.io.BufferedReader from the Apache Harmony implementation. Source: http://www.java2s.com/Open-Source/Java-Document/Apache-Harmony-Java-SE/java-package/java/io/BufferedReader.java.htm Scan a properties file conforming to the description at http://download.oracle.com/javase/6/docs/api/java/util/Properties.html#load(java.io.Reader) into tokens. The idea here is that as we read lines, we return not just the line text but a Line object which has knowledge of its internal parts, such as if the line has a key, etc. This allows for streaming parsing and hopefully can handle very large properties files. There is one additional extension: a comment line which starts with #;; is treated as transient (not read in). This is used later to generate a transient header and footer This class tracks the absolute offset of lines in the file, and the line() method works like readLine() but returns extra info in addition to the String. The Line object returned by "line()" provides tokenization methods and also an absolute index of the line into the file. PropertiesParser2 "pulls" the lines using this BufferedReader implementation. Note: because this is a Reader, you must call close() on it when done.
Line| Field Summary | |
|---|---|
private char[] |
buf
The characters that can be read and refilled in bulk. |
private LineEnding |
delimiter
|
private int |
delimiterLength
|
private int |
end
|
private Reader |
in
|
private int |
mark
|
private int |
markLimit
|
private int |
pos
|
private long |
totalRead
|
| Fields inherited from class java.io.Reader |
|---|
lock |
| Constructor Summary | |
|---|---|
LineScanner(Reader in)
Constructs a new BufferedReader on the Reader in. |
|
LineScanner(Reader in,
int size)
Constructs a new BufferedReader on the Reader in. |
|
| Method Summary | |
|---|---|
void |
close()
Closes this reader. |
private int |
fillBuf()
Populates the buffer with data. |
LineEnding |
getDelimiter()
|
long |
getTotalRead()
|
private boolean |
isClosed()
Indicates whether or not this reader is closed. |
Line |
line()
|
void |
mark(int markLimit)
Sets a mark position in this reader. |
boolean |
markSupported()
Indicates whether this reader supports the mark() and
reset() methods. |
int |
read()
Reads a single character from this reader and returns it with the two higher-order bytes set to 0. |
int |
read(char[] buffer,
int offset,
int length)
Reads at most length characters from this reader and stores them
at offset in the character array buffer. |
String |
readLine()
Returns the next line of text available from this reader. |
boolean |
ready()
Indicates whether this reader is ready to be read without blocking. |
void |
reset()
Resets this reader's position to the last mark() location. |
long |
skip(long amount)
Skips amount characters in this reader. |
| Methods inherited from class java.io.Reader |
|---|
read, read |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
private final Reader in
private char[] buf
{ X X X X X X X X X X X X - - }
^ ^ ^
| | |
mark pos end
Pos points to the next readable character. End is one greater than the
last readable character. When pos == end, the buffer is empty and
must be filled before characters can be read.
Mark is the value pos will be set to on calls to reset(). Its
value is in the range [0...pos]. If the mark is -1, the
buffer cannot be reset.
MarkLimit limits the distance between the mark and the pos. When this
limit is exceeded, reset() is permitted (but not required) to
throw an exception. For shorter distances, reset() shall not throw
(unless the reader is closed).
private int pos
private int end
private int mark
private int markLimit
private long totalRead
private int delimiterLength
private LineEnding delimiter
| Constructor Detail |
|---|
public LineScanner(Reader in)
in. The
buffer gets the default size (8 KB).
in - the Reader that is buffered.
public LineScanner(Reader in,
int size)
in. The buffer
size is specified by the parameter size.
in - the Reader that is buffered.size - the size of the buffer to allocate.
IllegalArgumentException - if size <= 0.| Method Detail |
|---|
public void close()
throws IOException
close in interface Closeableclose in class ReaderIOException - if an error occurs while closing this reader.
private int fillBuf()
throws IOException
pos < end.
IOExceptionprivate boolean isClosed()
true if this reader is closed, false
otherwise.
public void mark(int markLimit)
throws IOException
markLimit
indicates how many characters can be read before the mark is invalidated.
Calling reset() will reposition the reader back to the marked
position if markLimit has not been surpassed.
mark in class ReadermarkLimit - the number of characters that can be read before the mark is
invalidated.
IllegalArgumentException - if markLimit < 0.
IOException - if an error occurs while setting a mark in this reader.markSupported(),
reset()public boolean markSupported()
mark() and
reset() methods. This implementation returns true.
markSupported in class Readertrue for BufferedReader.mark(int),
reset()
public int read()
throws IOException
read in class ReaderIOException - if this reader is closed or some other I/O error occurs.
public int read(char[] buffer,
int offset,
int length)
throws IOException
length characters from this reader and stores them
at offset in the character array buffer. Returns the
number of characters actually read or -1 if the end of the source reader
has been reached. If all the buffered characters have been used, a mark
has not been set and the requested number of characters is larger than
this readers buffer size, BufferedReader bypasses the buffer and simply
places the results directly into buffer.
read in class Readerbuffer - the character array to store the characters read.offset - the initial position in buffer to store the bytes read
from this reader.length - the maximum number of characters to read, must be
non-negative.
IndexOutOfBoundsException - if offset < 0 or length < 0, or if
offset + length is greater than the size of
buffer.
IOException - if this reader is closed or some other I/O error occurs.public Line line()
public String readLine()
throws IOException
'\n',
'\r', "\r\n" or the end of the reader. The string does
not include the newline sequence.
null if no characters were
read before the end of the reader has been reached.
IOException - if this reader is closed or some other I/O error occurs.
public boolean ready()
throws IOException
ready in class Readertrue if this reader will not block when read is
called, false if unknown or blocking will occur.
IOException - if this reader is closed or some other I/O error occurs.read(),
read(char[], int, int),
readLine()
public void reset()
throws IOException
mark() location.
Invocations of read() and skip() will occur from this new
location.
reset in class ReaderIOException - if this reader is closed or no mark has been set.mark(int),
markSupported()
public long skip(long amount)
throws IOException
amount characters in this reader. Subsequent
read()s will not return these characters unless reset()
is used. Skipping characters may invalidate a mark if markLimit
is surpassed.
skip in class Readeramount - the maximum number of characters to skip.
IllegalArgumentException - if amount < 0.
IOException - if this reader is closed or some other I/O error occurs.mark(int),
markSupported(),
reset()public long getTotalRead()
public LineEnding getDelimiter()
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||