implemented res.writeBinary() to directly write a java byte array to the response

This commit is contained in:
hns 2001-03-01 16:02:11 +00:00
parent d45f3240ce
commit 63b63db8b5

View file

@ -51,6 +51,7 @@ public class ResponseTrans implements Externalizable {
public void reset () { public void reset () {
if (buffer != null) if (buffer != null)
buffer.setLength (0); buffer.setLength (0);
response = null;
redirect = null; redirect = null;
skin = null; skin = null;
title = head = body = message = error = ""; title = head = body = message = error = "";
@ -140,18 +141,33 @@ public class ResponseTrans implements Externalizable {
throw new RedirectException (url); throw new RedirectException (url);
} }
/**
* Allow to directly set the byte array for the response. Calling this more than once will
* overwrite the previous output. We take a generic object as parameter to be able to
* generate a better error message, but it must be byte[].
*/
public void writeBinary (byte[] what) {
/* if (what == null || !(what instanceof byte[])) {
String type = what == null ? "null" : what.getClass ().getName();
throw new RuntimeException ("Parameter for res.writeBinary must be byte[], but was "+type);
} */
response = what;
}
/** /**
* This has to be called after writing to this response has finished and before it is shipped back to the * 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 char array to minimize size.
*/ */
public synchronized void close () { public synchronized void close () {
if (response == null) {
if (buffer != null) { if (buffer != null) {
response = buffer.toString ().getBytes (); response = buffer.toString ().getBytes ();
buffer = null; // make sure this is done only once, even with more requsts attached buffer = null; // make sure this is done only once, even with more requsts attached
} else { } else {
response = new byte[0]; response = new byte[0];
} }
}
notifyAll (); notifyAll ();
} }