|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.io.Writer
com.claritysys.io.CharArrayWriter
public final class CharArrayWriter
A high performance, single-threaded writer that appends all writes to an output buffer, much like ByteArrayOutputStream. The buffer grows as needed.
Create one of these with a large buffer and reuse it from request to request.
It is not multi-thread safe!
You must ensure that only one servlet request is accessing it at a time, by allocating CharArrayWriters from a pool or some other technique.
There are several performance characteristics of this writer:
One of the challenges in using this will be to write the char[] to a byte stream. It has been found that OutputStreamWriter has disgusting performance characteristics.
The reason this class is a CharArray writer and not a ByteArray writer is because most of the output is String objects. The String class provides a method for copying the String's internal char[] directly to another char[] using System.arraycopy, without producing any objects during the copy. If it were a byte[], we would have to loop through the String's buffer (after getting a copy of it) and convert each char to a byte using the proper encoding (ASCII, UTF-8, etc). The String class has a getBytes method, but that produces a new byte[]. Object creation is the bane of performance.
Field Summary |
---|
Fields inherited from class java.io.Writer |
---|
lock |
Constructor Summary | |
---|---|
CharArrayWriter(int size)
Create a new writer with a buffer size given. |
Method Summary | |
---|---|
void |
close()
Does nothing. |
void |
flush()
Does nothing. |
char[] |
getBuffer()
Get the current char[]. |
int |
getBufferSize()
Get the capacity of the buffer, that is the length of the char[]. |
int |
getCount()
Return the current number of characters in the buffer. |
void |
print(char c)
Print a single char. |
void |
print(int i)
Print an int directly to the buffer, without doing a Integer.toString first to avoid creating unnecessary objects. |
void |
print(java.lang.String str)
"Print" a String. |
void |
reset()
Reset the current number of characters in the buffer to zero. |
void |
toArray(byte[] bytes)
Write the char[] into a byte[], while assuming all characters are actually ASCII (UTF-8), which HTML is, isn't it? |
java.lang.String |
toString()
Create a new String object and copy the current buffer into the Strings char buffer. |
void |
write(char[] cbuf,
int off,
int len)
Write a portion of an array of characters. |
void |
write(java.lang.String str)
Write a string. |
void |
write(java.lang.String str,
int off,
int len)
Write a portion of 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, wait, wait, wait |
Constructor Detail |
---|
public CharArrayWriter(int size)
size
- The size of the char[] in characters.Method Detail |
---|
public int getCount()
getBufferSize()
public void reset()
This acts like "flushing" the buffer, but it just discards the current contents. It literally just sets the current count to zero.
public char[] getBuffer()
DO NOT MODIFY THE CHAR[].
This is the actualy char[] (for performance) in use by the writer,
You must call getCount()
to know how many characters this
buffer actually contains.
public int getBufferSize()
getCount()
public java.lang.String toString()
Thus this method creates 2 objects, the new String and its internal char[]. It then copies the characters into the String's char[] using System.arraycopy.
Generally not good for performance.
toString
in class java.lang.Object
public void write(java.lang.String str)
write
in class java.io.Writer
str
- String to be writtenpublic void write(char[] cbuf, int off, int len)
write
in class java.io.Writer
cbuf
- Array of charactersoff
- Offset from which to start writing characterslen
- Number of characters to writepublic void write(java.lang.String str, int off, int len)
write
in class java.io.Writer
str
- A Stringoff
- Offset from which to start writing characterslen
- Number of characters to writepublic void flush()
flush
in interface java.io.Flushable
flush
in class java.io.Writer
public void close()
close
in interface java.io.Closeable
close
in class java.io.Writer
public void print(java.lang.String str)
write(String)
. It makes
this writer look like a PrintWriter.
str
- The string to print.public void print(char c)
c
- public void print(int i)
i
- The int to print to the buffer.public void toArray(byte[] bytes)
bytes
-
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |