1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 package javax.servlet;
21
22 import java.io.OutputStream;
23 import java.io.IOException;
24 import java.io.CharConversionException;
25 import java.text.MessageFormat;
26 import java.util.ResourceBundle;
27
28 /**
29 * Provides an output stream for sending binary data to the
30 * client. A <code>ServletOutputStream</code> object is normally retrieved
31 * via the {@link ServletResponse#getOutputStream} method.
32 *
33 * <p>This is an abstract class that the servlet container implements.
34 * Subclasses of this class
35 * must implement the <code>java.io.OutputStream.write(int)</code>
36 * method.
37 *
38 *
39 * @author Various
40 * @version $Version$
41 *
42 * @see ServletResponse
43 *
44 */
45
46 public abstract class ServletOutputStream extends OutputStream {
47
48 private static final String LSTRING_FILE = "javax.servlet.LocalStrings";
49 private static ResourceBundle lStrings =
50 ResourceBundle.getBundle(LSTRING_FILE);
51
52
53
54 /**
55 *
56 * Does nothing, because this is an abstract class.
57 *
58 */
59
60 protected ServletOutputStream() { }
61
62
63 /**
64 * Writes a <code>String</code> to the client,
65 * without a carriage return-line feed (CRLF)
66 * character at the end.
67 *
68 *
69 * @param s the <code>String</code> to send to the client
70 *
71 * @exception IOException if an input or output exception occurred
72 *
73 */
74
75 public void print(String s) throws IOException {
76 if (s==null) s="null";
77 int len = s.length();
78 for (int i = 0; i < len; i++) {
79 char c = s.charAt (i);
80
81 //
82 // XXX NOTE: This is clearly incorrect for many strings,
83 // but is the only consistent approach within the current
84 // servlet framework. It must suffice until servlet output
85 // streams properly encode their output.
86 //
87 if ((c & 0xff00) != 0) { // high order byte must be zero
88 String errMsg = lStrings.getString("err.not_iso8859_1");
89 Object[] errArgs = new Object[1];
90 errArgs[0] = new Character(c);
91 errMsg = MessageFormat.format(errMsg, errArgs);
92 throw new CharConversionException(errMsg);
93 }
94 write (c);
95 }
96 }
97
98
99
100 /**
101 * Writes a <code>boolean</code> value to the client,
102 * with no carriage return-line feed (CRLF)
103 * character at the end.
104 *
105 * @param b the <code>boolean</code> value
106 * to send to the client
107 *
108 * @exception IOException if an input or output exception occurred
109 *
110 */
111
112 public void print(boolean b) throws IOException {
113 String msg;
114 if (b) {
115 msg = lStrings.getString("value.true");
116 } else {
117 msg = lStrings.getString("value.false");
118 }
119 print(msg);
120 }
121
122
123
124 /**
125 * Writes a character to the client,
126 * with no carriage return-line feed (CRLF)
127 * at the end.
128 *
129 * @param c the character to send to the client
130 *
131 * @exception IOException if an input or output exception occurred
132 *
133 */
134
135 public void print(char c) throws IOException {
136 print(String.valueOf(c));
137 }
138
139
140
141
142 /**
143 *
144 * Writes an int to the client,
145 * with no carriage return-line feed (CRLF)
146 * at the end.
147 *
148 * @param i the int to send to the client
149 *
150 * @exception IOException if an input or output exception occurred
151 *
152 */
153
154 public void print(int i) throws IOException {
155 print(String.valueOf(i));
156 }
157
158
159
160
161 /**
162 *
163 * Writes a <code>long</code> value to the client,
164 * with no carriage return-line feed (CRLF) at the end.
165 *
166 * @param l the <code>long</code> value
167 * to send to the client
168 *
169 * @exception IOException if an input or output exception
170 * occurred
171 *
172 */
173
174 public void print(long l) throws IOException {
175 print(String.valueOf(l));
176 }
177
178
179
180 /**
181 *
182 * Writes a <code>float</code> value to the client,
183 * with no carriage return-line feed (CRLF) at the end.
184 *
185 * @param f the <code>float</code> value
186 * to send to the client
187 *
188 * @exception IOException if an input or output exception occurred
189 *
190 *
191 */
192
193 public void print(float f) throws IOException {
194 print(String.valueOf(f));
195 }
196
197
198
199 /**
200 *
201 * Writes a <code>double</code> value to the client,
202 * with no carriage return-line feed (CRLF) at the end.
203 *
204 * @param d the <code>double</code> value
205 * to send to the client
206 *
207 * @exception IOException if an input or output exception occurred
208 *
209 */
210
211 public void print(double d) throws IOException {
212 print(String.valueOf(d));
213 }
214
215
216
217 /**
218 * Writes a carriage return-line feed (CRLF)
219 * to the client.
220 *
221 *
222 *
223 * @exception IOException if an input or output exception occurred
224 *
225 */
226
227 public void println() throws IOException {
228 print("\r\n");
229 }
230
231
232
233 /**
234 * Writes a <code>String</code> to the client,
235 * followed by a carriage return-line feed (CRLF).
236 *
237 *
238 * @param s the <code>String</code> to write to the client
239 *
240 * @exception IOException if an input or output exception occurred
241 *
242 */
243
244 public void println(String s) throws IOException {
245 print(s);
246 println();
247 }
248
249
250
251
252 /**
253 *
254 * Writes a <code>boolean</code> value to the client,
255 * followed by a
256 * carriage return-line feed (CRLF).
257 *
258 *
259 * @param b the <code>boolean</code> value
260 * to write to the client
261 *
262 * @exception IOException if an input or output exception occurred
263 *
264 */
265
266 public void println(boolean b) throws IOException {
267 print(b);
268 println();
269 }
270
271
272
273 /**
274 *
275 * Writes a character to the client, followed by a carriage
276 * return-line feed (CRLF).
277 *
278 * @param c the character to write to the client
279 *
280 * @exception IOException if an input or output exception occurred
281 *
282 */
283
284 public void println(char c) throws IOException {
285 print(c);
286 println();
287 }
288
289
290
291 /**
292 *
293 * Writes an int to the client, followed by a
294 * carriage return-line feed (CRLF) character.
295 *
296 *
297 * @param i the int to write to the client
298 *
299 * @exception IOException if an input or output exception occurred
300 *
301 */
302
303 public void println(int i) throws IOException {
304 print(i);
305 println();
306 }
307
308
309
310 /**
311 *
312 * Writes a <code>long</code> value to the client, followed by a
313 * carriage return-line feed (CRLF).
314 *
315 *
316 * @param l the <code>long</code> value to write to the client
317 *
318 * @exception IOException if an input or output exception occurred
319 *
320 */
321
322 public void println(long l) throws IOException {
323 print(l);
324 println();
325 }
326
327
328
329 /**
330 *
331 * Writes a <code>float</code> value to the client,
332 * followed by a carriage return-line feed (CRLF).
333 *
334 * @param f the <code>float</code> value
335 * to write to the client
336 *
337 *
338 * @exception IOException if an input or output exception
339 * occurred
340 *
341 */
342
343 public void println(float f) throws IOException {
344 print(f);
345 println();
346 }
347
348
349
350 /**
351 *
352 * Writes a <code>double</code> value to the client,
353 * followed by a carriage return-line feed (CRLF).
354 *
355 *
356 * @param d the <code>double</code> value
357 * to write to the client
358 *
359 * @exception IOException if an input or output exception occurred
360 *
361 */
362
363 public void println(double d) throws IOException {
364 print(d);
365 println();
366 }
367 }