* Make ResponseTrans extend java.io.Writer in order to simplify using it with
3rd party templating engines.
This commit is contained in:
parent
86e880c713
commit
ad8b7e11ec
2 changed files with 84 additions and 38 deletions
|
@ -31,7 +31,7 @@ import org.apache.xmlrpc.XmlRpcResponseProcessor;
|
|||
* A Transmitter for a response to the servlet client. Objects of this
|
||||
* class are directly exposed to JavaScript as global property res.
|
||||
*/
|
||||
public final class ResponseTrans implements Serializable {
|
||||
public final class ResponseTrans extends Writer implements Serializable {
|
||||
|
||||
static final long serialVersionUID = -8627370766119740844L;
|
||||
static final int INITIAL_BUFFER_SIZE = 2048;
|
||||
|
@ -263,45 +263,95 @@ public final class ResponseTrans implements Serializable {
|
|||
}
|
||||
|
||||
/**
|
||||
* Append a string to the response unchanged. This is often called
|
||||
* at the end of a request to write out the whole page, so if buffer
|
||||
* is uninitialized we just set it to the string argument.
|
||||
* Append a string to the response unchanged.
|
||||
*/
|
||||
public synchronized void write(Object what) {
|
||||
if (what != null) {
|
||||
String str = what.toString();
|
||||
|
||||
public synchronized void write(String str) {
|
||||
if (str != null) {
|
||||
if (buffer == null) {
|
||||
buffer = new StringBuffer(Math.max(str.length() + 100, INITIAL_BUFFER_SIZE));
|
||||
}
|
||||
|
||||
buffer.append(str);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends a objct to the response unchanged.
|
||||
* The object is first converted to a string.
|
||||
*/
|
||||
public void write(Object what) {
|
||||
if (what != null) {
|
||||
write(what.toString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends a part from a char array to the response buffer.
|
||||
*
|
||||
* @param chars
|
||||
* @param offset
|
||||
* @param length
|
||||
*/
|
||||
public synchronized void write(char[] chars, int offset, int length) {
|
||||
if (buffer == null) {
|
||||
buffer = new StringBuffer(Math.max(length + 100, INITIAL_BUFFER_SIZE));
|
||||
}
|
||||
buffer.append(chars, offset, length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends a char array to the response buffer.
|
||||
*
|
||||
* @param chars
|
||||
*/
|
||||
public void write(char chars[]) {
|
||||
write(chars, 0, chars.length);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Appends a signle character to the response buffer.
|
||||
* @param c
|
||||
*/
|
||||
public synchronized void write(int c) {
|
||||
if (buffer == null) {
|
||||
buffer = new StringBuffer(INITIAL_BUFFER_SIZE);
|
||||
}
|
||||
buffer.append((char) c);
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends a part from a string to the response buffer.
|
||||
* @param str
|
||||
* @param offset
|
||||
* @param length
|
||||
*/
|
||||
public void write(String str, int offset, int length) {
|
||||
char cbuf[] = new char[length];
|
||||
str.getChars(offset, (offset + length), cbuf, 0);
|
||||
write(cbuf, 0, length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write object to response buffer and append a platform dependent newline sequence.
|
||||
*/
|
||||
public synchronized void writeln(Object what) {
|
||||
write(what);
|
||||
|
||||
// if what is null, buffer may still be uninitialized
|
||||
if (buffer == null) {
|
||||
buffer = new StringBuffer(INITIAL_BUFFER_SIZE);
|
||||
}
|
||||
|
||||
buffer.append(newLine);
|
||||
}
|
||||
|
||||
/**
|
||||
* Append a part from a char array to the response buffer.
|
||||
* Writes a platform dependent newline sequence to response buffer.
|
||||
*/
|
||||
public synchronized void writeCharArray(char[] c, int start, int length) {
|
||||
public synchronized void writeln() {
|
||||
// buffer may still be uninitialized
|
||||
if (buffer == null) {
|
||||
buffer = new StringBuffer(Math.max(length, INITIAL_BUFFER_SIZE));
|
||||
buffer = new StringBuffer(INITIAL_BUFFER_SIZE);
|
||||
}
|
||||
|
||||
buffer.append(c, start, length);
|
||||
buffer.append(newLine);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -383,21 +433,6 @@ public final class ResponseTrans implements Serializable {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @param str ...
|
||||
*/
|
||||
public synchronized void append(String str) {
|
||||
if (str != null) {
|
||||
if (buffer == null) {
|
||||
buffer = new StringBuffer(Math.max(str.length(), INITIAL_BUFFER_SIZE));
|
||||
}
|
||||
|
||||
buffer.append(str);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
|
@ -492,9 +527,21 @@ public final class ResponseTrans implements Serializable {
|
|||
writeBinary(xresproc.encodeException(x, charset));
|
||||
}
|
||||
|
||||
public void flush() {
|
||||
// does nothing!
|
||||
}
|
||||
|
||||
/**
|
||||
* This has to be called after writing to this response has finished and before it is shipped back to the
|
||||
* web server. Transforms the string buffer into a char array to minimize size.
|
||||
* web server. Transforms the string buffer into a byte array for transmission.
|
||||
*/
|
||||
public void close() throws UnsupportedEncodingException {
|
||||
close(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* This has to be called after writing to this response has finished and before it is shipped back to the
|
||||
* web server. Transforms the string buffer into a byte array for transmission.
|
||||
*/
|
||||
public synchronized void close(String cset) throws UnsupportedEncodingException {
|
||||
// if the response was already written and committed by the application
|
||||
|
@ -842,5 +889,4 @@ public final class ResponseTrans implements Serializable {
|
|||
|
||||
return c;
|
||||
}
|
||||
|
||||
}
|
|
@ -155,7 +155,7 @@ public final class Skin {
|
|||
}
|
||||
|
||||
if (macros == null) {
|
||||
reval.getResponse().writeCharArray(source, 0, sourceLength);
|
||||
reval.getResponse().write(source, 0, sourceLength);
|
||||
reval.skinDepth--;
|
||||
|
||||
return;
|
||||
|
@ -171,7 +171,7 @@ public final class Skin {
|
|||
|
||||
for (int i = 0; i < macros.length; i++) {
|
||||
if (macros[i].start > written) {
|
||||
reval.getResponse().writeCharArray(source, written, macros[i].start - written);
|
||||
reval.getResponse().write(source, written, macros[i].start - written);
|
||||
}
|
||||
|
||||
macros[i].render(reval, thisObject, paramObject, handlerCache);
|
||||
|
@ -179,7 +179,7 @@ public final class Skin {
|
|||
}
|
||||
|
||||
if (written < sourceLength) {
|
||||
reval.getResponse().writeCharArray(source, written, sourceLength - written);
|
||||
reval.getResponse().write(source, written, sourceLength - written);
|
||||
}
|
||||
} finally {
|
||||
reval.skinDepth--;
|
||||
|
|
Loading…
Add table
Reference in a new issue