View Javadoc

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