org.apache.xml.serializer
Class WriterToUTF8Buffered

java.lang.Object
  extended by java.io.Writer
      extended by org.apache.xml.serializer.WriterToUTF8Buffered
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable, java.lang.Appendable, WriterChain

final class WriterToUTF8Buffered
extends java.io.Writer
implements WriterChain

This class writes unicode characters to a byte stream (java.io.OutputStream) as quickly as possible. It buffers the output in an internal buffer which must be flushed to the OutputStream when done. This flushing is done via the close() flush() or flushBuffer() method. This class is only used internally within Xalan.


Field Summary
private static int BYTES_MAX
          number of bytes that the byte buffer can hold.
private static int CHARS_MAX
          number of characters that the character buffer can hold.
private  int count
          The number of valid bytes in the buffer.
private  char[] m_inputChars
           
private  java.io.OutputStream m_os
          The byte stream to write to.
private  byte[] m_outputBytes
          The internal buffer where data is stored.
 
Fields inherited from class java.io.Writer
lock
 
Constructor Summary
WriterToUTF8Buffered(java.io.OutputStream out)
          Create an buffered UTF-8 writer.
 
Method Summary
 void close()
          Close the stream, flushing it first.
 void flush()
          Flush the stream.
 void flushBuffer()
          Flush the internal buffer
 java.io.OutputStream getOutputStream()
          Get the output stream where the events will be serialized to.
 java.io.Writer getWriter()
          If this method returns null, getOutputStream() must return non-null.
 void write(char[] chars, int start, int length)
          Write a portion of an array of characters.
 void write(int c)
          Write a single character.
 void write(java.lang.String s)
          Write a string.
 
Methods inherited from class java.io.Writer
append, append, append, write, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.apache.xml.serializer.WriterChain
write, write
 

Field Detail

BYTES_MAX

private static final int BYTES_MAX
number of bytes that the byte buffer can hold. This is a fixed constant is used rather than m_outputBytes.lenght for performance.

See Also:
Constant Field Values

CHARS_MAX

private static final int CHARS_MAX
number of characters that the character buffer can hold. This is 1/3 of the number of bytes because UTF-8 encoding can expand one unicode character by up to 3 bytes.

See Also:
Constant Field Values

m_os

private final java.io.OutputStream m_os
The byte stream to write to. (sc & sb remove final to compile in JDK 1.1.8)


m_outputBytes

private final byte[] m_outputBytes
The internal buffer where data is stored. (sc & sb remove final to compile in JDK 1.1.8)


m_inputChars

private final char[] m_inputChars

count

private int count
The number of valid bytes in the buffer. This value is always in the range 0 through m_outputBytes.length; elements m_outputBytes[0] through m_outputBytes[count-1] contain valid byte data.

Constructor Detail

WriterToUTF8Buffered

public WriterToUTF8Buffered(java.io.OutputStream out)
Create an buffered UTF-8 writer.

Parameters:
out - the underlying output stream.
Throws:
java.io.UnsupportedEncodingException
Method Detail

write

public void write(int c)
           throws java.io.IOException
Write a single character. The character to be written is contained in the 16 low-order bits of the given integer value; the 16 high-order bits are ignored.

Subclasses that intend to support efficient single-character output should override this method.

Specified by:
write in interface WriterChain
Overrides:
write in class java.io.Writer
Parameters:
c - int specifying a character to be written.
Throws:
java.io.IOException - If an I/O error occurs

write

public void write(char[] chars,
                  int start,
                  int length)
           throws java.io.IOException
Write a portion of an array of characters.

Specified by:
write in interface WriterChain
Specified by:
write in class java.io.Writer
Parameters:
chars - Array of characters
start - Offset from which to start writing characters
length - Number of characters to write
Throws:
java.io.IOException - If an I/O error occurs
java.io.IOException

write

public void write(java.lang.String s)
           throws java.io.IOException
Write a string.

Specified by:
write in interface WriterChain
Overrides:
write in class java.io.Writer
Parameters:
s - String to be written
Throws:
java.io.IOException - If an I/O error occurs

flushBuffer

public void flushBuffer()
                 throws java.io.IOException
Flush the internal buffer

Throws:
java.io.IOException

flush

public void flush()
           throws java.io.IOException
Flush 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.

Specified by:
flush in interface java.io.Flushable
Specified by:
flush in interface WriterChain
Specified by:
flush in class java.io.Writer
Throws:
java.io.IOException - If an I/O error occurs
java.io.IOException

close

public void close()
           throws java.io.IOException
Close 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.

Specified by:
close in interface java.io.Closeable
Specified by:
close in interface WriterChain
Specified by:
close in class java.io.Writer
Throws:
java.io.IOException - If an I/O error occurs
java.io.IOException

getOutputStream

public java.io.OutputStream getOutputStream()
Get the output stream where the events will be serialized to.

Specified by:
getOutputStream in interface WriterChain
Returns:
reference to the result stream, or null of only a writer was set.

getWriter

public java.io.Writer getWriter()
Description copied from interface: WriterChain
If this method returns null, getOutputStream() must return non-null. Get the writer that this writer sends its output to. It is possible that the Writer returned by this method does not implement the WriterChain interface.

Specified by:
getWriter in interface WriterChain