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