org.webmacro
Class FastWriter

java.lang.Object
  |
  +--java.io.Writer
        |
        +--org.webmacro.FastWriter

public class FastWriter
extends java.io.Writer

FastWriter attempts to optimize output speed in a WebMacro template through several specific optimizations:

Note that if you turn on the asciiHack and then write non-ASCII data the output will be mangled.

Note that the FastWriter requires an explicit flush

If you re-use a FastWriter you must re-use it in a context which uses the SAME unicode conversion. The caches and internal data structures which the FastWriter allocates are tied to the encoding it was created with.


Field Summary
static int DEFAULT_BUFFER_SIZE
           
static int MAX_POOL_SIZE
           
static java.lang.String SAFE_UNICODE_ENCODING
          This encoding is either UTF16-BE or, if the platform does not support it, UTF8.
 
Fields inherited from class java.io.Writer
lock
 
Constructor Summary
FastWriter(Broker broker, java.io.OutputStream out, java.lang.String encoding)
          Create a FastWriter to the target outputstream.
FastWriter(Broker broker, java.lang.String encoding)
          Create a new FastWriter with no output stream target.
 
Method Summary
 void bflush()
           
 void close()
          Return the FastWriter to the queue for later re-use.
 void flush()
          Flush all data out to the OutputStream, if any, clearing the internal buffers.
 boolean getAsciiHack()
          Returns true if we are mangling the unicode conversion in an attempt to eek out a bit of extra efficiency.
 Encoder getEncoder()
          Get the encoder used by this FastWriter to transform char[] data into byte[] data.
 java.lang.String getEncoding()
          Get the character encoding this FastWriter uses to convert characters to byte[]
static FastWriter getInstance(Broker broker)
          Return a FastWriter with default encoding and no output stream.
static FastWriter getInstance(Broker broker, java.io.OutputStream out, java.lang.String encoding)
          Get a new FastWriter.
static FastWriter getInstance(Broker broker, java.lang.String encoding)
          Return a FastWriter with the specified encoding and no output stream.
 java.io.OutputStream getOutputStream()
          Get the output stream this FastWriter sends output to.
 void reset(java.io.OutputStream out)
          Reset the fastwriter, clearing any contents that have been generated so far.
 void setAsciiHack(boolean on)
          Ordinarily an expensive char-to-byte routine is used to convert strings and char[]'s to byte format.
 int size()
          Return the number of bytes that would be written out if flush() is called.
 byte[] toByteArray()
          Copy the contents written so far into a byte array.
 java.lang.String toString()
          Copy the contents written so far into a String.
 void write(byte[] rawBytes)
          Write raw bytes to the underlying stream.
 void write(byte[] rawBytes, int offset, int len)
          Write raw bytes to the underlying stream.
 void write(char[] cbuf)
          Write characters to the output stream performing slow unicode conversion unless AsciiHack is on.
 void write(char[] cbuf, int offset, int len)
          Write characters to to the output stream performing slow unicode conversion unless the AsciiHack is on.
 void write(int c)
          Write a single character, performing slow unicode conversion unless AsciiHack is on.
 void write(java.lang.String s)
          Write a string to the underlying output stream, performing unicode conversion.
 void write(java.lang.String s, int off, int len)
           
 void writeStatic(java.lang.String s)
          Write a string to the underlying output stream, performing unicode conversion if necessary--try and read the encoding from an encoding cache if possible.
 void writeTo(java.io.OutputStream out)
          Copy the contents written so far to the suppiled output stream
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

SAFE_UNICODE_ENCODING

public static final java.lang.String SAFE_UNICODE_ENCODING
This encoding is either UTF16-BE or, if the platform does not support it, UTF8. It is a Unicode encoding which can have encoded strings concatenated together.

DEFAULT_BUFFER_SIZE

public static final int DEFAULT_BUFFER_SIZE

MAX_POOL_SIZE

public static final int MAX_POOL_SIZE
Constructor Detail

FastWriter

public FastWriter(Broker broker,
                  java.io.OutputStream out,
                  java.lang.String encoding)
           throws java.io.UnsupportedEncodingException
Create a FastWriter to the target outputstream. You must specify a character encoding. You can also call writeTo(), toString(), and toByteArray() to access any un-flush()ed contents.

FastWriter

