JADE v6.1

com.dautelle.util
Class Utf8StreamWriter

java.lang.Object
  extended byjava.io.Writer
      extended bycom.dautelle.util.Utf8StreamWriter

public final class Utf8StreamWriter
extends java.io.Writer

This class represents an UTF-8 stream writer.

This writer supports surrogate char pairs (representing characters in the range [U+10000 .. U+10FFFF]). It can also be used to write characters from their unicodes (31 bits) directly (ref. write(int)).

Instances of this class can be reused for different output streams and can be part of a higher level component (e.g. serializer) in order to avoid dynamic buffer allocation when the destination output changes. Also wrapping using a java.io.BufferedWriter is unnescessary as instances of this class embed their own data buffers.

Note: This writer is unsynchronized and always produces well-formed UTF-8 sequences.

This class is public domain (not copyrighted).

Version:
4.6, July 14, 2003
Author:
Jean-Marie Dautelle
See Also:
Utf8StreamReader

Field Summary
 
Fields inherited from class java.io.Writer
lock
 
Constructor Summary
Utf8StreamWriter()
          Default constructor.
Utf8StreamWriter(int bufferSize)
          Creates a Utf8StreamWriter of specified buffer size.
 
Method Summary
 void close()
          Closes the stream, flushing it first.
 void flush()
          Flushes the stream.
 Utf8StreamWriter setOutputStream(java.io.OutputStream outStream)
          Sets the output stream to use for writing until this writer is closed.
 void write(char c)
          Writes a single character.
 void write(char[] cbuf, int off, int len)
          Writes a portion of an array of characters.
 void write(int code)
          Writes a character given its 31-bits Unicode.
 void write(java.lang.String str, int off, int len)
          Writes a portion of a string.
 
Methods inherited from class java.io.Writer
write, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Utf8StreamWriter

public Utf8StreamWriter()
Default constructor.


Utf8StreamWriter

public Utf8StreamWriter(int bufferSize)
Creates a Utf8StreamWriter of specified buffer size.

Parameters:
bufferSize - the buffer size in bytes.
Method Detail

setOutputStream

public Utf8StreamWriter setOutputStream(java.io.OutputStream outStream)
Sets the output stream to use for writing until this writer is closed. For example:
     Writer writer = new Utf8StreamWriter().setOutputStream(outStream);
 
is equivalent but writes faster than
     Writer writer = new java.io.OutputStreamWriter(outStream, "UTF-8");
 

Parameters:
outStream - the output stream.
Returns:
this UTF-8 writer.
See Also:
close()

write

public void write(char c)
           throws java.io.IOException
Writes a single character. This method supports 16-bits character surrogates.

Parameters:
c - char the character to be written (possibly a surrogate).
Throws:
java.io.IOException - if an I/O error occurs.

write

public void write(int code)
           throws java.io.IOException
Writes a character given its 31-bits Unicode.

Parameters:
code - the 31 bits Unicode of the character to be written.
Throws:
java.io.IOException - if an I/O error occurs.

write

public void write(char[] cbuf,
                  int off,
                  int len)
           throws java.io.IOException
Writes a portion of an array of characters.

Parameters:
cbuf - the array of characters.
off - the offset from which to start writing characters.
len - the number of characters to write.
Throws:
java.io.IOException - if an I/O error occurs.

write

public void write(java.lang.String str,
                  int off,
                  int len)
           throws java.io.IOException
Writes a portion of a string.

Parameters:
str - a String.
off - the offset from which to start writing characters.
len - the number of characters to write.
Throws:
java.io.IOException - if an I/O error occurs

flush

public void flush()
           throws java.io.IOException
Flushes the stream. If the stream has saved any characters from the various write() methods in a buffer, write them immediately to their intended destination. Then, if that destination is another character or byte stream, flush it. Thus one flush() invocation will flush all the buffers in a chain of Writers and OutputStreams.

Throws:
java.io.IOException - if an I/O error occurs.

close

public void close()
           throws java.io.IOException
Closes the stream, flushing it first. Once a stream has been closed, further write() or flush() invocations will cause an IOException to be thrown. Closing a previously-closed stream, however, has no effect.

Throws:
java.io.IOException - If an I/O error occurs

JADE v6.1

Copyright © 2004 Jean-Marie Dautelle.