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 //
021 // This source code implements specifications defined by the Java
022 // Community Process. In order to remain compliant with the specification
023 // DO NOT add / change / or delete method signatures!
024 //
025
026 package javax.servlet;
027
028 import java.io.CharConversionException;
029 import java.io.IOException;
030 import java.io.OutputStream;
031 import java.text.MessageFormat;
032 import java.util.ResourceBundle;
033
034 /**
035 * Provides an output stream for sending binary data to the
036 * client. A <code>ServletOutputStream</code> object is normally retrieved
037 * via the {@link ServletResponse#getOutputStream} method.
038 *
039 * <p>This is an abstract class that the servlet container implements.
040 * Subclasses of this class
041 * must implement the <code>java.io.OutputStream.write(int)</code>
042 * method.
043 *
044 * @see ServletResponse
045 *
046 * @version $Rev: 467553 $ $Date: 2006-10-24 21:01:51 -0700 (Tue, 24 Oct 2006) $
047 */
048 public abstract class ServletOutputStream extends OutputStream {
049 private static final String LSTRING_FILE = "javax.servlet.LocalStrings";
050 private static ResourceBundle lStrings = ResourceBundle.getBundle(LSTRING_FILE);
051
052 /**
053 * Does nothing, because this is an abstract class.
054 */
055 protected ServletOutputStream() {
056 }
057
058 /**
059 * Writes a <code>String</code> to the client,
060 * without a carriage return-line feed (CRLF)
061 * character at the end.
062 *
063 * @param s the <code>String</code> to send to the client
064 *
065 * @exception IOException if an input or output exception occurred
066 */
067 public void print(String s) throws IOException {
068 if (s == null) s = "null";
069 int len = s.length();
070 for (int i = 0; i < len; i++) {
071 char c = s.charAt(i);
072
073 //
074 // XXX NOTE: This is clearly incorrect for many strings,
075 // but is the only consistent approach within the current
076 // servlet framework. It must suffice until servlet output
077 // streams properly encode their output.
078 //
079 if ((c & 0xff00) != 0) { // high order byte must be zero
080 String errMsg = lStrings.getString("err.not_iso8859_1");
081 Object[] errArgs = new Object[1];
082 errArgs[0] = new Character(c);
083 errMsg = MessageFormat.format(errMsg, errArgs);
084 throw new CharConversionException(errMsg);
085 }
086 write(c);
087 }
088 }
089
090
091 /**
092 * Writes a <code>boolean</code> value to the client,
093 * with no carriage return-line feed (CRLF)
094 * character at the end.
095 *
096 * @param b the <code>boolean</code> value
097 * to send to the client
098 *
099 * @exception IOException if an input or output exception occurred
100 */
101 public void print(boolean b) throws IOException {
102 String msg;
103 if (b) {
104 msg = lStrings.getString("value.true");
105 } else {
106 msg = lStrings.getString("value.false");
107 }
108 print(msg);
109 }
110
111 /**
112 * Writes a character to the client,
113 * with no carriage return-line feed (CRLF)
114 * at the end.
115 *
116 * @param c the character to send to the client
117 *
118 * @exception IOException if an input or output exception occurred
119 */
120 public void print(char c) throws IOException {
121 print(String.valueOf(c));
122 }
123
124 /**
125 * Writes an int to the client,
126 * with no carriage return-line feed (CRLF)
127 * at the end.
128 *
129 * @param i the int to send to the client
130 *
131 * @exception IOException if an input or output exception occurred
132 */
133 public void print(int i) throws IOException {
134 print(String.valueOf(i));
135 }
136
137 /**
138 * Writes a <code>long</code> value to the client,
139 * with no carriage return-line feed (CRLF) at the end.
140 *
141 * @param l the <code>long</code> value
142 * to send to the client
143 *
144 * @exception IOException if an input or output exception
145 * occurred
146 */
147 public void print(long l) throws IOException {
148 print(String.valueOf(l));
149 }
150
151 /**
152 * Writes a <code>float</code> value to the client,
153 * with no carriage return-line feed (CRLF) at the end.
154 *
155 * @param f the <code>float</code> value
156 * to send to the client
157 *
158 * @exception IOException if an input or output exception occurred
159 */
160 public void print(float f) throws IOException {
161 print(String.valueOf(f));
162 }
163
164 /**
165 * Writes a <code>double</code> value to the client,
166 * with no carriage return-line feed (CRLF) at the end.
167 *
168 * @param d the <code>double</code> value
169 * to send to the client
170 *
171 * @exception IOException if an input or output exception occurred
172 */
173 public void print(double d) throws IOException {
174 print(String.valueOf(d));
175 }
176
177 /**
178 * Writes a carriage return-line feed (CRLF)
179 * to the client.
180 *
181 * @exception IOException if an input or output exception occurred
182 */
183 public void println() throws IOException {
184 print("\r\n");
185 }
186
187 /**
188 * Writes a <code>String</code> to the client,
189 * followed by a carriage return-line feed (CRLF).
190 *
191 * @param s the </code>String</code> to write to the client
192 *
193 * @exception IOException if an input or output exception occurred
194 */
195 public void println(String s) throws IOException {
196 print(s);
197 println();
198 }
199
200 /**
201 * Writes a <code>boolean</code> value to the client,
202 * followed by a
203 * carriage return-line feed (CRLF).
204 *
205 * @param b the <code>boolean</code> value
206 * to write to the client
207 *
208 * @exception IOException if an input or output exception occurred
209 */
210 public void println(boolean b) throws IOException {
211 print(b);
212 println();
213 }
214
215 /**
216 * Writes a character to the client, followed by a carriage
217 * return-line feed (CRLF).
218 *
219 * @param c the character to write to the client
220 *
221 * @exception IOException if an input or output exception occurred
222 */
223 public void println(char c) throws IOException {
224 print(c);
225 println();
226 }
227
228 /**
229 * Writes an int to the client, followed by a
230 * carriage return-line feed (CRLF) character.
231 *
232 * @param i the int to write to the client
233 *
234 * @exception IOException if an input or output exception occurred
235 */
236 public void println(int i) throws IOException {
237 print(i);
238 println();
239 }
240
241 /**
242 * Writes a <code>long</code> value to the client, followed by a
243 * carriage return-line feed (CRLF).
244 *
245 * @param l the <code>long</code> value to write to the client
246 *
247 * @exception IOException if an input or output exception occurred
248 */
249 public void println(long l) throws IOException {
250 print(l);
251 println();
252 }
253
254 /**
255 * Writes a <code>float</code> value to the client,
256 * followed by a carriage return-line feed (CRLF).
257 *
258 * @param f the <code>float</code> value to write to the client
259 *
260 * @exception IOException if an input or output exception occurred
261 */
262 public void println(float f) throws IOException {
263 print(f);
264 println();
265 }
266
267 /**
268 * Writes a <code>double</code> value to the client,
269 * followed by a carriage return-line feed (CRLF).
270 *
271 * @param d the <code>double</code> value
272 * to write to the client
273 *
274 * @exception IOException if an input or output exception occurred
275 */
276 public void println(double d) throws IOException {
277 print(d);
278 println();
279 }
280 }