com.novell.io
Class EncodingStreamWriter

java.lang.Object
  extended byjava.io.Writer
      extended bycom.novell.io.EncodingStreamWriter

public class EncodingStreamWriter
extends Writer

This class provides the same functionality as java.io.OutputStreamWriter but also allows unencodable characters to be handled with other than a replacement character.

Via the CharEncoder-derived classes, it current provides implementations of the following encodings: ASCII, UTF-8, UTF-16, ISO-8859-1 though -9, -15 and -15.

This class has been updated to use java.nio.charset.CharsetEncoder instances for encodings not supported directly by this package.

When a character that is output cannot be encoded in the underlying encoding, this class throws com.novell.io.CharacterEncodingException to allow callers to handle the character. Note that wrapping this class in a java.io.BufferedWriter will not allow the use of this mechanism, since the actual encoding of the characters will likely happen after the calls that write the characters. However this Writer class has an internal buffer that is used to store the encoded bytes and the encoded bytes will therefore not be written to the underlying stream immediately.

This class is initially designed for use with XML serialization which has an escape mechanism for outputting characters that cannot be encoded in a particular scheme


Field Summary
 
Fields inherited from class java.io.Writer
lock
 
Constructor Summary
EncodingStreamWriter(OutputStream stream, CharEncoder encoder)
          Construct an EncodingStreamWriter with the passed encoder
EncodingStreamWriter(OutputStream stream, String encoding)
          Construct an EncodingStreamWriter that supports the passed encoding scheme
 
Method Summary
 void close()
          Close the stream, flushing it first.
 void flush()
          Flush the stream
 int getBufferSize()
          Return the size of the internal buffer
 String getEncoding()
          Return the encoding used by this EncodingStreamWriter
 void setBufferSize(int size)
          Set the buffer size of this Writer
protected  boolean tryFlush()
           
 void write(char[] buf)
          Write characters to the stream
 void write(char[] buf, int off, int len)
          Write characters to the stream
 void write(int c)
          Write a single character to the stream
 void write(String str)
          Write characters to the stream
 void write(String str, int off, int len)
          Write characters to the stream
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

EncodingStreamWriter

public EncodingStreamWriter(OutputStream stream,
                            String encoding)
                     throws UnsupportedEncodingException
Construct an EncodingStreamWriter that supports the passed encoding scheme

Parameters:
stream - The underlying stream to which to output the bytes
encoding - The desired encoding
Throws:
UnsupportedEncodingException - Thrown if the desired encoding is not available.

EncodingStreamWriter

public EncodingStreamWriter(OutputStream stream,
                            CharEncoder encoder)
Construct an EncodingStreamWriter with the passed encoder

Parameters:
stream - The underlying stream to which to output the bytes
encoder - the CharEncoder to use
Method Detail

setBufferSize

public void setBufferSize(int size)
                   throws IOException
Set the buffer size of this Writer

Parameters:
size - The desired size, in bytes of the buffer
Throws:
IOException

getBufferSize

public int getBufferSize()
Return the size of the internal buffer

Returns:
The size, in bytes, of the internal buffer

getEncoding

public String getEncoding()
Return the encoding used by this EncodingStreamWriter

Returns:
The encoding used by this EncodingStreamWriter

close

public void close()
           throws IOException
Close the stream, flushing it first.

Throws:
IOException

flush

public void flush()
           throws IOException
Flush the stream

Throws:
IOException

write

public void write(int c)
           throws IOException
Write a single character to the stream

Parameters:
c - The char to be written (upper 16 bits ignored)
Throws:
IOException

write

public void write(char[] buf)
           throws IOException
Write characters to the stream

Parameters:
buf - The array of characters to write
Throws:
IOException

write

public void write(char[] buf,
                  int off,
                  int len)
           throws IOException
Write characters to the stream

Parameters:
buf - The array of characters
off - The starting offset within the array
len - The number of characters to write, starting at off
Throws:
IOException

write

public void write(String str)
           throws IOException
Write characters to the stream

Parameters:
str - The characters to write
Throws:
IOException

write

public void write(String str,
                  int off,
                  int len)
           throws IOException
Write characters to the stream

Parameters:
str - The characters to write
off - The starting offset within the string
len - The number of characters to write, starting at off
Throws:
IOException

tryFlush

protected boolean tryFlush()
                    throws IOException
Throws:
IOException