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: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.filters;
030:
031: import com.caucho.log.Log;
032: import com.caucho.server.connection.ToByteResponseStream;
033: import com.caucho.util.L10N;
034:
035: import java.io.IOException;
036: import java.io.OutputStream;
037: import java.util.logging.Logger;
038:
039: public class FilterWrapperResponseStream extends ToByteResponseStream {
040: static final Logger log = Log
041: .open(FilterWrapperResponseStream.class);
042:
043: static final L10N L = new L10N(FilterWrapperResponseStream.class);
044:
045: private CauchoResponseWrapper _response;
046:
047: private OutputStream _os;
048:
049: public FilterWrapperResponseStream() {
050: }
051:
052: public void init(CauchoResponseWrapper response) {
053: _response = response;
054: _os = null;
055: }
056:
057: /**
058: * Writes the next chunk of data to the response stream.
059: *
060: * @param buf the buffer containing the data
061: * @param offset start offset into the buffer
062: * @param length length of the data in the buffer
063: */
064: protected void writeNext(byte[] buf, int offset, int length,
065: boolean isEnd) throws IOException {
066: OutputStream os = getStream();
067:
068: if (os != null)
069: os.write(buf, offset, length);
070: }
071:
072: /**
073: * flushing
074: */
075: public void flush() throws IOException {
076: flushBuffer();
077:
078: OutputStream os = getStream();
079: if (os != null)
080: os.flush();
081: }
082:
083: /**
084: * Gets the stream.
085: */
086: private OutputStream getStream() throws IOException {
087: if (_os != null)
088: return _os;
089: else if (_response != null)
090: _os = _response.getStream();
091:
092: return _os;
093: }
094:
095: /**
096: * Finish.
097: */
098: public void finish() throws IOException {
099: flushBuffer();
100:
101: /*
102: if (_os != null)
103: _os.flush();
104: */
105:
106: _response = null;
107: _os = null;
108: }
109:
110: /**
111: * Close.
112: */
113: public void close() throws IOException {
114: super .close();
115:
116: _response = null;
117:
118: OutputStream os = _os;
119: _os = null;
120: // server/1839
121:
122: if (os != null)
123: os.close();
124: }
125: }
|