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 =
046            ResourceBundle.getBundle(LSTRING_FILE);
047    
048    
049        
050        /**
051         *
052         * Does nothing, because this is an abstract class.
053         *
054         */
055    
056        protected ServletOutputStream() { }
057    
058    
059        /**
060         * Writes a <code>String</code> to the client, 
061         * without a carriage return-line feed (CRLF) 
062         * character at the end.
063         *
064         *
065         * @param s                 the <code>String</code> to send to the client
066         *
067         * @exception IOException   if an input or output exception occurred
068         *
069         */
070    
071        public void print(String s) throws IOException {
072            if (s==null) s="null";
073            int len = s.length();
074            for (int i = 0; i < len; i++) {
075                char c = s.charAt (i);
076    
077                //
078                // XXX NOTE:  This is clearly incorrect for many strings,
079                // but is the only consistent approach within the current
080                // servlet framework.  It must suffice until servlet output
081                // streams properly encode their output.
082                //
083                if ((c & 0xff00) != 0) {        // high order byte must be zero
084                    String errMsg = lStrings.getString("err.not_iso8859_1");
085                    Object[] errArgs = new Object[1];
086                    errArgs[0] = new Character(c);
087                    errMsg = MessageFormat.format(errMsg, errArgs);
088                    throw new CharConversionException(errMsg);
089                }
090                write (c);
091            }
092        }
093    
094    
095    
096        /**
097         * Writes a <code>boolean</code> value to the client,
098         * with no carriage return-line feed (CRLF) 
099         * 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    }