foundry-0.9.1

foundry.io
Class CommentStrippingBufferedReader

java.lang.Object
  |
  +--java.io.Reader
        |
        +--java.io.BufferedReader
              |
              +--foundry.io.CommentStrippingBufferedReader

public class CommentStrippingBufferedReader
extends BufferedReader

A BufferedReader that only returns characters that are not part of a comment. A comment, for this class' purposes, starts with a designated String and ends with the value of the line.separator system property.

Version:
$Revision: 1.4 $
Author:
Laird Nelson
See Also:
BufferedReader

Field Summary
private  String commentLeader
          The String that constitutes the comment characters.
private  int[] miniBuffer
          A buffer used to store partial matches of multi-character comment leaders.
private  int miniBufferIndex
          The index into the miniBuffer.
 
Fields inherited from class java.io.BufferedReader
cb, defaultCharBufferSize, defaultExpectedLineLength, in, INVALIDATED, markedChar, markedSkipLF, nChars, nextChar, readAheadLimit, skipLF, UNMARKED
 
Fields inherited from class java.io.Reader
lock, maxSkipBufferSize, skipBuffer
 
Constructor Summary
CommentStrippingBufferedReader(Reader in)
          Creates a new CommentStrippingBufferedReader that wraps, buffers and strips comments from the supplied Reader.
CommentStrippingBufferedReader(Reader in, int size)
          Creates a new CommentStrippingBufferedReader that wraps, buffers and strips comments from the supplied Reader.
CommentStrippingBufferedReader(Reader in, int size, String leader)
          Creates a new CommentStrippingBufferedReader that wraps, buffers and strips comments from the supplied Reader.
CommentStrippingBufferedReader(Reader in, String leader)
          Creates a new CommentStrippingBufferedReader that wraps, buffers and strips comments from the supplied Reader.
 
Method Summary
private  void discardLine()
          Rather inefficiently throws out the remainder of a line from the current position, placing the line terminator characters into the miniBuffer.
private  void emptyMiniBuffer()
          Empties the miniBuffer and resets the miniBufferIndex.
 String getCommentLeader()
          Returns the current comment leader.
 int read()
          Reads a character from the underlying Reader, buffering as necessary and skipping:
    comment characters (see getCommentLeader()) any characters following comment characters up until (but not including) the next occurrence of the value of the line.separator system property For example, from a line like the following:
 int read(char[] cbuf, int offset, int len)
          Adheres to the general contract for BufferedReader.read(char[], int, int), but skips characters that are not comments.
 String readLine()
          Reads an entire line of text, stripping comment characters and, like its parent, disposing of the line terminator.
 void setCommentLeader(String leader)
          Sets the comment leader.
 
Methods inherited from class java.io.BufferedReader
, close, ensureOpen, fill, mark, markSupported, read1, readLine, ready, reset, skip
 
Methods inherited from class java.io.Reader
read
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

commentLeader

private String commentLeader
The String that constitutes the comment characters. Often set to "#" or "//".

miniBuffer

private int[] miniBuffer
A buffer used to store partial matches of multi-character comment leaders.

miniBufferIndex

private int miniBufferIndex
The index into the miniBuffer.
Constructor Detail

CommentStrippingBufferedReader

public CommentStrippingBufferedReader(Reader in)
Creates a new CommentStrippingBufferedReader that wraps, buffers and strips comments from the supplied Reader.
Parameters:
in - the underlying Reader

CommentStrippingBufferedReader

public CommentStrippingBufferedReader(Reader in,
                                      String leader)
Creates a new CommentStrippingBufferedReader that wraps, buffers and strips comments from the supplied Reader.
Parameters:
in - the underlying Reader
leader - the String that indicates that everything up to the next line separator but not including it is a comment; if null then the comment leader will be "#"

CommentStrippingBufferedReader

public CommentStrippingBufferedReader(Reader in,
                                      int size)
Creates a new CommentStrippingBufferedReader that wraps, buffers and strips comments from the supplied Reader.
Parameters:
in - the underlying Reader
size - the size of the buffer
See Also:
BufferedReader

CommentStrippingBufferedReader

public CommentStrippingBufferedReader(Reader in,
                                      int size,
                                      String leader)
Creates a new CommentStrippingBufferedReader that wraps, buffers and strips comments from the supplied Reader.
Parameters:
in - the underlying Reader
size - the size of the buffer
leader - the String that indicates that everything up to the next line separator but not including it is a comment; if null, then the comment leader will be "#"
Method Detail

setCommentLeader

public void setCommentLeader(String leader)
                      throws NullPointerException
Sets the comment leader. It is not wise to call this method while reading a character stream, as it may cause this CommentStrippingBufferedReader to ignore some data from the stream.
Parameters:
leader - the String that indicates that everything up to the next line separator but not including it is a comment
Throws:
NullPointerException - if leader is null

getCommentLeader

public String getCommentLeader()
Returns the current comment leader. This method must never return null.
Returns:
the current comment leader

read

public int read()
         throws IOException
Reads a character from the underlying Reader, buffering as necessary and skipping: For example, from a line like the following:
 name=ljnelson # the name of the user
 type=author
 
...successive calls to this method will yield "n", "a", "m", "e", "=", "l", "j", "n", "e", "l", "s", "o", "n", " ", "\n", "t", "y", "p", "e", "=", "a", "u", "t", "h", "o", "r", and "\n" (assuming the value of the line.separator system property is "\n" and not "\r\n").
Overrides:
read in class BufferedReader
Returns:
the character read as an integer, or -1 if the end of the underlying stream has been reached
Throws:
IOException - if some input/output exception occurs

read

public int read(char[] cbuf,
                int offset,
                int len)
         throws IOException
Adheres to the general contract for BufferedReader.read(char[], int, int), but skips characters that are not comments. See read() for a detailed explanation of how characters are considered to be comments.
Overrides:
read in class BufferedReader
Parameters:
cbuf - the char array to place read characters into
offset - the index within cbuf at which to begin inserting characters
len - the number of characters to insert
Returns:
the number of characters actually read
Throws:
IOException - if an input/output exception occurs

discardLine

private final void discardLine()
                        throws IOException
Rather inefficiently throws out the remainder of a line from the current position, placing the line terminator characters into the miniBuffer.
Throws:
IOException - if an input/output error occurs

emptyMiniBuffer

private final void emptyMiniBuffer()
Empties the miniBuffer and resets the miniBufferIndex.

readLine

public String readLine()
                throws IOException
Reads an entire line of text, stripping comment characters and, like its parent, disposing of the line terminator.
Overrides:
readLine in class BufferedReader
Returns:
the line read or null if there are no more lines to be read
Throws:
IOException - if an input/output exception occurs

foundry-0.9.1

Copyright © 2000 - Laird Jarrett Nelson