01: /*
02: * Created on Feb 2, 2005
03: */
04: package uk.org.ponder.servletutil;
05:
06: import java.io.InputStream;
07: import java.io.OutputStream;
08:
09: import uk.org.ponder.streamutil.StreamCopier;
10: import uk.org.ponder.streamutil.StreamCopyUtil;
11: import uk.org.ponder.util.UniversalRuntimeException;
12:
13: /**
14: * @author Antranig Basman (antranig@caret.cam.ac.uk)
15: *
16: */
17: public class DirectPageRewriter implements StreamCopier {
18: public static final int COPY_BUF_SIZ = 4092;
19:
20: private byte[] header1bytes;
21: private byte[] header2bytes;
22: private byte[] footerbytes;
23:
24: static ThreadLocal bytebuffer = new ThreadLocal() {
25: public Object initialValue() {
26: return new byte[COPY_BUF_SIZ];
27: }
28: };
29:
30: // Header2 currently ignored.
31: public void setHeaderFooter(String header1, String header2,
32: String footer, String encoding) {
33: try {
34: header1bytes = header1.getBytes(encoding);
35: header2bytes = header2.getBytes(encoding);
36: footerbytes = footer.getBytes(encoding);
37: } catch (Throwable t) {
38: throw UniversalRuntimeException.accumulate(t,
39: "Error converting headers to encoding " + encoding);
40: }
41:
42: }
43:
44: public void copyStream(InputStream in, OutputStream out) {
45: byte[] copybuf = (byte[]) bytebuffer.get();
46: try {
47: out.write(header1bytes);
48: StreamCopyUtil.inputToOutput(in, out, true, false, copybuf);
49: out.write(footerbytes);
50: } catch (Throwable t) {
51: throw UniversalRuntimeException.accumulate(t,
52: "Error writing headers");
53: }
54: }
55:
56: }
|