tribble.io
Class CharReader

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

public class CharReader
extends java.io.Reader

Generic character input stream.

This is a generic implementation of an input stream that is capable of reading a single character at a time.

This character stream handles various combinations of newline termination characters (LF, CR, and CR/LF pairs), returning a single newline character ('\n') for any given combination.

This also handles formfeed (FF) and null (NUL) characters, treating them as spaces (SP).

Tab (HT) characters are replaced with one or more spaces, to align on tabbed columns. By default, tabs are replaced with the appropriate number of spaces so as to align on 8-character columns, but this can be changed to a different width or disabled altogether.

Since:
2003-02-25
Version:
$Revision: 1.1 $ $Date: 2003/02/25 21:59:04 $
Author:
David R. Tribble, david@tribble.com.
Copyright ©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.

Field Summary
static int DFL_TABSIZE
          Default tab width (8).
static int EOF
          End of file character code.
protected  int m_colNo
          Column position (of the last character read).
protected  java.io.Reader m_in
          Character input stream.
protected  int m_tabSp
          Pending tab (HT) character replacement.
protected  int m_tabSz
          Tab (HT) column width.
protected  int m_ungetc
          Pushed-back (unread) character.
static int NEWLINE
          Newline (end of line) character code.
(package private) static java.lang.String REV
          Revision information.
 
Fields inherited from class java.io.Reader
lock
 
Constructor Summary
CharReader(java.io.Reader in)
          Constructor.
CharReader(java.io.Reader in, java.lang.Object lock)
          Constructor.
 
Method Summary
 void close()
          Close this input stream.
protected  void finalize()
          Finalization.
 void mark(int limit)
          Mark the current position in this input stream.
 boolean markSupported()
          Determine whether the underlying input stream supports position marks or not.
 int read()
          Read the next character from this input stream.
 int read(char[] cbuf, int off, int len)
          Read characters from this input stream into a portion of an array.
protected  int readChar()
          Read the next character from this input stream.
 boolean ready()
          Determine whether characters are ready to be read from this input stream or not.
 void reset()
          Reset the current position of this input stream to the last mark.
protected  void setInput(java.io.Reader in)
          Establish the input stream from which to read characters.
 void setTabSize(int n)
          Establish the tab (HT) column width.
 long skip(long n)
          Skip characters in this input stream.
 
Methods inherited from class java.io.Reader
read, read
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

REV

static final java.lang.String REV
Revision information.

See Also:
Constant Field Values

EOF

public static final int EOF
End of file character code.

See Also:
Constant Field Values

NEWLINE

public static final int NEWLINE
Newline (end of line) character code.

See Also:
Constant Field Values

DFL_TABSIZE

public static final int DFL_TABSIZE
Default tab width (8).

See Also:
Constant Field Values

m_in

protected java.io.Reader m_in
Character input stream.


m_ungetc

protected int m_ungetc
Pushed-back (unread) character.


m_colNo

protected int m_colNo
Column position (of the last character read).


m_tabSz

protected int m_tabSz
Tab (HT) column width.


m_tabSp

protected int m_tabSp
Pending tab (HT) character replacement.

Constructor Detail

CharReader

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

Parameters:
in - The input stream from which to read characters.
Since:
1.1, 2003-02-24

CharReader

public CharReader(java.io.Reader in,
                  java.lang.Object lock)
Constructor.

Parameters:
in - The input stream from which to read characters.
lock - An object that this input character stream will use to synchronize critical sections.
Since:
1.1, 2003-02-25
Method Detail

close

public void close()
           throws java.io.IOException
Close this input stream.

Disassociates the underlying input and output streams from this input stream.

Specified by:
close in interface java.io.Closeable
Specified by:
close in class java.io.Reader
Throws:
java.io.IOException - Thrown if an I/O error occurs.
Since:
1.1, 2003-02-25

read

public int read()
         throws java.io.IOException
Read the next character from this input stream.

This character stream handles various combinations of newline termination characters (LF, CR, and CR/LF pairs), returning a single newline character ('\n') for any given combination.

This stream also handles formfeed (FF) and null (NUL) characters, replacing them with spaces (SP).

Tab (HT) characters are replaced by one or more space (SP) characters, so that the character following the tab is aligned on a tab-sized column. Tab character replacement can be disabled by specifying a tab size of zero (see setTabSize()).

This method may block until a character has been read.

