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