Skip to content

Package: JspWriter

JspWriter

nameinstructionbranchcomplexitylinemethod
JspWriter(int, boolean)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
getBufferSize()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
isAutoFlush()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

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