View Javadoc

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