001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020 package javax.servlet;
021
022 import java.io.OutputStream;
023 import java.io.IOException;
024 import java.io.CharConversionException;
025 import java.text.MessageFormat;
026 import java.util.ResourceBundle;
027
028 /**
029 * Provides an output stream for sending binary data to the
030 * client. A <code>ServletOutputStream</code> object is normally retrieved
031 * via the {@link ServletResponse#getOutputStream} method.
032 *
033 * <p>This is an abstract class that the servlet container implements.
034 * Subclasses of this class
035 * must implement the <code>java.io.OutputStream.write(int)</code>
036 * method.
037 *
038 *
039 * @author Various
040 * @version $Version$
041 *
042 * @see ServletResponse
043 *
044 */
045
046 public abstract class ServletOutputStream extends OutputStream {
047
048 private static final String LSTRING_FILE = "javax.servlet.LocalStrings";
049 private static ResourceBundle lStrings =
050 ResourceBundle.getBundle(LSTRING_FILE);
051
052
053
054 /**
055 *
056 * Does nothing, because this is an abstract class.
057 *
058 */
059
060 protected ServletOutputStream() { }
061
062
063 /**
064 * Writes a <code>String</code> to the client,
065 * without a carriage return-line feed (CRLF)
066 * character at the end.
067 *
068 *
069 * @param s the <code>String</code> to send to the client
070 *
071 * @exception IOException if an input or output exception occurred
072 *
073 */
074
075 public void print(String s) throws IOException {
076 if (s==null) s="null";
077 int len = s.length();
078 for (int i = 0; i < len; i++) {
079 char c = s.charAt (i);
080
081 //
082 // XXX NOTE: This is clearly incorrect for many strings,
083 // but is the only consistent approach within the current
084 // servlet framework. It must suffice until servlet output
085 // streams properly encode their output.
086 //
087 if ((c & 0xff00) != 0) { // high order byte must be zero
088 String errMsg = lStrings.getString("err.not_iso8859_1");
089 Object[] errArgs = new Object[1];
090 errArgs[0] = new Character(c);
091 errMsg = MessageFormat.format(errMsg, errArgs);
092 throw new CharConversionException(errMsg);
093 }
094 write (c);
095 }
096 }
097
098
099
100 /**
101 * Writes a <code>boolean</code> value to the client,
102 * with no carriage return-line feed (CRLF)
103 * character at the end.
104 *
105 * @param b the <code>boolean</code> value
106 * to send to the client
107 *
108 * @exception IOException if an input or output exception occurred
109 *
110 */
111
112 public void print(boolean b) throws IOException {
113 String msg;
114 if (b) {
115 msg = lStrings.getString("value.true");
116 } else {
117 msg = lStrings.getString("value.false");
118 }
119 print(msg);
120 }
121
122
123
124 /**
125 * Writes a character to the client,
126 * with no carriage return-line feed (CRLF)
127 * at the end.
128 *
129 * @param c the character to send to the client
130 *
131 * @exception IOException if an input or output exception occurred
132 *
133 */
134
135 public void print(char c) throws IOException {
136 print(String.valueOf(c));
137 }
138
139
140
141
142 /**
143 *
144 * Writes an int to the client,
145 * with no carriage return-line feed (CRLF)
146 * at the end.
147 *
148 * @param i the int to send to the client
149 *
150 * @exception IOException if an input or output exception occurred
151 *
152 */
153
154 public void print(int i) throws IOException {
155 print(String.valueOf(i));
156 }
157
158
159
160
161 /**
162 *
163 * Writes a <code>long</code> value to the client,
164 * with no carriage return-line feed (CRLF) at the end.
165 *
166 * @param l the <code>long</code> value
167 * to send to the client
168 *
169 * @exception IOException if an input or output exception
170 * occurred
171 *
172 */
173
174 public void print(long l) throws IOException {
175 print(String.valueOf(l));
176 }
177
178
179
180 /**
181 *
182 * Writes a <code>float</code> value to the client,
183 * with no carriage return-line feed (CRLF) at the end.
184 *
185 * @param f the <code>float</code> value
186 * to send to the client
187 *
188 * @exception IOException if an input or output exception occurred
189 *
190 *
191 */
192
193 public void print(float f) throws IOException {
194 print(String.valueOf(f));
195 }
196
197
198
199 /**
200 *
201 * Writes a <code>double</code> value to the client,
202 * with no carriage return-line feed (CRLF) at the end.
203 *
204 * @param d the <code>double</code> value
205 * to send to the client
206 *
207 * @exception IOException if an input or output exception occurred
208 *
209 */
210
211 public void print(double d) throws IOException {
212 print(String.valueOf(d));
213 }
214
215
216
217 /**
218 * Writes a carriage return-line feed (CRLF)
219 * to the client.
220 *
221 *
222 *
223 * @exception IOException if an input or output exception occurred
224 *
225 */
226
227 public void println() throws IOException {
228 print("\r\n");
229 }
230
231
232
233 /**
234 * Writes a <code>String</code> to the client,
235 * followed by a carriage return-line feed (CRLF).
236 *
237 *
238 * @param s the <code>String</code> to write to the client
239 *
240 * @exception IOException if an input or output exception occurred
241 *
242 */
243
244 public void println(String s) throws IOException {
245 print(s);
246 println();
247 }
248
249
250
251
252 /**
253 *
254 * Writes a <code>boolean</code> value to the client,
255 * followed by a
256 * carriage return-line feed (CRLF).
257 *
258 *
259 * @param b the <code>boolean</code> value
260 * to write to the client
261 *
262 * @exception IOException if an input or output exception occurred
263 *
264 */
265
266 public void println(boolean b) throws IOException {
267 print(b);
268 println();
269 }
270
271
272
273 /**
274 *
275 * Writes a character to the client, followed by a carriage
276 * return-line feed (CRLF).
277 *
278 * @param c the character to write to the client
279 *
280 * @exception IOException if an input or output exception occurred
281 *
282 */
283
284 public void println(char c) throws IOException {
285 print(c);
286 println();
287 }
288
289
290
291 /**
292 *
293 * Writes an int to the client, followed by a
294 * carriage return-line feed (CRLF) character.
295 *
296 *
297 * @param i the int to write to the client
298 *
299 * @exception IOException if an input or output exception occurred
300 *
301 */
302
303 public void println(int i) throws IOException {
304 print(i);
305 println();
306 }
307
308
309
310 /**
311 *
312 * Writes a <code>long</code> value to the client, followed by a
313 * carriage return-line feed (CRLF).
314 *
315 *
316 * @param l the <code>long</code> value to write to the client
317 *
318 * @exception IOException if an input or output exception occurred
319 *
320 */
321
322 public void println(long l) throws IOException {
323 print(l);
324 println();
325 }
326
327
328
329 /**
330 *
331 * Writes a <code>float</code> value to the client,
332 * followed by a carriage return-line feed (CRLF).
333 *
334 * @param f the <code>float</code> value
335 * to write to the client
336 *
337 *
338 * @exception IOException if an input or output exception
339 * occurred
340 *
341 */
342
343 public void println(float f) throws IOException {
344 print(f);
345 println();
346 }
347
348
349
350 /**
351 *
352 * Writes a <code>double</code> value to the client,
353 * followed by a carriage return-line feed (CRLF).
354 *
355 *
356 * @param d the <code>double</code> value
357 * to write to the client
358 *
359 * @exception IOException if an input or output exception occurred
360 *
361 */
362
363 public void println(double d) throws IOException {
364 print(d);
365 println();
366 }
367 }