Overrides:
read in class java.io.Reader
Returns:
The next character code read from the input stream, or -1 if there are no more characters to read (i.e., the end of the input stream was reached).
Throws:
java.io.IOException - Thrown if an I/O (read) error occurs.
Since:
1.1, 2003-02-25

read

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

Characters are read from the input stream up to (and including) the first newline character read or until len characters are read into array cbuf, whichever comes first.

See read() for more details about the way characters are read from this input stream.

This method may block until enough characters have been read.

Specified by:
read in class java.io.Reader
Parameters:
cbuf - Character array to read characters into.
off - Index of the first character within cbuf to read.
len - Maximum number of characters to read into cbuf.
Returns:
The number of characters actually read into array cbuf (which will be no more than len), or -1 if there are no more characters available to read (i.e., the end of the input stream was reached).
Throws:
java.io.IOException - Thrown if an I/O (read) error occurs.
Since:
1.1, 2003-02-25

markSupported

public boolean markSupported()
Determine whether the underlying input stream supports position marks or not.

Overrides:
markSupported in class java.io.Reader
Returns:
True if the underlying input stream supports the mark(int) operation, otherwise false.
Throws:
java.io.IOException - Thrown if an I/O error occurs.
Since:
1.1, 2003-02-25

mark

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

Overrides:
mark in class java.io.Reader
Parameters:
limit - Minimum number number of characters that can be read ahead without losing the mark.
Throws:
java.io.IOException - Thrown if the underlying input stream does not support this operation, or if some other I/O error occurs.
Since:
1.1, 2003-02-25

reset

public void reset()
           throws java.io.IOException
Reset the current position of this input stream to the last mark. If no mark has been set, the stream is repositioned appropriately, such as to its beginning.

Overrides:
reset in class java.io.Reader
Throws:
java.io.IOException - Thrown if the underlying input stream does not support this operation, or if the previous mark has been invalidated, or if some other I/O error occurs.
Since:
1.1, 2003-02-25

skip

public long skip(long n)
          throws java.io.IOException
Skip characters in this input stream. This method may block until enough characters have been skipped.

Overrides:
skip in class java.io.Reader
Parameters:
n - The number of characters to skip in this input stream.
Returns:
The number of characters actually skipped (which may be less than n).
Throws:
java.io.IOException - Thrown if an I/O (read) error occurs.
java.lang.IllegalArgumentException - (unchecked) Thrown if n is negative.
Since:
1.1, 2003-02-25

ready

public boolean ready()
              throws java.io.IOException
Determine whether characters are ready to be read from this input stream or not.

Overrides:
ready in class java.io.Reader
Returns:
True if the next read() operation is guaranteed not to block for input, otherwise false. Note that a return of false does not guarantee that the next read() will block.
Throws:
java.io.IOException - Thrown if an I/O (read) error occurs.
Since:
1.1, 2003-02-25

setTabSize

public void setTabSize(int n)
Establish the tab (HT) column width.

Setting a tab width of zero disables the replacing of tab (HT) characters with spaces, i.e., tab characters are read as is. The default tab width is 8 (see DFL_TABSIZE).

Parameters:
n - The tab column width.
Since:
1.1, 2003-02-24

finalize

protected void finalize()
Finalization. Closes the underlying input stream.

Overrides:
finalize in class java.lang.Object
Since:
1.1, 2003-02-24

setInput

protected void setInput(java.io.Reader in)
Establish the input stream from which to read characters.

Parameters:
in - The underlying input stream from which to read characters.
Since:
1.1, 2003-02-25

readChar

protected int readChar()
                throws java.io.IOException
Read the next character from this input stream.

This character stream handles various combinations of newline termination characters (LF, CR, and CR/LF pairs), returning a single newline character ('\n') for any given combination.

This stream also handles formfeed (FF) and null (NUL) characters, replacing them with spaces (SP).

Tab (HT) characters are replaced by one or more space (SP) characters, so that the character following the tab is aligned on a tab-sized column. Tab character replacement can be disabled by specifying a tab size of zero (see setTabSize()).

This method may block until a character has been read.

Note that this method is not synchronized, so any public member method that invokes it should itself be synchronized.

Returns:
The next character code read from the input stream, or -1 if there are no more characters to read (i.e., the end of the input stream was reached).
Throws:
java.io.IOException - Thrown if an I/O (read) error occurs.
Since:
1.1, 2003-02-25