KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > connection > ResponseWriter


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.server.connection;
31
32 import com.caucho.log.Log;
33 import com.caucho.util.L10N;
34 import com.caucho.vfs.AbstractPrintWriter;
35
36 import java.io.IOException JavaDoc;
37 import java.util.logging.Level JavaDoc;
38 import java.util.logging.Logger JavaDoc;
39
40 public class ResponseWriter extends AbstractPrintWriter {
41   static final Logger JavaDoc log = Log.open(ResponseWriter.class);
42   static final L10N L = new L10N(ResponseWriter.class);
43   
44   private AbstractResponseStream _out;
45   private boolean _hasError;
46
47   public ResponseWriter()
48   {
49   }
50
51   ResponseWriter(AbstractResponseStream out)
52   {
53     _out = out;
54   }
55
56   public void init(AbstractResponseStream out)
57   {
58     _out = out;
59     _hasError = false;
60   }
61
62   public int getBufferSize()
63   {
64     return _out.getBufferSize();
65   }
66
67   /**
68    * Sets the buffer size.
69    */

70   public void setBufferSize(int size)
71   {
72     _out.setBufferSize(size);
73   }
74
75   public int getRemaining()
76   {
77     return _out.getRemaining();
78   }
79
80   /**
81    * Checks for an error.
82    */

83   public boolean checkError()
84   {
85     return _hasError;
86   }
87
88   /**
89    * Clears the response buffer.
90    */

91   public void clearBuffer()
92   {
93     _out.clearBuffer();
94   }
95   
96   /**
97    * Writes a character to the output.
98    *
99    * @param buf the buffer to write.
100    */

101   final public void write(int ch)
102   {
103     try {
104       _out.print(ch);
105     } catch (IOException JavaDoc e) {
106       _hasError = true;
107       log.log(Level.FINE, e.toString(), e);
108     }
109   }
110
111   /**
112    * Writes a character array to the writer.
113    *
114    * @param buf the buffer to write.
115    * @param off the offset into the buffer
116    * @param len the number of characters to write
117    */

118   final public void write(char []buf, int offset, int length)
119   {
120     try {
121       _out.print(buf, offset, length);
122     } catch (IOException JavaDoc e) {
123       _hasError = true;
124       log.log(Level.FINE, e.toString(), e);
125     }
126   }
127
128   /**
129    * Writes a subsection of a string to the output.
130    */

131   final public void write(String JavaDoc s, int off, int len)
132   {
133     try {
134       char []writeBuffer = _out.getCharBuffer();
135       int size = writeBuffer.length;
136       int writeLength = _out.getCharOffset();
137       int end = off + len;
138
139       while (off < end) {
140     int sublen = end - off;
141
142     if (size - writeLength < sublen) {
143       if (size == writeLength) {
144         writeBuffer = _out.nextCharBuffer(writeLength);
145         writeLength = 0;
146       
147         if (size < sublen)
148           sublen = size;
149       }
150       else
151         sublen = size - writeLength;
152     }
153
154     int tail = off + sublen;
155     s.getChars(off, tail, writeBuffer, writeLength);
156
157     off = tail;
158     writeLength += sublen;
159       }
160
161       _out.setCharOffset(writeLength);
162     } catch (IOException JavaDoc e) {
163       _hasError = true;
164       log.log(Level.FINE, e.toString(), e);
165     }
166   }
167
168   /**
169    * Flushes the buffered response to the output stream.
170    */

171   public void flush()
172   {
173     try {
174       _out.flushChar();
175     } catch (IOException JavaDoc e) {
176       _hasError = true;
177       log.log(Level.FINE, e.toString(), e);
178     }
179   }
180
181   /**
182    * Flush the contents of the buffer to the underlying stream.
183    *
184    * @param isEnd true if the request is done.
185    */

186   public void flushBuffer()
187   {
188     try {
189       _out.flushBuffer();
190     } catch (IOException JavaDoc e) {
191       _hasError = true;
192       log.log(Level.FINE, e.toString(), e);
193     }
194   }
195
196   public void close()
197   {
198     _hasError = false;
199
200     // server/0200
201
// server/1720
202
/*
203     try {
204       _out.close();
205     } catch (IOException e) {
206       log.log(Level.FINE, e.toString(), e);
207     }
208     */

209
210     /*
211     try {
212       _out.flushBuffer();
213     } catch (IOException e) {
214       log.log(Level.FINE, e.toString(), e);
215     }
216     */

217   }
218 }
219
Popular Tags