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.AbstractPrintWriter;
035:
036: import java.io.IOException;
037: import java.util.logging.Level;
038: import java.util.logging.Logger;
039:
040: public class ResponseWriter extends AbstractPrintWriter {
041: static final Logger log = Log.open(ResponseWriter.class);
042: static final L10N L = new L10N(ResponseWriter.class);
043:
044: private AbstractResponseStream _out;
045: private boolean _hasError;
046:
047: public ResponseWriter() {
048: }
049:
050: ResponseWriter(AbstractResponseStream out) {
051: _out = out;
052: }
053:
054: public void init(AbstractResponseStream out) {
055: _out = out;
056: _hasError = false;
057: }
058:
059: public int getBufferSize() {
060: return _out.getBufferSize();
061: }
062:
063: /**
064: * Sets the buffer size.
065: */
066: public void setBufferSize(int size) {
067: _out.setBufferSize(size);
068: }
069:
070: public int getRemaining() {
071: return _out.getRemaining();
072: }
073:
074: /**
075: * Checks for an error.
076: */
077: public boolean checkError() {
078: return _hasError;
079: }
080:
081: /**
082: * Clears the response buffer.
083: */
084: public void clearBuffer() {
085: _out.clearBuffer();
086: }
087:
088: /**
089: * Writes a character to the output.
090: *
091: * @param buf the buffer to write.
092: */
093: final public void write(int ch) {
094: try {
095: _out.print(ch);
096: } catch (IOException e) {
097: _hasError = true;
098: log.log(Level.FINE, e.toString(), e);
099: }
100: }
101:
102: /**
103: * Writes a character array to the writer.
104: *
105: * @param buf the buffer to write.
106: * @param off the offset into the buffer
107: * @param len the number of characters to write
108: */
109: final public void write(char[] buf, int offset, int length) {
110: try {
111: _out.print(buf, offset, length);
112: } catch (IOException e) {
113: _hasError = true;
114: log.log(Level.FINE, e.toString(), e);
115: }
116: }
117:
118: /**
119: * Writes a subsection of a string to the output.
120: */
121: final public void write(String s, int off, int len) {
122: try {
123: char[] writeBuffer = _out.getCharBuffer();
124: int size = writeBuffer.length;
125: int writeLength = _out.getCharOffset();
126: int end = off + len;
127:
128: while (off < end) {
129: int sublen = end - off;
130:
131: if (size - writeLength < sublen) {
132: if (size == writeLength) {
133: writeBuffer = _out.nextCharBuffer(writeLength);
134: writeLength = 0;
135:
136: if (size < sublen)
137: sublen = size;
138: } else
139: sublen = size - writeLength;
140: }
141:
142: int tail = off + sublen;
143: s.getChars(off, tail, writeBuffer, writeLength);
144:
145: off = tail;
146: writeLength += sublen;
147: }
148:
149: _out.setCharOffset(writeLength);
150: } catch (IOException e) {
151: _hasError = true;
152: log.log(Level.FINE, e.toString(), e);
153: }
154: }
155:
156: /**
157: * Flushes the buffered response to the output stream.
158: */
159: public void flush() {
160: try {
161: _out.flushChar();
162: } catch (IOException e) {
163: _hasError = true;
164: log.log(Level.FINE, e.toString(), e);
165: }
166: }
167:
168: /**
169: * Flush the contents of the buffer to the underlying stream.
170: *
171: * @param isEnd true if the request is done.
172: */
173: public void flushBuffer() {
174: try {
175: _out.flushBuffer();
176: } catch (IOException e) {
177: _hasError = true;
178: log.log(Level.FINE, e.toString(), e);
179: }
180: }
181:
182: public void close() {
183: _hasError = false;
184:
185: // server/0200
186: // server/1720
187: /*
188: try {
189: _out.close();
190: } catch (IOException e) {
191: log.log(Level.FINE, e.toString(), e);
192: }
193: */
194:
195: /*
196: try {
197: _out.flushBuffer();
198: } catch (IOException e) {
199: log.log(Level.FINE, e.toString(), e);
200: }
201: */
202: }
203: }
|