org.apache.xalan.serialize
Class WriterToUTF8Buffered

java.lang.Object
  extended byjava.io.Writer
      extended byorg.apache.xalan.serialize.WriterToUTF8Buffered

public final class WriterToUTF8Buffered
extends Writer

This class writes ASCII to a byte stream as quickly as possible. For the moment it does not do buffering, though I reserve the right to do some buffering down the line if I can prove that it will be faster even if the output stream is buffered.


Field Summary
private  byte[] buf
          The internal buffer where data is stored.
private  int count
          The number of valid bytes in the buffer.
private  OutputStream m_os
          The byte stream to write to.
 
Fields inherited from class java.io.Writer
lock
 
Constructor Summary
WriterToUTF8Buffered(OutputStream out)
          Create an buffered UTF-8 writer.
WriterToUTF8Buffered(OutputStream out, int size)
          Create an buffered UTF-8 writer to write data to the specified underlying output stream with the specified buffer size.
 
Method Summary
 void close()
          Close the stream, flushing it first.
 void flush()
          Flush the stream.
 void flushBuffer()
          Flush the internal buffer
 OutputStream getOutputStream()
          Get the output stream where the events will be serialized to.
 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(String s)
          Write a string.
private  void writeDirect(char[] chars, int start, int length)
          Write a portion of an array of characters.
private  void writeDirect(String s)
          Write 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
 

Field Detail

m_os

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


buf

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


count

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

Constructor Detail

WriterToUTF8Buffered

public WriterToUTF8Buffered(OutputStream out)
                     throws UnsupportedEncodingException
Create an buffered UTF-8 writer.

Parameters:
out - the underlying output stream.
Throws:
UnsupportedEncodingException

WriterToUTF8Buffered

public WriterToUTF8Buffered(OutputStream out,
                            int size)
Create an buffered UTF-8 writer to write data to the specified underlying output stream with the specified buffer size.

Parameters:
out - the underlying output stream.
size - the buffer size.
Throws:
IllegalArgumentException - if size <= 0.
Method Detail

write

public void write(int c)
           throws 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.

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

writeDirect

private final void writeDirect(char[] chars,
                               int start,
                               int length)
                        throws IOException
Write a portion of an array of characters.

Parameters:
chars - Array of characters
start - Offset from which to start writing characters
length - Number of characters to write
Throws:
IOException - If an I/O error occurs
IOException

writeDirect

private final void writeDirect(String s)
                        throws IOException
Write a string.

Parameters:
s - String to be written
Throws:
IOException - If an I/O error occurs

write

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

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

write

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

Overrides:
write in class Writer
Parameters:
s - String to be written
Throws:
IOException - If an I/O error occurs

flushBuffer

public void flushBuffer()
                 throws IOException
Flush the internal buffer

Throws:
IOException

flush

public void flush()
           throws 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 class Writer
Throws:
IOException - If an I/O error occurs
IOException

close

public void close()
           throws 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 class Writer
Throws:
IOException - If an I/O error occurs
IOException

getOutputStream

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

Returns:
reference to the result stream, or null of only a writer was set.