1
2
3 /*
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
5 *
6 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
7 *
8 * Portions Copyright Apache Software Foundation.
9 *
10 * The contents of this file are subject to the terms of either the GNU
11 * General Public License Version 2 only ("GPL") or the Common Development
12 * and Distribution License("CDDL") (collectively, the "License"). You
13 * may not use this file except in compliance with the License. You can obtain
14 * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
15 * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
16 * language governing permissions and limitations under the License.
17 *
18 * When distributing the software, include this License Header Notice in each
19 * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
20 * Sun designates this particular file as subject to the "Classpath" exception
21 * as provided by Sun in the GPL Version 2 section of the License file that
22 * accompanied this code. If applicable, add the following below the License
23 * Header, with the fields enclosed by brackets [] replaced by your own
24 * identifying information: "Portions Copyrighted [year]
25 * [name of copyright owner]"
26 *
27 * Contributor(s):
28 *
29 * If you wish your version of this file to be governed by only the CDDL or
30 * only the GPL Version 2, indicate your decision by adding "[Contributor]
31 * elects to include this software in this distribution under the [CDDL or GPL
32 * Version 2] license." If you don't indicate a single choice of license, a
33 * recipient has the option to distribute your version of this file under
34 * either the CDDL, the GPL Version 2 or to extend the choice of license to
35 * its licensees as provided above. However, if you add GPL Version 2 code
36 * and therefore, elected the GPL Version 2 license, then the option applies
37 * only if the new code is made subject to such option by the copyright
38 * holder.
39 */
40
41 package javax.servlet;
42
43 import java.io.OutputStream;
44 import java.io.IOException;
45 import java.io.CharConversionException;
46 import java.text.MessageFormat;
47 import java.util.ResourceBundle;
48
49 /**
50 * Provides an output stream for sending binary data to the
51 * client. A <code>ServletOutputStream</code> object is normally retrieved
52 * via the {@link ServletResponse#getOutputStream} method.
53 *
54 * <p>This is an abstract class that the servlet container implements.
55 * Subclasses of this class
56 * must implement the <code>java.io.OutputStream.write(int)</code>
57 * method.
58 *
59 *
60 * @author Various
61 *
62 * @see ServletResponse
63 *
64 */
65
66 public abstract class ServletOutputStream extends OutputStream {
67
68 private static final String LSTRING_FILE = "javax.servlet.LocalStrings";
69 private static ResourceBundle lStrings =
70 ResourceBundle.getBundle(LSTRING_FILE);
71
72
73
74 /**
75 *
76 * Does nothing, because this is an abstract class.
77 *
78 */
79
80 protected ServletOutputStream() { }
81
82
83 /**
84 * Writes a <code>String</code> to the client,
85 * without a carriage return-line feed (CRLF)
86 * character at the end.
87 *
88 *
89 * @param s the <code>String</code> to send to the client
90 *
91 * @exception IOException if an input or output exception occurred
92 *
93 */
94
95 public void print(String s) throws IOException {
96 if (s==null) s="null";
97 int len = s.length();
98 for (int i = 0; i < len; i++) {
99 char c = s.charAt (i);
100
101 //
102 // XXX NOTE: This is clearly incorrect for many strings,
103 // but is the only consistent approach within the current
104 // servlet framework. It must suffice until servlet output
105 // streams properly encode their output.
106 //
107 if ((c & 0xff00) != 0) { // high order byte must be zero
108 String errMsg = lStrings.getString("err.not_iso8859_1");
109 Object[] errArgs = new Object[1];
110 errArgs[0] = new Character(c);
111 errMsg = MessageFormat.format(errMsg, errArgs);
112 throw new CharConversionException(errMsg);
113 }
114 write (c);
115 }
116 }
117
118
119
120 /**
121 * Writes a <code>boolean</code> value to the client,
122 * with no carriage return-line feed (CRLF)
123 * character at the end.
124 *
125 * @param b the <code>boolean</code> value
126 * to send to the client
127 *
128 * @exception IOException if an input or output exception occurred
129 *
130 */
131
132 public void print(boolean b) throws IOException {
133 String msg;
134 if (b) {
135 msg = lStrings.getString("value.true");
136 } else {
137 msg = lStrings.getString("value.false");
138 }
139 print(msg);
140 }
141
142
143
144 /**
145 * Writes a character to the client,
146 * with no carriage return-line feed (CRLF)
147 * at the end.
148 *
149 * @param c the character to send to the client
150 *
151 * @exception IOException if an input or output exception occurred
152 *
153 */
154
155 public void print(char c) throws IOException {
156 print(String.valueOf(c));
157 }
158
159
160
161
162 /**
163 *
164 * Writes an int to the client,
165 * with no carriage return-line feed (CRLF)
166 * at the end.
167 *
168 * @param i the int to send to the client
169 *
170 * @exception IOException if an input or output exception occurred
171 *
172 */
173
174 public void print(int i) throws IOException {
175 print(String.valueOf(i));
176 }
177
178
179
180
181 /**
182 *
183 * Writes a <code>long</code> value to the client,
184 * with no carriage return-line feed (CRLF) at the end.
185 *
186 * @param l the <code>long</code> value
187 * to send to the client
188 *
189 * @exception IOException if an input or output exception
190 * occurred
191 *
192 */
193
194 public void print(long l) throws IOException {
195 print(String.valueOf(l));
196 }
197
198
199
200 /**
201 *
202 * Writes a <code>float</code> value to the client,
203 * with no carriage return-line feed (CRLF) at the end.
204 *
205 * @param f the <code>float</code> value
206 * to send to the client
207 *
208 * @exception IOException if an input or output exception occurred
209 *
210 *
211 */
212
213 public void print(float f) throws IOException {
214 print(String.valueOf(f));
215 }
216
217
218
219 /**
220 *
221 * Writes a <code>double</code> value to the client,
222 * with no carriage return-line feed (CRLF) at the end.
223 *
224 * @param d the <code>double</code> value
225 * to send to the client
226 *
227 * @exception IOException if an input or output exception occurred
228 *
229 */
230
231 public void print(double d) throws IOException {
232 print(String.valueOf(d));
233 }
234
235
236
237 /**
238 * Writes a carriage return-line feed (CRLF)
239 * to the client.
240 *
241 *
242 *
243 * @exception IOException if an input or output exception occurred
244 *
245 */
246
247 public void println() throws IOException {
248 print("\r\n");
249 }
250
251
252
253 /**
254 * Writes a <code>String</code> to the client,
255 * followed by a carriage return-line feed (CRLF).
256 *
257 *
258 * @param s the <code>String</code> to write to the client
259 *
260 * @exception IOException if an input or output exception occurred
261 *
262 */
263
264 public void println(String s) throws IOException {
265 print(s);
266 println();
267 }
268
269
270
271
272 /**
273 *
274 * Writes a <code>boolean</code> value to the client,
275 * followed by a
276 * carriage return-line feed (CRLF).
277 *
278 *
279 * @param b the <code>boolean</code> value
280 * to write to the client
281 *
282 * @exception IOException if an input or output exception occurred
283 *
284 */
285
286 public void println(boolean b) throws IOException {
287 print(b);
288 println();
289 }
290
291
292
293 /**
294 *
295 * Writes a character to the client, followed by a carriage
296 * return-line feed (CRLF).
297 *
298 * @param c the character to write to the client
299 *
300 * @exception IOException if an input or output exception occurred
301 *
302 */
303
304 public void println(char c) throws IOException {
305 print(c);
306 println();
307 }
308
309
310
311 /**
312 *
313 * Writes an int to the client, followed by a
314 * carriage return-line feed (CRLF) character.
315 *
316 *
317 * @param i the int to write to the client
318 *
319 * @exception IOException if an input or output exception occurred
320 *
321 */
322
323 public void println(int i) throws IOException {
324 print(i);
325 println();
326 }
327
328
329
330 /**
331 *
332 * Writes a <code>long</code> value to the client, followed by a
333 * carriage return-line feed (CRLF).
334 *
335 *
336 * @param l the <code>long</code> value to write to the client
337 *
338 * @exception IOException if an input or output exception occurred
339 *
340 */
341
342 public void println(long l) throws IOException {
343 print(l);
344 println();
345 }
346
347
348
349 /**
350 *
351 * Writes a <code>float</code> value to the client,
352 * followed by a carriage return-line feed (CRLF).
353 *
354 * @param f the <code>float</code> value
355 * to write to the client
356 *
357 *
358 * @exception IOException if an input or output exception
359 * occurred
360 *
361 */
362
363 public void println(float f) throws IOException {
364 print(f);
365 println();
366 }
367
368
369
370 /**
371 *
372 * Writes a <code>double</code> value to the client,
373 * followed by a carriage return-line feed (CRLF).
374 *
375 *
376 * @param d the <code>double</code> value
377 * to write to the client
378 *
379 * @exception IOException if an input or output exception occurred
380 *
381 */
382
383 public void println(double d) throws IOException {
384 print(d);
385 println();
386 }
387 }