001 /** 002 * 003 * Copyright 2003-2004 The Apache Software Foundation 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018 // 019 // This source code implements specifications defined by the Java 020 // Community Process. In order to remain compliant with the specification 021 // DO NOT add / change / or delete method signatures! 022 // 023 024 package javax.servlet; 025 026 import java.io.CharConversionException; 027 import java.io.IOException; 028 import java.io.OutputStream; 029 import java.text.MessageFormat; 030 import java.util.ResourceBundle; 031 032 /** 033 * Provides an output stream for sending binary data to the 034 * client. A <code>ServletOutputStream</code> object is normally retrieved 035 * via the {@link ServletResponse#getOutputStream} method. 036 * 037 * <p>This is an abstract class that the servlet container implements. 038 * Subclasses of this class 039 * must implement the <code>java.io.OutputStream.write(int)</code> 040 * method. 041 * 042 * @see ServletResponse 043 * 044 * @version $Rev: 46019 $ $Date: 2004-09-14 02:56:06 -0700 (Tue, 14 Sep 2004) $ 045 */ 046 public abstract class ServletOutputStream extends OutputStream { 047 private static final String LSTRING_FILE = "javax.servlet.LocalStrings"; 048 private static ResourceBundle lStrings = ResourceBundle.getBundle(LSTRING_FILE); 049 050 /** 051 * Does nothing, because this is an abstract class. 052 */ 053 protected ServletOutputStream() { 054 } 055 056 /** 057 * Writes a <code>String</code> to the client, 058 * without a carriage return-line feed (CRLF) 059 * character at the end. 060 * 061 * @param s the <code>String</code> to send to the client 062 * 063 * @exception IOException if an input or output exception occurred 064 */ 065 public void print(String s) throws IOException { 066 if (s == null) s = "null"; 067 int len = s.length(); 068 for (int i = 0; i < len; i++) { 069 char c = s.charAt(i); 070 071 // 072 // XXX NOTE: This is clearly incorrect for many strings, 073 // but is the only consistent approach within the current 074 // servlet framework. It must suffice until servlet output 075 // streams properly encode their output. 076 // 077 if ((c & 0xff00) != 0) { // high order byte must be zero 078 String errMsg = lStrings.getString("err.not_iso8859_1"); 079 Object[] errArgs = new Object[1]; 080 errArgs[0] = new Character(c); 081 errMsg = MessageFormat.format(errMsg, errArgs); 082 throw new CharConversionException(errMsg); 083 } 084 write(c); 085 } 086 } 087 088 089 /** 090 * Writes a <code>boolean</code> value to the client, 091 * with no carriage return-line feed (CRLF) 092 * character at the end. 093 * 094 * @param b the <code>boolean</code> value 095 * to send to the client 096 * 097 * @exception IOException if an input or output exception occurred 098 */ 099 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 }