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