001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.server.connection;
031:
032: import com.caucho.log.Log;
033: import com.caucho.util.L10N;
034: import com.caucho.vfs.TempBuffer;
035:
036: import javax.servlet.ServletOutputStream;
037: import javax.servlet.http.HttpServletResponse;
038: import java.io.IOException;
039: import java.io.PrintWriter;
040: import java.util.logging.Level;
041: import java.util.logging.Logger;
042:
043: public class WrapperResponseStream extends AbstractResponseStream {
044: static final Logger log = Log.open(WrapperResponseStream.class);
045:
046: static final L10N L = new L10N(WrapperResponseStream.class);
047:
048: private static final int SIZE = TempBuffer.SIZE;
049:
050: private final byte[] _byteBuffer = new byte[SIZE];
051: private final char[] _charBuffer = new char[SIZE];
052:
053: private HttpServletResponse _next;
054:
055: private ServletOutputStream _os;
056: private PrintWriter _writer;
057:
058: public WrapperResponseStream() {
059: }
060:
061: public void init(HttpServletResponse next) {
062: if (next == null)
063: throw new NullPointerException();
064:
065: _next = next;
066: _os = null;
067: _writer = null;
068: }
069:
070: /**
071: * Returns true for a caucho response stream.
072: */
073: public boolean isCauchoResponseStream() {
074: if (_next instanceof AbstractHttpResponse)
075: return ((AbstractHttpResponse) _next)
076: .isCauchoResponseStream();
077: else
078: return false;
079: }
080:
081: /**
082: * Sets the buffer size.
083: */
084: public void setBufferSize(int size) {
085: _next.setBufferSize(size);
086: }
087:
088: /**
089: * Gets the buffer size.
090: */
091: public int getBufferSize() {
092: return _next.getBufferSize();
093: }
094:
095: /**
096: * Returns the remaining buffer entries.
097: */
098: public int getRemaining() {
099: //return _next.getRemaining();
100: return 0;
101: }
102:
103: /**
104: * Returns the char buffer.
105: */
106: public char[] getCharBuffer() {
107: return _charBuffer;
108: }
109:
110: /**
111: * Returns the char buffer offset.
112: */
113: public int getCharOffset() {
114: return 0;
115: }
116:
117: /**
118: * Sets the char buffer offset.
119: */
120: public void setCharOffset(int offset) {
121: if (offset > 0) {
122: try {
123: print(_charBuffer, 0, offset);
124: } catch (IOException e) {
125: log.log(Level.FINE, e.toString(), e);
126: }
127: }
128: }
129:
130: /**
131: * Returns the next char buffer.
132: */
133: public char[] nextCharBuffer(int offset) throws IOException {
134: if (offset > 0)
135: print(_charBuffer, 0, offset);
136:
137: return _charBuffer;
138: }
139:
140: /**
141: * Writes the next chunk of data to the response stream.
142: *
143: * @param buf the buffer containing the data
144: * @param offset start offset into the buffer
145: * @param length length of the data in the buffer
146: */
147: public void print(int ch) throws IOException {
148: if (_writer == null) {
149: if (_next == null)
150: return;
151:
152: _writer = _next.getWriter();
153: }
154:
155: _writer.write(ch);
156: }
157:
158: /**
159: * Writes the next chunk of data to the response stream.
160: *
161: * @param buf the buffer containing the data
162: * @param offset start offset into the buffer
163: * @param length length of the data in the buffer
164: */
165: public void print(char[] buffer, int offset, int length)
166: throws IOException {
167: if (length == 0)
168: return;
169:
170: if (_writer == null) {
171: if (_next == null)
172: return;
173:
174: _writer = _next.getWriter();
175: }
176:
177: _writer.write(buffer, offset, length);
178: }
179:
180: /**
181: * Returns the buffer offset.
182: */
183: public int getBufferOffset() {
184: return 0;
185: }
186:
187: /**
188: * Sets the byte buffer offset.
189: */
190: public void setBufferOffset(int offset) {
191: if (offset > 0) {
192: try {
193: write(_byteBuffer, 0, offset);
194: } catch (IOException e) {
195: log.log(Level.FINE, e.toString(), e);
196: }
197: }
198: }
199:
200: /**
201: * Gets the byte buffer
202: */
203: public byte[] getBuffer() {
204: return _byteBuffer;
205: }
206:
207: /**
208: * Returns the next buffer.
209: */
210: public byte[] nextBuffer(int offset) throws IOException {
211: if (offset > 0)
212: write(_byteBuffer, 0, offset);
213:
214: return _byteBuffer;
215: }
216:
217: /**
218: * Writes the next chunk of data to the response stream.
219: *
220: * @param buf the buffer containing the data
221: * @param offset start offset into the buffer
222: * @param length length of the data in the buffer
223: */
224: public void write(int ch) throws IOException {
225: if (_os == null) {
226: if (_next == null)
227: return;
228:
229: _os = _next.getOutputStream();
230: }
231:
232: _os.write(ch);
233: }
234:
235: /**
236: * Writes the next chunk of data to the response stream.
237: *
238: * @param buf the buffer containing the data
239: * @param offset start offset into the buffer
240: * @param length length of the data in the buffer
241: */
242: public void write(byte[] buf, int offset, int length)
243: throws IOException {
244: if (length == 0)
245: return;
246:
247: if (_os == null) {
248: if (_next == null)
249: return;
250:
251: _os = _next.getOutputStream();
252: }
253:
254: _os.write(buf, offset, length);
255: }
256:
257: /**
258: * Writes the char buffer to the output stream.
259: */
260: public void flushCharBuffer() throws IOException {
261: }
262:
263: /**
264: * Flushes the buffer.
265: */
266: public void flushBuffer() throws IOException {
267: }
268:
269: /**
270: * Flushes the buffer.
271: */
272: public void flushChar() throws IOException {
273: if (_writer == null) {
274: if (_next == null)
275: return;
276:
277: _writer = _next.getWriter();
278: }
279:
280: if (_writer != null)
281: _writer.flush();
282: }
283:
284: /**
285: * Flushes the buffer.
286: */
287: public void flushByte() throws IOException {
288: if (_os == null) {
289: if (_next == null)
290: return;
291:
292: _os = _next.getOutputStream();
293: }
294:
295: if (_os != null)
296: _os.flush();
297: }
298:
299: /**
300: * Clears the buffer.
301: */
302: public void clearBuffer() {
303: if (_next != null)
304: _next.resetBuffer();
305: }
306:
307: /**
308: * Flushes the output.
309: */
310: public void flush() throws IOException {
311: if (_writer != null)
312: _writer.flush();
313:
314: if (_os != null)
315: _os.flush();
316: }
317:
318: /**
319: * Finish.
320: */
321: public void finish() throws IOException {
322: /*
323: if (_writer != null)
324: _writer.flush();
325:
326: if (_os != null)
327: _os.flush();
328: */
329:
330: _next = null;
331: _os = null;
332: _writer = null;
333: }
334:
335: /**
336: * Close.
337: */
338: public void close() throws IOException {
339: // jsp/17f0
340: PrintWriter writer = _writer;
341: _writer = null;
342:
343: if (writer != null) {
344: try {
345: writer.close();
346: } catch (Throwable e) {
347: log.log(Level.FINER, e.toString(), e);
348: }
349: }
350:
351: ServletOutputStream os = _os;
352: _os = null;
353:
354: if (os != null) {
355: try {
356: os.close();
357: } catch (Throwable e) {
358: log.log(Level.FINER, e.toString(), e);
359: }
360: }
361:
362: _next = null;
363: }
364: }
|