1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 1999 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. *
  54. * ====================================================================
  55. *
  56. * This source code implements specifications defined by the Java
  57. * Community Process. In order to remain compliant with the specification
  58. * DO NOT add / change / or delete method signatures!
  59. */
  60. package javax.servlet;
  61. import java.io.OutputStream;
  62. import java.io.IOException;
  63. import java.io.CharConversionException;
  64. import java.text.MessageFormat;
  65. import java.util.ResourceBundle;
  66. /**
  67. * Provides an output stream for sending binary data to the
  68. * client. A <code>ServletOutputStream</code> object is normally retrieved
  69. * via the {@link ServletResponse#getOutputStream} method.
  70. *
  71. * <p>This is an abstract class that the servlet container implements.
  72. * Subclasses of this class
  73. * must implement the <code>java.io.OutputStream.write(int)</code>
  74. * method.
  75. *
  76. *
  77. * @author Various
  78. * @version $Version$
  79. *
  80. * @see ServletResponse
  81. *
  82. */
  83. public abstract class ServletOutputStream extends OutputStream {
  84. private static final String LSTRING_FILE = "javax.servlet.LocalStrings";
  85. private static ResourceBundle lStrings =
  86. ResourceBundle.getBundle(LSTRING_FILE);
  87. /**
  88. *
  89. * Does nothing, because this is an abstract class.
  90. *
  91. */
  92. protected ServletOutputStream() { }
  93. /**
  94. * Writes a <code>String</code> to the client,
  95. * without a carriage return-line feed (CRLF)
  96. * character at the end.
  97. *
  98. *
  99. * @param s the <code>String</code to send to the client
  100. *
  101. * @exception IOException if an input or output exception occurred
  102. *
  103. */
  104. public void print(String s) throws IOException {
  105. if (s==null) s="null";
  106. int len = s.length();
  107. for (int i = 0; i < len; i++) {
  108. char c = s.charAt (i);
  109. //
  110. // XXX NOTE: This is clearly incorrect for many strings,
  111. // but is the only consistent approach within the current
  112. // servlet framework. It must suffice until servlet output
  113. // streams properly encode their output.
  114. //
  115. if ((c & 0xff00) != 0) { // high order byte must be zero
  116. String errMsg = lStrings.getString("err.not_iso8859_1");
  117. Object[] errArgs = new Object[1];
  118. errArgs[0] = new Character(c);
  119. errMsg = MessageFormat.format(errMsg, errArgs);
  120. throw new CharConversionException(errMsg);
  121. }
  122. write (c);
  123. }
  124. }
  125. /**
  126. * Writes a <code>boolean</code> value to the client,
  127. * with no carriage return-line feed (CRLF)
  128. * character at the end.
  129. *
  130. * @param b the <code>boolean</code> value
  131. * to send to the client
  132. *
  133. * @exception IOException if an input or output exception occurred
  134. *
  135. */
  136. public void print(boolean b) throws IOException {
  137. String msg;
  138. if (b) {
  139. msg = lStrings.getString("value.true");
  140. } else {
  141. msg = lStrings.getString("value.false");
  142. }
  143. print(msg);
  144. }
  145. /**
  146. * Writes a character to the client,
  147. * with no carriage return-line feed (CRLF)
  148. * at the end.
  149. *
  150. * @param c the character to send to the client
  151. *
  152. * @exception IOException if an input or output exception occurred
  153. *
  154. */
  155. public void print(char c) throws IOException {
  156. print(String.valueOf(c));
  157. }
  158. /**
  159. *
  160. * Writes an int to the client,
  161. * with no carriage return-line feed (CRLF)
  162. * at the end.
  163. *
  164. * @param i the int to send to the client
  165. *
  166. * @exception IOException if an input or output exception occurred
  167. *
  168. */
  169. public void print(int i) throws IOException {
  170. print(String.valueOf(i));
  171. }
  172. /**
  173. *
  174. * Writes a <code>long</code> value to the client,
  175. * with no carriage return-line feed (CRLF) at the end.
  176. *
  177. * @param l the <code>long</code> value
  178. * to send to the client
  179. *
  180. * @exception IOException if an input or output exception
  181. * occurred
  182. *
  183. */
  184. public void print(long l) throws IOException {
  185. print(String.valueOf(l));
  186. }
  187. /**
  188. *
  189. * Writes a <code>float</code> value to the client,
  190. * with no carriage return-line feed (CRLF) at the end.
  191. *
  192. * @param f the <code>float</code> value
  193. * to send to the client
  194. *
  195. * @exception IOException if an input or output exception occurred
  196. *
  197. *
  198. */
  199. public void print(float f) throws IOException {
  200. print(String.valueOf(f));
  201. }
  202. /**
  203. *
  204. * Writes a <code>double</code> value to the client,
  205. * with no carriage return-line feed (CRLF) at the end.
  206. *
  207. * @param d the <code>double</code> value
  208. * to send to the client
  209. *
  210. * @exception IOException if an input or output exception occurred
  211. *
  212. */
  213. public void print(double d) throws IOException {
  214. print(String.valueOf(d));
  215. }
  216. /**
  217. * Writes a carriage return-line feed (CRLF)
  218. * to the client.
  219. *
  220. *
  221. *
  222. * @exception IOException if an input or output exception occurred
  223. *
  224. */
  225. public void println() throws IOException {
  226. print("\r\n");
  227. }
  228. /**
  229. * Writes a <code>String</code> to the client,
  230. * followed by a carriage return-line feed (CRLF).
  231. *
  232. *
  233. * @param s the </code>String</code> to write to the client
  234. *
  235. * @exception IOException if an input or output exception occurred
  236. *
  237. */
  238. public void println(String s) throws IOException {
  239. print(s);
  240. println();
  241. }
  242. /**
  243. *
  244. * Writes a <code>boolean</code> value to the client,
  245. * followed by a
  246. * carriage return-line feed (CRLF).
  247. *
  248. *
  249. * @param b the <code>boolean</code> value
  250. * to write to the client
  251. *
  252. * @exception IOException if an input or output exception occurred
  253. *
  254. */
  255. public void println(boolean b) throws IOException {
  256. print(b);
  257. println();
  258. }
  259. /**
  260. *
  261. * Writes a character to the client, followed by a carriage
  262. * return-line feed (CRLF).
  263. *
  264. * @param c the character to write to the client
  265. *
  266. * @exception IOException if an input or output exception occurred
  267. *
  268. */
  269. public void println(char c) throws IOException {
  270. print(c);
  271. println();
  272. }
  273. /**
  274. *
  275. * Writes an int to the client, followed by a
  276. * carriage return-line feed (CRLF) character.
  277. *
  278. *
  279. * @param i the int to write to the client
  280. *
  281. * @exception IOException if an input or output exception occurred
  282. *
  283. */
  284. public void println(int i) throws IOException {
  285. print(i);
  286. println();
  287. }
  288. /**
  289. *
  290. * Writes a <code>long</code> value to the client, followed by a
  291. * carriage return-line feed (CRLF).
  292. *
  293. *
  294. * @param l the <code>long</code> value to write to the client
  295. *
  296. * @exception IOException if an input or output exception occurred
  297. *
  298. */
  299. public void println(long l) throws IOException {
  300. print(l);
  301. println();
  302. }
  303. /**
  304. *
  305. * Writes a <code>float</code> value to the client,
  306. * followed by a carriage return-line feed (CRLF).
  307. *
  308. * @param f the <code>float</code> value
  309. * to write to the client
  310. *
  311. *
  312. * @exception IOException if an input or output exception
  313. * occurred
  314. *
  315. */
  316. public void println(float f) throws IOException {
  317. print(f);
  318. println();
  319. }
  320. /**
  321. *
  322. * Writes a <code>double</code> value to the client,
  323. * followed by a carriage return-line feed (CRLF).
  324. *
  325. *
  326. * @param d the <code>double</code> value
  327. * to write to the client
  328. *
  329. * @exception IOException if an input or output exception occurred
  330. *
  331. */
  332. public void println(double d) throws IOException {
  333. print(d);
  334. println();
  335. }
  336. }