public FastWriter(Broker broker,
                  java.lang.String encoding)
           throws java.io.UnsupportedEncodingException
Create a new FastWriter with no output stream target. You can still call writeTo(), toString(), and toByteArray().
Method Detail

getEncoding

public java.lang.String getEncoding()
Get the character encoding this FastWriter uses to convert characters to byte[]

getEncoder

public Encoder getEncoder()
Get the encoder used by this FastWriter to transform char[] data into byte[] data.

getOutputStream

public java.io.OutputStream getOutputStream()
Get the output stream this FastWriter sends output to. It may be null, in which case output is not sent anywhere.

setAsciiHack

public void setAsciiHack(boolean on)
Ordinarily an expensive char-to-byte routine is used to convert strings and char[]'s to byte format. If you know that your data is going to be ASCII only for some number of writes, turn on this AsciiHack and then write the ASCII data. It's much faster. Remember to turn the AsciiHack off before writing true Unicode characters, otherwise they'll be mangled.

getAsciiHack

public boolean getAsciiHack()
Returns true if we are mangling the unicode conversion in an attempt to eek out a bit of extra efficiency.

write

public void write(char[] cbuf)
           throws java.io.IOException
Write characters to the output stream performing slow unicode conversion unless AsciiHack is on.
Overrides:
write in class java.io.Writer

write

public void write(char[] cbuf,
                  int offset,
                  int len)
           throws java.io.IOException
Write characters to to the output stream performing slow unicode conversion unless the AsciiHack is on.
Overrides:
write in class java.io.Writer

write

public void write(int c)
           throws java.io.IOException
Write a single character, performing slow unicode conversion unless AsciiHack is on.
Overrides:
write in class java.io.Writer

write

public void write(java.lang.String s)
           throws java.io.IOException
Write a string to the underlying output stream, performing unicode conversion.
Overrides:
write in class java.io.Writer

write

public void write(java.lang.String s,
                  int off,
                  int len)
           throws java.io.IOException
Overrides:
write in class java.io.Writer

writeStatic

public void writeStatic(java.lang.String s)
Write a string to the underlying output stream, performing unicode conversion if necessary--try and read the encoding from an encoding cache if possible.

write

public void write(byte[] rawBytes)
Write raw bytes to the underlying stream. These bytes must be properly encoded with the encoding returned by getEncoding().

write

public void write(byte[] rawBytes,
                  int offset,
                  int len)
Write raw bytes to the underlying stream. Tehse bytes must be properly encoded witht he encoding returned by getEncoding()

bflush

public void bflush()

flush

public void flush()
           throws java.io.IOException
Flush all data out to the OutputStream, if any, clearing the internal buffers. Note that data is ONLY written to the output stream on a flush() operation, and never at any other time. Consequently this is one of the few places that you may actually encounter an IOException when using the FastWriter class.
Overrides:
flush in class java.io.Writer

size

public int size()
         throws java.io.IOException
Return the number of bytes that would be written out if flush() is called.

toByteArray

public byte[] toByteArray()
Copy the contents written so far into a byte array.

toString

public java.lang.String toString()
Copy the contents written so far into a String.
Overrides:
toString in class java.lang.Object

writeTo

public void writeTo(java.io.OutputStream out)
             throws java.io.IOException
Copy the contents written so far to the suppiled output stream

reset

public void reset(java.io.OutputStream out)
Reset the fastwriter, clearing any contents that have been generated so far.

getInstance

public static FastWriter getInstance(Broker broker,
                                     java.io.OutputStream out,
                                     java.lang.String encoding)
                              throws java.io.UnsupportedEncodingException
Get a new FastWriter. You must then call writeTo(..) before attempting to write to the FastWriter.

getInstance

public static FastWriter getInstance(Broker broker,
                                     java.lang.String encoding)
                              throws java.io.UnsupportedEncodingException
Return a FastWriter with the specified encoding and no output stream.

getInstance

public static FastWriter getInstance(Broker broker)
Return a FastWriter with default encoding and no output stream.

close

public void close()
           throws java.io.IOException
Return the FastWriter to the queue for later re-use. You must not use the FastWriter after this call. Calling close() returns the FastWriter to the pool. If you don't want to return it to the pool just discard it without a close().
Overrides:
close in class java.io.Writer