tribble.io
Class LineReader

java.lang.Object
  extended by java.io.Reader
      extended by java.io.BufferedReader
          extended by java.io.LineNumberReader
              extended by tribble.io.LineReader
All Implemented Interfaces:
java.io.Closeable, java.lang.Readable

public class LineReader
extends java.io.LineNumberReader

Text line reader. Reads text lines from a text file.

This class extends the standard java.io.LineNumberReader class, performing the same functions, except that it expands tabs (HT, ' ') characters into spaces. By default, tabs are assumed to be aligned on 8-space columns, but this can be changed by calling the setTabWidth(int) method.

Since:
2002-12-21
Version:
$Revision: 1.5 $ $Date: 2003/02/16 22:45:37 $
Author:
David R. Tribble, david@tribble.com.
Copyright 2002-2003 by David R. Tribble, all rights reserved.
Permission is granted to freely use and distribute this source code provided that the original copyright and authorship notices remain intact.
See Also:
LineNumberReader

Field Summary
static int DFL_BUFSIZE
          Default maximum input text line buffer size.
static int DFL_TABSIZE
          Default tab width.
protected  char[] m_buf
          Input text line buffer.
protected  int m_colNo
          Column number of the last character read (starting at 1).
protected  java.io.Reader m_in
          Underlying input reader.
protected  int m_spacesLeft
          Pending spaces remaining for a tab expansion.
protected  int m_tabSize
          Tab width, zero specifies no tab expansion.
(package private) static java.lang.String REV
          Revision information.
 
Fields inherited from class java.io.Reader
lock
 
Constructor Summary
LineReader(java.io.Reader in)
          Constructor.
LineReader(java.io.Reader in, int bufSize)
          Constructor.
 
Method Summary
 int getTabWidth()
          Retrieve the number of spaces that tab characters are expanded into.
static void main(java.lang.String[] args)
          Test driver.
 void mark(int limit)
          Mark the present position in this input stream.
 boolean markSupported()
          Determine whether the mark(int) method is supported or not.
 int read(char[] buf, int off, int len)
          Read characters into a portion of an array.
protected  int readChar()
          Read a single text character from this input stream.
 java.lang.String readLine()
          Read a single line of text from this input stream.
 void reset()
          Reset this input stream to the most recent mark.
 void setTabWidth(int n)
          Establish the number of spaces that tab characters are expanded into.
 
Methods inherited from class java.io.LineNumberReader
getLineNumber, read, setLineNumber, skip
 
Methods inherited from class java.io.BufferedReader
close, ready
 
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

REV

static final java.lang.String REV
Revision information.

See Also:
Constant Field Values

DFL_TABSIZE

public static int DFL_TABSIZE
Default tab width.


DFL_BUFSIZE

public static int DFL_BUFSIZE
Default maximum input text line buffer size.


m_in

protected java.io.Reader m_in
Underlying input reader.


m_tabSize

protected int m_tabSize
Tab width, zero specifies no tab expansion.


m_colNo

protected int m_colNo
Column number of the last character read (starting at 1).


m_spacesLeft

protected int m_spacesLeft
Pending spaces remaining for a tab expansion.


m_buf

protected char[] m_buf
Input text line buffer.

Constructor Detail

LineReader

public LineReader(java.io.Reader in)
Constructor.

Parameters:
in - A character input stream to read characters from. This cannot be null.
Since:
1.1, 2002-12-21

LineReader

public LineReader(java.io.Reader in,
                  int bufSize)
Constructor.

Parameters:
in - A character input stream to read characters from. This cannot be null.
bufSize - The size of the input buffer to use for this input stream.
Since:
1.3, 2002-12-23
Method Detail

main

public static void main(java.lang.String[] args)
Test driver. Reads text lines from one or more text files.

Usage

    java tribble.io.LineReader file... 

Parameters:
args - Command line arguments.
Since:
1.2, 2002-12-23

setTabWidth

public void setTabWidth(int n)
Establish the number of spaces that tab characters are expanded into.

By default, tabs are expanded into spaces so as to align on every DFL_TABSIZE column.

Parameters:
n - The number of columns that tab characters align to. This can be zero, which specifies that tabs are not be replaced with spaces, but should be left as is.
Since:
1.2, 2002-12-22
See Also:
getTabWidth()

getTabWidth

public int getTabWidth()
Retrieve the number of spaces that tab characters are expanded into.

Returns:
The number of columns that tab characters align to. This can be zero, which specifies that tabs are not be replaced with spaces, but should be left as is.
Since:
1.2, 2002-12-22
See Also:
setTabWidth(int)

readLine

public java.lang.String readLine()
                          throws java.io.IOException
Read a single line of text from this input stream.

This method operates the same as LineNumberReader.readLine() except that it expands tab characters into spaces. The number of spaces is specified by a prior call to method setTabWidth(int).

Input lines are terminated by either a linefeed (' '), a carriage return (' '), or a carriage return followed by a linefeed (' '). Input lines are also considered terminated if they are followed by an end-of-file marker.

Invalid characters (i.e., nonprinting control characters) are replaced by spaces.

Overrides:
readLine in class java.io.LineNumberReader
Returns:
A string containing the text of the next input line, or null if the end of the input stream was reached. This string can be empty (""), indicating that the input line was composed of only a newline.
Throws:
java.io.IOException - Thrown if an I/O (read) error occurs.
Since:
1.1, 2002-12-21
See Also:
setTabWidth(int)

read

public int read(char[] buf,
                int off,
                int len)
         throws java.io.IOException
Read characters into a portion of an array.

Overrides:
read in class java.io.LineNumberReader
Parameters:
buf - A character array to read characters into.
off - Index of the first character within buf to read.
len - The maximum number of characters to read into buf.
Returns:
The actual number of characters read into array buf, which may be less than len.
Throws:
java.io.IOException
Since:
1.2, 2002-12-21

markSupported

public boolean markSupported()
Determine whether the mark(int) method is supported or not.

Overrides:
markSupported in class java.io.BufferedReader
Returns:
A value specifying whether the underlying input reader supports the mark() method or not.
Since:
1.2, 2002-12-22
See Also:
mark(int)

mark

public void mark(int limit)
          throws java.io.IOException
Mark the present position in this input stream.

Overrides:
mark in class java.io.LineNumberReader
Parameters:
limit - Limit on the number of characters that may be read while still preserving the mark.
Throws:
java.io.IOException - Thrown if this operation is not supported, or if some other I/O error occurs.
Since:
1.2, 2002-12-22
See Also:
markSupported(), reset()

reset

public void reset()
           throws java.io.IOException
Reset this input stream to the most recent mark.

Overrides:
reset in class java.io.LineNumberReader
Throws:
java.io.IOException - Thrown if this operation is not supported, or if some other I/O error occurs.
Since:
1.2, 2002-12-22
See Also:
mark(int)

readChar

protected int readChar()
                throws java.io.IOException
Read a single text character from this input stream.

This method operates the same as LineNumberReader.readLine() except that it expands tab characters into spaces. The number of spaces is specified by a prior call to method setTabWidth(int).

Invalid characters (i.e., nonprinting control characters) are replaced by spaces.

Note

This method is not synchronized, so any public member methods calling this method should be synchronized themselves.

Returns:
A character code, or -1 if the end of the input stream has been reached.
Throws:
java.io.IOException - Thrown if an I/O (read) error occurs.
Since:
1.2, 2002-12-22
See Also:
read(char[], int, int), readLine()