ewe.io
Class TextWriter

java.lang.Object
  extended byewe.io.Writer
      extended byewe.io.TextWriter
Direct Known Subclasses:
OutputStreamWriter

public class TextWriter
extends Writer

A TextWriter is used for writing Unicode text files that may be encoded as bytes in any format. It uses a TextCodec for encoding the characters into bytes to be sent to the output 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
protected  boolean closed
          This indicates that the close() method has already been called.
 TextCodec codec
          This is the codec used for encoding outgoing data characters into bytes.
protected  OutputStream output
          This is the underlying stream, or the "stream" Stream may be used instead.
protected  Stream stream
          This is the underlying stream, or the "output" OutputStream may be used instead.
 
Fields inherited from class ewe.io.Writer
lock
 
Constructor Summary
TextWriter(BasicStream out)
          Create a new TextWriter to write to the specified Stream.
TextWriter(OutputStream out)
          Create a new TextWriter to write to the specified Stream.
TextWriter(String path, boolean append)
          Create a new TextWriter to write to the specified file name.
 
Method Summary
 void close()
          Close the TextWriter and the underlying Stream.
 void flush()
          Flush any waiting data to the underlying output Stream.
 void print(String text)
          Write out a String of text.
 void println()
          Output a line-feed (end of line) character.
 void println(String text)
          Write out a String of text followed by a line-feed..
 void write(char[] chars, int offset, int length)
          Write out a number of characters.
static void writeAll(String text, Object basicStreamOrOutputStream, TextCodec codec)
          Write an entire string to a stream.
 
Methods inherited from class ewe.io.Writer
getLock, write, write, write, write
 
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 "output" OutputStream may be used instead.


output

protected OutputStream output
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 encoding outgoing data characters into bytes. By default it is a JavaUtf8Codec.

Constructor Detail

TextWriter

public TextWriter(BasicStream out)
Create a new TextWriter to write to the specified Stream.

Parameters:
out - The output Stream.

TextWriter

public TextWriter(OutputStream out)
Create a new TextWriter to write to the specified Stream.

Parameters:
out - The OutputStream.

TextWriter

public TextWriter(String path,
                  boolean append)
           throws IOException
Create a new TextWriter to write to the specified file name.

Parameters:
path - The path of the file.
append - true to append to an existing file, false to overwrite.
Throws:
IOException - if the file could not be opened for writing.
Method Detail

flush

public void flush()
           throws IOException
Flush any waiting data to the underlying output Stream. Flush the output Stream as well.

Specified by:
flush in class Writer
Throws:
IOException - If an I/O error occurs

write

public void write(char[] chars,
                  int offset,
                  int length)
           throws IOException
Write out a number of characters.

Specified by:
write in class Writer
Parameters:
chars - The characters to be writen.
offset - The location in the chars to start writing from.
length - The number of characters to write.
Throws:
IOException - If there is an error writing the data.

print

public void print(String text)
           throws IOException
Write out a String of text.

Parameters:
text - The String to write out.
Throws:
IOException - If there is an error writing the data.

println

public void println(String text)
             throws IOException
Write out a String of text followed by a line-feed..

Parameters:
text - The String to write out.
Throws:
IOException - If there is an error writing the data.

println

public void println()
             throws IOException
Output a line-feed (end of line) character.

Throws:
IOException - If there is an error writing the data.

close

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

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

writeAll

public static void writeAll(String text,
                            Object basicStreamOrOutputStream,
                            TextCodec codec)
                     throws IOException
Write an entire string to a stream. The stream will be closed after.

Parameters:
basicStreamOrOutputStream - This must be a BasicStream or an OutputStream object.
codec - The codec to use or null for the default Java UTF8 codec.
Throws:
IOException - if an error occured during writing or encoding.