01: /*
02: * @author <a href="mailto:novotny@gridsphere.org">Jason Novotny</a>
03: * @version $Id: StoredPortletResponseImpl.java 6385 2007-10-25 14:02:26Z wehrens $
04: */
05: package org.gridsphere.portlet.impl;
06:
07: import javax.servlet.ServletOutputStream;
08: import javax.servlet.http.HttpServletRequest;
09: import javax.servlet.http.HttpServletResponse;
10: import java.io.IOException;
11: import java.io.PrintWriter;
12: import java.io.Writer;
13:
14: public class StoredPortletResponseImpl extends RenderResponseImpl {
15:
16: private PrintWriter writer;
17:
18: private class StoredServletOutputStreamImpl extends
19: ServletOutputStream {
20:
21: protected Writer writer;
22:
23: public StoredServletOutputStreamImpl(Writer writer) {
24: this .writer = writer;
25: }
26:
27: public void write(int b) throws IOException {
28: writer.write(b);
29: }
30:
31: public String toString() {
32: return writer.toString();
33: }
34:
35: }
36:
37: public StoredPortletResponseImpl(HttpServletRequest request,
38: HttpServletResponse response, Writer writer) {
39: super (request, response);
40: this .writer = new PrintWriter(writer);
41: }
42:
43: /**
44: * Writes an array of bytes
45: *
46: * @param buf the array to be written
47: * @throws IOException if an I/O error occurred
48: */
49: public void write(byte[] buf) throws IOException {
50: char[] tmp = new char[buf.length];
51: for (int i = 0; i < tmp.length; i++)
52: tmp[i] = (char) (buf[i] & 0xff);
53: writer.write(tmp, 0, buf.length);
54: }
55:
56: /**
57: * Writes a single byte to the output stream
58: */
59: public void write(int val) throws IOException {
60: writer.write(val);
61: }
62:
63: /**
64: * Writes a subarray of bytes
65: *
66: * @param buf the array to be written
67: * @param pOffset the offset into the array
68: * @param length the number of bytes to write
69: * @throws IOException if an I/O error occurred
70: */
71: public void write(byte[] buf, int pOffset, int length)
72: throws IOException {
73: char[] tmp = new char[length];
74: for (int i = 0; i < length; i++)
75: tmp[i] = (char) (buf[i + pOffset] & 0xff);
76: writer.write(tmp, 0, length);
77: }
78:
79: public PrintWriter getWriter() throws IOException {
80: return writer;
81: }
82:
83: public ServletOutputStream getOutputStream() throws IOException {
84: return new StoredServletOutputStreamImpl(writer);
85: }
86:
87: public void flushBuffer() throws IOException {
88: writer.flush();
89: }
90:
91: }
|