Sharkysoft home

lava.io
Class UnixLineReader

java.lang.Object
  |
  +--java.io.Reader
        |
        +--java.io.FilterReader
              |
              +--lava.io.LFilterReader
                    |
                    +--lava.io.UnixLineReader

public class UnixLineReader
extends LFilterReader

Filters any-platform text stream to appear as if it originated from a Unix file.

Don't let the name fool you! UnixLineReader is useful on all platforms. It allows you to read a text stream without worrying about which platform the stream was created on. Unix, Macintosh, and PCs all create text files using different endline sequences. Unfortunately, using system-independent code to get the line.separator property is an inadequate solution to the problem, since a program running on one platform may be reading a text file that was created on another platform.

This class automatically recognizes any of the three major line-ending sequences (Unix: "\n"; Win32: "\r\n"; Macintosh: "\r") -- even if they are intermixed within the stream (i.e., by concatenating files from different platforms) -- and transparently converts the line separators to the standard Unix endline character '\n' when they are read.

Knowing what kind of endline sequence to expect greatly simplifies the process of parsing line ends when you are reading the stream character by character. Looking for the end of the line? Just look for the '\n' character, and don't worry about trying to guess which platform the stream was created on! This filter also makes sure the last line is terminated with a line separator, even it this was not the case in the original source.

Skip characters. The counts of characters skipped are given in terms of filtered characters. (This matters only when reading DOS or Windows text files, since the endline sequence is 2 characters, but counted only as 1 character by this method.) The number of characters actually skipped is returned. A zero return value may indicate EOF.


Fields inherited from class java.io.FilterReader
in
 
Fields inherited from class java.io.Reader
lock
 
Constructor Summary
UnixLineReader(java.io.BufferedReader reader)
          Initializes a new instance to read from the specified reader.
 
Method Summary
 int getLineNumber()
          Returns the total number of lines read so far.
 void mark(int read_ahead_limit)
          Mark the present position in this filtered stream.
 int read()
          Reads the next character from the stream.
 int read(char[] dest, int off, int len)
          Reads a block of characters from the stream.
 java.lang.String readLine()
          Reads a line, or the rest of the current line if some of the current line has already been read.
 void reset()
          Attempts to reset the stream.
 void setLineNumber(int n)
          Sets the current line number.
 
Methods inherited from class lava.io.LFilterReader
read
 
Methods inherited from class java.io.FilterReader
close, markSupported, ready, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

UnixLineReader

public UnixLineReader(java.io.BufferedReader reader)
Initializes a new instance to read from the specified reader.
Parameters:
reader - the reader
Method Detail

read

public int read()
         throws java.io.IOException
Reads the next character from the stream.
Overrides:
read in class java.io.FilterReader
Returns:
the character read

read

public int read(char[] dest,
                int off,
                int len)
         throws java.io.IOException
Reads a block of characters from the stream.
Overrides:
read in class java.io.FilterReader
Parameters:
dest - destination array for storing the characters read
off - starting offset into the destination array
len - number of characters to read
Returns:
number of characters actually read (may be lest than len, indicating EOF or IO trouble)
Throws:
java.io.IOException - if an I/O error occurs

mark

public void mark(int read_ahead_limit)
          throws java.io.IOException
Mark the present position in this filtered stream. Subsequent calls to reset() will attempt to reposition the stream to the position and state it was in when this method was called. There is a limit to how many characters can be read ahead before attempts to reset will no longer succeed. Not all streams support mark(). Subclasses that override this method need to call super.mark.
Overrides:
mark in class java.io.FilterReader
Parameters:
read_ahead_limit - the read ahead limit
Throws:
java.io.IOException - if the stream does not support mark()

reset

public void reset()
           throws java.io.IOException
Attempts to reset the stream. If the stream has been marked, then this method will attempt to store the state of the stream at the time it was marked. If the stream has not been marked, then this method will attempt to reset the stream in some way that is appropriate to this particular stream. Not all streams support reset(). Subclasses that override this method need to call super.reset.
Overrides:
reset in class java.io.FilterReader
Throws:
java.io.IOException - if the stream cannot be reset

readLine

public final java.lang.String readLine()
                                throws java.io.IOException
Reads a line, or the rest of the current line if some of the current line has already been read.
Returns:
the line that was read, or null if EOF
Throws:
java.io.IOException - if an I/O error occurs

getLineNumber

public int getLineNumber()
Returns the total number of lines read so far. This method works only if the underlying reader, provided at construction time, is an instance of LineNumberReader. Otherwise, a ClassCastException will be thrown.
Returns:
the total number of lines read
Throws:
ClassCastException - if the base reader is not a LineNumberReader

setLineNumber

public void setLineNumber(int n)
Sets the current line number. This method works only if the underlying reader, provided at construction time, is an instance of LineNumberReader. Otherwise, a RuntimeException will be thrown.
Returns:
the total number of lines read
Throws:
ClassCastException - if the base reader is not a LineNumberReader

Sharkysoft home