ewe.io
Class TextReader

java.lang.Object
  extended byewe.io.Reader
      extended byewe.io.TextReader
Direct Known Subclasses:
InputStreamReader

public class TextReader
extends Reader

A TextReader is used for reading Unicode text files that may be encoded as bytes in any format. It uses a TextCodec for decoding the byte encoded input Stream and so you must provide the correct Codec for the file. The only Codecs provided by the Ewe library are the JavaUtf8Codec (which will encode ANY Unicode text) and AsciiCodec (which only encodes Unicode values from 0 to 255 as their exact byte values).

By default, a JavaUtf8Codec is used for encoding.


Field Summary
 int bufferSize
          This is the buffer size to use when reading from the underlying stream.
protected  boolean closed
          This indicates that the close() method has already been called.
 TextCodec codec
          This is the codec used for decoding incoming data bytes into characters.
protected  InputStream input
          This is the underlying stream, or the "stream" Stream may be used instead.
protected  Stream stream
          This is the underlying stream, or the "input" InputStream may be used instead.
 
Fields inherited from class ewe.io.Reader
lock
 
Constructor Summary
TextReader(BasicStream in)
          Create a new TextReader to read from the specified Stream.
TextReader(InputStream in)
          Create a new TextReader to read from the specified InputStream.
TextReader(String path)
          Create a new TextReader to read from the specified file name.
 
Method Summary
 void close()
          Close the TextReader and the underlying Stream.
 int read(char[] dest, int offset, int length)
          Read in a number of characters.
 String readAll()
          Read in all the characters from this TextReader as a String.
static String readAll(Object basicStreamOrInputStream, TextCodec codec)
          Read all the characters from the input stream using the specified TextCodec.
 int readDirect(char[] dest, int offset, int length)
          Read in a number of characters.
 void readFully(char[] dest, int offset, int length)
          Read in exactly the specified number of characters.
 String readFully(int length)
          Read in exactly the specified number of characters as a String.
 String readLine()
          Read a line of text.
 String readString(int length)
          Read in up to the specified maximum number of characters as a String.
 boolean ready()
          Tell whether this stream is ready to be read.
 
Methods inherited from class ewe.io.Reader
getLock, mark, markSupported, read, read, reset, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, toString
 

Field Detail

stream

protected Stream stream
This is the underlying stream, or the "input" InputStream may be used instead.


input

protected InputStream input
This is the underlying stream, or the "stream" Stream may be used instead.


closed

protected boolean closed
This indicates that the close() method has already been called.


codec

public TextCodec codec
This is the codec used for decoding incoming data bytes into characters. By default it is a JavaUtf8Codec.


bufferSize

public int bufferSize
This is the buffer size to use when reading from the underlying stream.

Constructor Detail

TextReader

public TextReader(BasicStream in)
Create a new TextReader to read from the specified Stream.

Parameters:
in - The input Stream.

TextReader

public TextReader(InputStream in)
Create a new TextReader to read from the specified InputStream.

Parameters:
in - The InputStream

TextReader

public TextReader(String path)
           throws IOException
Create a new TextReader to read from the specified file name.

Parameters:
path - the path to the file to read.
Method Detail

readDirect

public int readDirect(char[] dest,
                      int offset,
                      int length)
               throws IOException
Read in a number of characters.

Parameters:
dest - The destination buffer for the characters.
offset - The location in the destination to start writing to.
length - The maximum number of characters to read.
Returns:
The number of characters read or -1 if the end of the stream has been reached.
Throws:
IOException - If there is an error reading the data.

readLine

public String readLine()
                throws IOException
Read a line of text.

Returns:
The line read in (without the trailing CR/LF) or null if end-of-file has been reached.
Throws:
IOException

read

public int read(char[] dest,
                int offset,
                int length)
         throws IOException
Read in a number of characters.

Specified by:
read in class Reader
Parameters:
dest - The destination buffer for the characters.
offset - The location in the destination to start writing to.
length - The maximum number of characters to read.
Returns:
The number of characters read or -1 if the end of the stream has been reached.
Throws:
IOException - If there is an error reading the data.

close

public void close()
           throws IOException
Close the TextReader and the underlying Stream.

Specified by:
close in class Reader
Throws:
IOException - on error.

readFully

public void readFully(char[] dest,
                      int offset,
                      int length)
               throws IOException
Read in exactly the specified number of characters. An exception will be thrown if the Stream ends before the specified number of characters is read.

Parameters:
dest - The destination for the characters.
offset - The offset in the destination for the characters.
length - The number of characters to read.
Throws:
IOException - if an IO error occurs or if the

readFully

public String readFully(int length)
                 throws IOException
Read in exactly the specified number of characters as a String.

Parameters:
length - The number of characters to read.
Returns:
A String holding the text.
Throws:
IOException - if an IO error occurs or if the Stream ends before all the characters could be read.

readString

public String readString(int length)
                  throws IOException
Read in up to the specified maximum number of characters as a String. There may be less characters specified IF the Stream ends before the number of characters can be read. If the method returns null the Stream has ended.

Parameters:
length - The maximum number of characters to read.
Returns:
A String holding the characters read or null on end of Stream.
Throws:
IOException - if an IO error occurs.

readAll

public String readAll()
               throws IOException
Read in all the characters from this TextReader as a String. The TextReader is NOT closed after.

Returns:
The entire stream read in as a String.
Throws:
IOException - if an error occured during reading or decoding.

readAll

public static String readAll(Object basicStreamOrInputStream,
                             TextCodec codec)
                      throws IOException
Read all the characters from the input stream using the specified TextCodec. The stream will be closed after.

Parameters:
basicStreamOrInputStream - This must be a BasicStream or an InputStream object.
codec - The codec to use or null for the default Java UTF8 codec.
Returns:
The entire stream read in as a String.
Throws:
IOException - if an error occured during reading or decoding.

ready

public boolean ready()
              throws IOException
Description copied from class: Reader
Tell whether this stream is ready to be read.

Overrides:
ready in class Reader
Returns:
True if the next read() is guaranteed not to block for input, false otherwise. Note that returning false does not guarantee that the next read will block.
Throws:
IOException - If an I/O error occurs