View Javadoc

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