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. package javax.servlet.jsp;
  56. import java.io.IOException;
  57. /**
  58. * <p>
  59. * The actions and template data in a JSP page is written using the
  60. * JspWriter object that is referenced by the implicit variable out which
  61. * is initialized automatically using methods in the PageContext object.
  62. *<p>
  63. * This abstract class emulates some of the functionality found in the
  64. * java.io.BufferedWriter and java.io.PrintWriter classes,
  65. * however it differs in that it throws java.io.IOException from the print
  66. * methods while PrintWriter does not.
  67. * <p><B>Buffering</B>
  68. * <p>
  69. * The initial JspWriter object is associated with the PrintWriter object
  70. * of the ServletResponse in a way that depends on whether the page is or
  71. * is not buffered. If the page is not buffered, output written to this
  72. * JspWriter object will be written through to the PrintWriter directly,
  73. * which will be created if necessary by invoking the getWriter() method
  74. * on the response object. But if the page is buffered, the PrintWriter
  75. * object will not be created until the buffer is flushed and
  76. * operations like setContentType() are legal. Since this flexibility
  77. * simplifies programming substantially, buffering is the default for JSP
  78. * pages.
  79. * <p>
  80. * Buffering raises the issue of what to do when the buffer is
  81. * exceeded. Two approaches can be taken:
  82. * <ul>
  83. * <li>
  84. * Exceeding the buffer is not a fatal error; when the buffer is
  85. * exceeded, just flush the output.
  86. * <li>
  87. * Exceeding the buffer is a fatal error; when the buffer is exceeded,
  88. * raise an exception.
  89. * </ul>
  90. * <p>
  91. * Both approaches are valid, and thus both are supported in the JSP
  92. * technology. The behavior of a page is controlled by the autoFlush
  93. * attribute, which defaults to true. In general, JSP pages that need to
  94. * be sure that correct and complete data has been sent to their client
  95. * may want to set autoFlush to false, with a typical case being that
  96. * where the client is an application itself. On the other hand, JSP
  97. * pages that send data that is meaningful even when partially
  98. * constructed may want to set autoFlush to true; such as when the
  99. * data is sent for immediate display through a browser. Each application
  100. * will need to consider their specific needs.
  101. * <p>
  102. * An alternative considered was to make the buffer size unbounded; but,
  103. * this had the disadvantage that runaway computations would consume an
  104. * unbounded amount of resources.
  105. * <p>
  106. * The "out" implicit variable of a JSP implementation class is of this type.
  107. * If the page directive selects autoflush="true" then all the I/O operations
  108. * on this class shall automatically flush the contents of the buffer if an
  109. * overflow condition would result if the current operation were performed
  110. * without a flush. If autoflush="false" then all the I/O operations on this
  111. * class shall throw an IOException if performing the current operation would
  112. * result in a buffer overflow condition.
  113. *
  114. * @see java.io.Writer
  115. * @see java.io.BufferedWriter
  116. * @see java.io.PrintWriter
  117. */
  118. abstract public class JspWriter extends java.io.Writer {
  119. /**
  120. * constant indicating that the Writer is not buffering output
  121. */
  122. public static final int NO_BUFFER = 0;
  123. /**
  124. * constant indicating that the Writer is buffered and is using the implementation default buffer size
  125. */
  126. public static final int DEFAULT_BUFFER = -1;
  127. /**
  128. * constant indicating that the Writer is buffered and is unbounded; this is used in BodyContent
  129. */
  130. public static final int UNBOUNDED_BUFFER = -2;
  131. /**
  132. * protected constructor.
  133. */
  134. protected JspWriter(int bufferSize, boolean autoFlush) {
  135. this.bufferSize = bufferSize;
  136. this.autoFlush = autoFlush;
  137. }
  138. /**
  139. * Write a line separator. The line separator string is defined by the
  140. * system property <tt>line.separator</tt>, and is not necessarily a single
  141. * newline ('\n') character.
  142. *
  143. * @exception IOException If an I/O error occurs
  144. */
  145. abstract public void newLine() throws IOException;
  146. /**
  147. * Print a boolean value. The string produced by <code>{@link
  148. * java.lang.String#valueOf(boolean)}</code> is translated into bytes
  149. * according to the platform's default character encoding, and these bytes
  150. * are written in exactly the manner of the <code>{@link
  151. * #write(int)}</code> method.
  152. *
  153. * @param b The <code>boolean</code> to be printed
  154. * @throws java.io.IOException
  155. */
  156. abstract public void print(boolean b) throws IOException;
  157. /**
  158. * Print a character. The character is translated into one or more bytes
  159. * according to the platform's default character encoding, and these bytes
  160. * are written in exactly the manner of the <code>{@link
  161. * #write(int)}</code> method.
  162. *
  163. * @param c The <code>char</code> to be printed
  164. * @throws java.io.IOException
  165. */
  166. abstract public void print(char c) throws IOException;
  167. /**
  168. * Print an integer. The string produced by <code>{@link
  169. * java.lang.String#valueOf(int)}</code> is translated into bytes according
  170. * to the platform's default character encoding, and these bytes are
  171. * written in exactly the manner of the <code>{@link #write(int)}</code>
  172. * method.
  173. *
  174. * @param i The <code>int</code> to be printed
  175. * @see java.lang.Integer#toString(int)
  176. * @throws java.io.IOException
  177. */
  178. abstract public void print(int i) throws IOException;
  179. /**
  180. * Print a long integer. The string produced by <code>{@link
  181. * java.lang.String#valueOf(long)}</code> is translated into bytes
  182. * according to the platform's default character encoding, and these bytes
  183. * are written in exactly the manner of the <code>{@link #write(int)}</code>
  184. * method.
  185. *
  186. * @param l The <code>long</code> to be printed
  187. * @see java.lang.Long#toString(long)
  188. * @throws java.io.IOException
  189. */
  190. abstract public void print(long l) throws IOException;
  191. /**
  192. * Print a floating-point number. The string produced by <code>{@link
  193. * java.lang.String#valueOf(float)}</code> is translated into bytes
  194. * according to the platform's default character encoding, and these bytes
  195. * are written in exactly the manner of the <code>{@link #write(int)}</code>
  196. * method.
  197. *
  198. * @param f The <code>float</code> to be printed
  199. * @see java.lang.Float#toString(float)
  200. * @throws java.io.IOException
  201. */
  202. abstract public void print(float f) throws IOException;
  203. /**
  204. * Print a double-precision floating-point number. The string produced by
  205. * <code>{@link java.lang.String#valueOf(double)}</code> is translated into
  206. * bytes according to the platform's default character encoding, and these
  207. * bytes are written in exactly the manner of the <code>{@link
  208. * #write(int)}</code> method.
  209. *
  210. * @param d The <code>double</code> to be printed
  211. * @see java.lang.Double#toString(double)
  212. * @throws java.io.IOException
  213. */
  214. abstract public void print(double d) throws IOException;
  215. /**
  216. * Print an array of characters. The characters are converted into bytes
  217. * according to the platform's default character encoding, and these bytes
  218. * are written in exactly the manner of the <code>{@link #write(int)}</code>
  219. * method.
  220. *
  221. * @param s The array of chars to be printed
  222. *
  223. * @throws NullPointerException If <code>s</code> is <code>null</code>
  224. * @throws java.io.IOException
  225. */
  226. abstract public void print(char s[]) throws IOException;
  227. /**
  228. * Print a string. If the argument is <code>null</code> then the string
  229. * <code>"null"</code> is printed. Otherwise, the string's characters are
  230. * converted into bytes according to the platform's default character
  231. * encoding, and these bytes are written in exactly the manner of the
  232. * <code>{@link #write(int)}</code> method.
  233. *
  234. * @param s The <code>String</code> to be printed
  235. * @throws java.io.IOException
  236. */
  237. abstract public void print(String s) throws IOException;
  238. /**
  239. * Print an object. The string produced by the <code>{@link
  240. * java.lang.String#valueOf(Object)}</code> method is translated into bytes
  241. * according to the platform's default character encoding, and these bytes
  242. * are written in exactly the manner of the <code>{@link #write(int)}</code>
  243. * method.
  244. *
  245. * @param obj The <code>Object</code> to be printed
  246. * @see java.lang.Object#toString()
  247. * @throws java.io.IOException
  248. */
  249. abstract public void print(Object obj) throws IOException;
  250. /**
  251. * Terminate the current line by writing the line separator string. The
  252. * line separator string is defined by the system property
  253. * <code>line.separator</code>, and is not necessarily a single newline
  254. * character (<code>'\n'</code>).
  255. * @throws java.io.IOException
  256. */
  257. abstract public void println() throws IOException;
  258. /**
  259. * Print a boolean value and then terminate the line. This method behaves
  260. * as though it invokes <code>{@link #print(boolean)}</code> and then
  261. * <code>{@link #println()}</code>.
  262. * @throws java.io.IOException
  263. */
  264. abstract public void println(boolean x) throws IOException;
  265. /**
  266. * Print a character and then terminate the line. This method behaves as
  267. * though it invokes <code>{@link #print(char)}</code> and then <code>{@link
  268. * #println()}</code>.
  269. * @throws java.io.IOException
  270. */
  271. abstract public void println(char x) throws IOException;
  272. /**
  273. * Print an integer and then terminate the line. This method behaves as
  274. * though it invokes <code>{@link #print(int)}</code> and then <code>{@link
  275. * #println()}</code>.
  276. * @throws java.io.IOException
  277. */
  278. abstract public void println(int x) throws IOException;
  279. /**
  280. * Print a long integer and then terminate the line. This method behaves
  281. * as though it invokes <code>{@link #print(long)}</code> and then
  282. * <code>{@link #println()}</code>.
  283. * @throws java.io.IOException
  284. */
  285. abstract public void println(long x) throws IOException;
  286. /**
  287. * Print a floating-point number and then terminate the line. This method
  288. * behaves as though it invokes <code>{@link #print(float)}</code> and then
  289. * <code>{@link #println()}</code>.
  290. * @throws java.io.IOException
  291. */
  292. abstract public void println(float x) throws IOException;
  293. /**
  294. * Print a double-precision floating-point number and then terminate the
  295. * line. This method behaves as though it invokes <code>{@link
  296. * #print(double)}</code> and then <code>{@link #println()}</code>.
  297. * @throws java.io.IOException
  298. */
  299. abstract public void println(double x) throws IOException;
  300. /**
  301. * Print an array of characters and then terminate the line. This method
  302. * behaves as though it invokes <code>print(char[])</code> and then
  303. * <code>println()</code>.
  304. * @throws java.io.IOException
  305. */
  306. abstract public void println(char x[]) throws IOException;
  307. /**
  308. * Print a String and then terminate the line. This method behaves as
  309. * though it invokes <code>{@link #print(String)}</code> and then
  310. * <code>{@link #println()}</code>.
  311. * @throws java.io.IOException
  312. */
  313. abstract public void println(String x) throws IOException;
  314. /**
  315. * Print an Object and then terminate the line. This method behaves as
  316. * though it invokes <code>{@link #print(Object)}</code> and then
  317. * <code>{@link #println()}</code>.
  318. * @throws java.io.IOException
  319. */
  320. abstract public void println(Object x) throws IOException;
  321. /**
  322. * Clear the contents of the buffer. If the buffer has been already
  323. * been flushed then the clear operation shall throw an IOException
  324. * to signal the fact that some data has already been irrevocably
  325. * written to the client response stream.
  326. *
  327. * @throws IOException If an I/O error occurs
  328. */
  329. abstract public void clear() throws IOException;
  330. /**
  331. * Clears the current contents of the buffer. Unlike clear(), this
  332. * method will not throw an IOException if the buffer has already been
  333. * flushed. It merely clears the current content of the buffer and
  334. * returns.
  335. *
  336. * @throws IOException If an I/O error occurs
  337. */
  338. abstract public void clearBuffer() throws IOException;
  339. /**
  340. * Flush the stream. If the stream has saved any characters from the
  341. * various write() methods in a buffer, write them immediately to their
  342. * intended destination. Then, if that destination is another character or
  343. * byte stream, flush it. Thus one flush() invocation will flush all the
  344. * buffers in a chain of Writers and OutputStreams.
  345. * <p>
  346. * The method may be invoked indirectly if the buffer size is exceeded.
  347. * <p>
  348. * Once a stream has been closed,
  349. * further write() or flush() invocations will cause an IOException to be
  350. * thrown.
  351. *
  352. * @exception IOException If an I/O error occurs
  353. */
  354. abstract public void flush() throws IOException;
  355. /**
  356. * Close the stream, flushing it first
  357. * <p>
  358. * This method needs not be invoked explicitly for the initial JspWriter
  359. * as the code generated by the JSP container will automatically
  360. * include a call to close().
  361. * <p>
  362. * Closing a previously-closed stream, unlike flush(), has no effect.
  363. *
  364. * @exception IOException If an I/O error occurs
  365. */
  366. abstract public void close() throws IOException;
  367. /**
  368. * This method returns the size of the buffer used by the JspWriter.
  369. *
  370. * @return the size of the buffer in bytes, or 0 is unbuffered.
  371. */
  372. public int getBufferSize() { return bufferSize; }
  373. /**
  374. * This method returns the number of unused bytes in the buffer.
  375. *
  376. * @return the number of bytes unused in the buffer
  377. */
  378. abstract public int getRemaining();
  379. /**
  380. * This method indicates whether the JspWriter is autoFlushing.
  381. *
  382. * @return if this JspWriter is auto flushing or throwing IOExceptions on buffer overflow conditions
  383. */
  384. public boolean isAutoFlush() { return autoFlush; }
  385. /*
  386. * fields
  387. */
  388. protected int bufferSize;
  389. protected boolean autoFlush;
  390. }