001 /*
002 * Copyright 2004 The Apache Software Foundation
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package javax.servlet;
017
018 import java.io.OutputStream;
019 import java.io.IOException;
020 import java.io.CharConversionException;
021 import java.text.MessageFormat;
022 import java.util.ResourceBundle;
023
024 /**
025 * Provides an output stream for sending binary data to the
026 * client. A <code>ServletOutputStream</code> object is normally retrieved
027 * via the {@link ServletResponse#getOutputStream} method.
028 *
029 * <p>This is an abstract class that the servlet container implements.
030 * Subclasses of this class
031 * must implement the <code>java.io.OutputStream.write(int)</code>
032 * method.
033 *
034 *
035 * @author Various
036 * @version $Version$
037 *
038 * @see ServletResponse
039 *
040 */
041
042 public abstract class ServletOutputStream extends OutputStream {
043
044 private static final String LSTRING_FILE = "javax.servlet.LocalStrings";
045 private static ResourceBundle lStrings = ResourceBundle
046 .getBundle(LSTRING_FILE);
047
048 /**
049 *
050 * Does nothing, because this is an abstract class.
051 *
052 */
053
054 protected ServletOutputStream() {
055 }
056
057 /**
058 * Writes a <code>String</code> to the client,
059 * without a carriage return-line feed (CRLF)
060 * character at the end.
061 *
062 *
063 * @param s the <code>String</code> to send to the client
064 *
065 * @exception IOException if an input or output exception occurred
066 *
067 */
068
069 public void print(String s) throws IOException {
070 if (s == null)
071 s = "null";
072 int len = s.length();
073 for (int i = 0; i < len; i++) {
074 char c = s.charAt(i);
075
076 //
077 // XXX NOTE: This is clearly incorrect for many strings,
078 // but is the only consistent approach within the current
079 // servlet framework. It must suffice until servlet output
080 // streams properly encode their output.
081 //
082 if ((c & 0xff00) != 0) { // high order byte must be zero
083 String errMsg = lStrings.getString("err.not_iso8859_1");
084 Object[] errArgs = new Object[1];
085 errArgs[0] = new Character(c);
086 errMsg = MessageFormat.format(errMsg, errArgs);
087 throw new CharConversionException(errMsg);
088 }
089 write(c);
090 }
091 }
092
093 /**
094 * Writes a <code>boolean</code> value to the client,
095 * with no carriage return-line feed (CRLF)
096 * character at the end.
097 *
098 * @param b the <code>boolean</code> value
099 * to send to the client
100 *
101 * @exception IOException if an input or output exception occurred
102 *
103 */
104
105 public void print(boolean b) throws IOException {
106 String msg;
107 if (b) {
108 msg = lStrings.getString("value.true");
109 } else {
110 msg = lStrings.getString("value.false");
111 }
112 print(msg);
113 }
114
115 /**
116 * Writes a character to the client,
117 * with no carriage return-line feed (CRLF)
118 * at the end.
119 *
120 * @param c the character to send to the client
121 *
122 * @exception IOException if an input or output exception occurred
123 *
124 */
125
126 public void print(char c) throws IOException {
127 print(String.valueOf(c));
128 }
129
130 /**
131 *
132 * Writes an int to the client,
133 * with no carriage return-line feed (CRLF)
134 * at the end.
135 *
136 * @param i the int to send to the client
137 *
138 * @exception IOException if an input or output exception occurred
139 *
140 */
141
142 public void print(int i) throws IOException {
143 print(String.valueOf(i));
144 }
145
146 /**
147 *
148 * Writes a <code>long</code> value to the client,
149 * with no carriage return-line feed (CRLF) at the end.
150 *
151 * @param l the <code>long</code> value
152 * to send to the client
153 *
154 * @exception IOException if an input or output exception
155 * occurred
156 *
157 */
158
159 public void print(long l) throws IOException {
160 print(String.valueOf(l));
161 }
162
163 /**
164 *
165 * Writes a <code>float</code> value to the client,
166 * with no carriage return-line feed (CRLF) at the end.
167 *
168 * @param f the <code>float</code> value
169 * to send to the client
170 *
171 * @exception IOException if an input or output exception occurred
172 *
173 *
174 */
175
176 public void print(float f) throws IOException {
177 print(String.valueOf(f));
178 }
179
180 /**
181 *
182 * Writes a <code>double</code> value to the client,
183 * with no carriage return-line feed (CRLF) at the end.
184 *
185 * @param d the <code>double</code> value
186 * to send to the client
187 *
188 * @exception IOException if an input or output exception occurred
189 *
190 */
191
192 public void print(double d) throws IOException {
193 print(String.valueOf(d));
194 }
195
196 /**
197 * Writes a carriage return-line feed (CRLF)
198 * to the client.
199 *
200 *
201 *
202 * @exception IOException if an input or output exception occurred
203 *
204 */
205
206 public void println() throws IOException {
207 print("\r\n");
208 }
209
210 /**
211 * Writes a <code>String</code> to the client,
212 * followed by a carriage return-line feed (CRLF).
213 *
214 *
215 * @param s the <code>String</code> to write to the client
216 *
217 * @exception IOException if an input or output exception occurred
218 *
219 */
220
221 public void println(String s) throws IOException {
222 print(s);
223 println();
224 }
225
226 /**
227 *
228 * Writes a <code>boolean</code> value to the client,
229 * followed by a
230 * carriage return-line feed (CRLF).
231 *
232 *
233 * @param b the <code>boolean</code> value
234 * to write to the client
235 *
236 * @exception IOException if an input or output exception occurred
237 *
238 */
239
240 public void println(boolean b) throws IOException {
241 print(b);
242 println();
243 }
244
245 /**
246 *
247 * Writes a character to the client, followed by a carriage
248 * return-line feed (CRLF).
249 *
250 * @param c the character to write to the client
251 *
252 * @exception IOException if an input or output exception occurred
253 *
254 */
255
256 public void println(char c) throws IOException {
257 print(c);
258 println();
259 }
260
261 /**
262 *
263 * Writes an int to the client, followed by a
264 * carriage return-line feed (CRLF) character.
265 *
266 *
267 * @param i the int to write to the client
268 *
269 * @exception IOException if an input or output exception occurred
270 *
271 */
272
273 public void println(int i) throws IOException {
274 print(i);
275 println();
276 }
277
278 /**
279 *
280 * Writes a <code>long</code> value to the client, followed by a
281 * carriage return-line feed (CRLF).
282 *
283 *
284 * @param l the <code>long</code> value to write to the client
285 *
286 * @exception IOException if an input or output exception occurred
287 *
288 */
289
290 public void println(long l) throws IOException {
291 print(l);
292 println();
293 }
294
295 /**
296 *
297 * Writes a <code>float</code> value to the client,
298 * followed by a carriage return-line feed (CRLF).
299 *
300 * @param f the <code>float</code> value
301 * to write to the client
302 *
303 *
304 * @exception IOException if an input or output exception
305 * occurred
306 *
307 */
308
309 public void println(float f) throws IOException {
310 print(f);
311 println();
312 }
313
314 /**
315 *
316 * Writes a <code>double</code> value to the client,
317 * followed by a carriage return-line feed (CRLF).
318 *
319 *
320 * @param d the <code>double</code> value
321 * to write to the client
322 *
323 * @exception IOException if an input or output exception occurred
324 *
325 */
326
327 public void println(double d) throws IOException {
328 print(d);
329 println();
330 }
331 }
|