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:
018: package org.apache.jasper.runtime;
019:
020: import java.io.CharArrayReader;
021: import java.io.IOException;
022: import java.io.Reader;
023: import java.io.Writer;
024:
025: import javax.servlet.jsp.JspWriter;
026: import javax.servlet.jsp.tagext.BodyContent;
027:
028: import org.apache.jasper.Constants;
029:
030: /**
031: * Write text to a character-output stream, buffering characters so as
032: * to provide for the efficient writing of single characters, arrays,
033: * and strings.
034: *
035: * Provide support for discarding for the output that has been buffered.
036: *
037: * @author Rajiv Mordani
038: * @author Jan Luehe
039: */
040: public class BodyContentImpl extends BodyContent {
041:
042: private static final String LINE_SEPARATOR = System
043: .getProperty("line.separator");
044: private static final boolean LIMIT_BUFFER = Boolean
045: .valueOf(
046: System
047: .getProperty(
048: "org.apache.jasper.runtime.BodyContentImpl.LIMIT_BUFFER",
049: "false")).booleanValue();
050:
051: private char[] cb;
052: private int nextChar;
053: private boolean closed;
054:
055: // Enclosed writer to which any output is written
056: private Writer writer;
057:
058: // See comment in setWriter()
059: private int bufferSizeSave;
060:
061: /**
062: * Constructor.
063: */
064: public BodyContentImpl(JspWriter enclosingWriter) {
065: super (enclosingWriter);
066: bufferSize = Constants.DEFAULT_TAG_BUFFER_SIZE;
067: cb = new char[bufferSize];
068: nextChar = 0;
069: closed = false;
070: }
071:
072: /**
073: * Write a single character.
074: */
075: public void write(int c) throws IOException {
076: if (writer != null) {
077: writer.write(c);
078: } else {
079: ensureOpen();
080: if (nextChar >= bufferSize) {
081: reAllocBuff(1);
082: }
083: cb[nextChar++] = (char) c;
084: }
085: }
086:
087: /**
088: * Write a portion of an array of characters.
089: *
090: * <p> Ordinarily this method stores characters from the given array into
091: * this stream's buffer, flushing the buffer to the underlying stream as
092: * needed. If the requested length is at least as large as the buffer,
093: * however, then this method will flush the buffer and write the characters
094: * directly to the underlying stream. Thus redundant
095: * <code>DiscardableBufferedWriter</code>s will not copy data
096: * unnecessarily.
097: *
098: * @param cbuf A character array
099: * @param off Offset from which to start reading characters
100: * @param len Number of characters to write
101: */
102: public void write(char[] cbuf, int off, int len) throws IOException {
103: if (writer != null) {
104: writer.write(cbuf, off, len);
105: } else {
106: ensureOpen();
107:
108: if ((off < 0) || (off > cbuf.length) || (len < 0)
109: || ((off + len) > cbuf.length) || ((off + len) < 0)) {
110: throw new IndexOutOfBoundsException();
111: } else if (len == 0) {
112: return;
113: }
114:
115: if (len >= bufferSize - nextChar)
116: reAllocBuff(len);
117:
118: System.arraycopy(cbuf, off, cb, nextChar, len);
119: nextChar += len;
120: }
121: }
122:
123: /**
124: * Write an array of characters. This method cannot be inherited from the
125: * Writer class because it must suppress I/O exceptions.
126: */
127: public void write(char[] buf) throws IOException {
128: if (writer != null) {
129: writer.write(buf);
130: } else {
131: write(buf, 0, buf.length);
132: }
133: }
134:
135: /**
136: * Write a portion of a String.
137: *
138: * @param s String to be written
139: * @param off Offset from which to start reading characters
140: * @param len Number of characters to be written
141: */
142: public void write(String s, int off, int len) throws IOException {
143: if (writer != null) {
144: writer.write(s, off, len);
145: } else {
146: ensureOpen();
147: if (len >= bufferSize - nextChar)
148: reAllocBuff(len);
149:
150: s.getChars(off, off + len, cb, nextChar);
151: nextChar += len;
152: }
153: }
154:
155: /**
156: * Write a string. This method cannot be inherited from the Writer class
157: * because it must suppress I/O exceptions.
158: */
159: public void write(String s) throws IOException {
160: if (writer != null) {
161: writer.write(s);
162: } else {
163: write(s, 0, s.length());
164: }
165: }
166:
167: /**
168: * Write a line separator. The line separator string is defined by the
169: * system property <tt>line.separator</tt>, and is not necessarily a single
170: * newline ('\n') character.
171: *
172: * @throws IOException If an I/O error occurs
173: */
174: public void newLine() throws IOException {
175: if (writer != null) {
176: writer.write(LINE_SEPARATOR);
177: } else {
178: write(LINE_SEPARATOR);
179: }
180: }
181:
182: /**
183: * Print a boolean value. The string produced by <code>{@link
184: * java.lang.String#valueOf(boolean)}</code> is translated into bytes
185: * according to the platform's default character encoding, and these bytes
186: * are written in exactly the manner of the <code>{@link
187: * #write(int)}</code> method.
188: *
189: * @param b The <code>boolean</code> to be printed
190: * @throws IOException
191: */
192: public void print(boolean b) throws IOException {
193: if (writer != null) {
194: writer.write(b ? "true" : "false");
195: } else {
196: write(b ? "true" : "false");
197: }
198: }
199:
200: /**
201: * Print a character. The character is translated into one or more bytes
202: * according to the platform's default character encoding, and these bytes
203: * are written in exactly the manner of the <code>{@link
204: * #write(int)}</code> method.
205: *
206: * @param c The <code>char</code> to be printed
207: * @throws IOException
208: */
209: public void print(char c) throws IOException {
210: if (writer != null) {
211: writer.write(String.valueOf(c));
212: } else {
213: write(String.valueOf(c));
214: }
215: }
216:
217: /**
218: * Print an integer. The string produced by <code>{@link
219: * java.lang.String#valueOf(int)}</code> is translated into bytes according
220: * to the platform's default character encoding, and these bytes are
221: * written in exactly the manner of the <code>{@link #write(int)}</code>
222: * method.
223: *
224: * @param i The <code>int</code> to be printed
225: * @throws IOException
226: */
227: public void print(int i) throws IOException {
228: if (writer != null) {
229: writer.write(String.valueOf(i));
230: } else {
231: write(String.valueOf(i));
232: }
233: }
234:
235: /**
236: * Print a long integer. The string produced by <code>{@link
237: * java.lang.String#valueOf(long)}</code> is translated into bytes
238: * according to the platform's default character encoding, and these bytes
239: * are written in exactly the manner of the
240: * <code>{@link #write(int)}</code> method.
241: *
242: * @param l The <code>long</code> to be printed
243: * @throws IOException
244: */
245: public void print(long l) throws IOException {
246: if (writer != null) {
247: writer.write(String.valueOf(l));
248: } else {
249: write(String.valueOf(l));
250: }
251: }
252:
253: /**
254: * Print a floating-point number. The string produced by <code>{@link
255: * java.lang.String#valueOf(float)}</code> is translated into bytes
256: * according to the platform's default character encoding, and these bytes
257: * are written in exactly the manner of the
258: * <code>{@link #write(int)}</code> method.
259: *
260: * @param f The <code>float</code> to be printed
261: * @throws IOException
262: */
263: public void print(float f) throws IOException {
264: if (writer != null) {
265: writer.write(String.valueOf(f));
266: } else {
267: write(String.valueOf(f));
268: }
269: }
270:
271: /**
272: * Print a double-precision floating-point number. The string produced by
273: * <code>{@link java.lang.String#valueOf(double)}</code> is translated into
274: * bytes according to the platform's default character encoding, and these
275: * bytes are written in exactly the manner of the <code>{@link
276: * #write(int)}</code> method.
277: *
278: * @param d The <code>double</code> to be printed
279: * @throws IOException
280: */
281: public void print(double d) throws IOException {
282: if (writer != null) {
283: writer.write(String.valueOf(d));
284: } else {
285: write(String.valueOf(d));
286: }
287: }
288:
289: /**
290: * Print an array of characters. The characters are converted into bytes
291: * according to the platform's default character encoding, and these bytes
292: * are written in exactly the manner of the
293: * <code>{@link #write(int)}</code> method.
294: *
295: * @param s The array of chars to be printed
296: *
297: * @throws NullPointerException If <code>s</code> is <code>null</code>
298: * @throws IOException
299: */
300: public void print(char[] s) throws IOException {
301: if (writer != null) {
302: writer.write(s);
303: } else {
304: write(s);
305: }
306: }
307:
308: /**
309: * Print a string. If the argument is <code>null</code> then the string
310: * <code>"null"</code> is printed. Otherwise, the string's characters are
311: * converted into bytes according to the platform's default character
312: * encoding, and these bytes are written in exactly the manner of the
313: * <code>{@link #write(int)}</code> method.
314: *
315: * @param s The <code>String</code> to be printed
316: * @throws IOException
317: */
318: public void print(String s) throws IOException {
319: if (s == null)
320: s = "null";
321: if (writer != null) {
322: writer.write(s);
323: } else {
324: write(s);
325: }
326: }
327:
328: /**
329: * Print an object. The string produced by the <code>{@link
330: * java.lang.String#valueOf(Object)}</code> method is translated into bytes
331: * according to the platform's default character encoding, and these bytes
332: * are written in exactly the manner of the
333: * <code>{@link #write(int)}</code> method.
334: *
335: * @param obj The <code>Object</code> to be printed
336: * @throws IOException
337: */
338: public void print(Object obj) throws IOException {
339: if (writer != null) {
340: writer.write(String.valueOf(obj));
341: } else {
342: write(String.valueOf(obj));
343: }
344: }
345:
346: /**
347: * Terminate the current line by writing the line separator string. The
348: * line separator string is defined by the system property
349: * <code>line.separator</code>, and is not necessarily a single newline
350: * character (<code>'\n'</code>).
351: *
352: * @throws IOException
353: */
354: public void println() throws IOException {
355: newLine();
356: }
357:
358: /**
359: * Print a boolean value and then terminate the line. This method behaves
360: * as though it invokes <code>{@link #print(boolean)}</code> and then
361: * <code>{@link #println()}</code>.
362: *
363: * @throws IOException
364: */
365: public void println(boolean x) throws IOException {
366: print(x);
367: println();
368: }
369:
370: /**
371: * Print a character and then terminate the line. This method behaves as
372: * though it invokes <code>{@link #print(char)}</code> and then
373: * <code>{@link #println()}</code>.
374: *
375: * @throws IOException
376: */
377: public void println(char x) throws IOException {
378: print(x);
379: println();
380: }
381:
382: /**
383: * Print an integer and then terminate the line. This method behaves as
384: * though it invokes <code>{@link #print(int)}</code> and then
385: * <code>{@link #println()}</code>.
386: *
387: * @throws IOException
388: */
389: public void println(int x) throws IOException {
390: print(x);
391: println();
392: }
393:
394: /**
395: * Print a long integer and then terminate the line. This method behaves
396: * as though it invokes <code>{@link #print(long)}</code> and then
397: * <code>{@link #println()}</code>.
398: *
399: * @throws IOException
400: */
401: public void println(long x) throws IOException {
402: print(x);
403: println();
404: }
405:
406: /**
407: * Print a floating-point number and then terminate the line. This method
408: * behaves as though it invokes <code>{@link #print(float)}</code> and then
409: * <code>{@link #println()}</code>.
410: *
411: * @throws IOException
412: */
413: public void println(float x) throws IOException {
414: print(x);
415: println();
416: }
417:
418: /**
419: * Print a double-precision floating-point number and then terminate the
420: * line. This method behaves as though it invokes <code>{@link
421: * #print(double)}</code> and then <code>{@link #println()}</code>.
422: *
423: * @throws IOException
424: */
425: public void println(double x) throws IOException {
426: print(x);
427: println();
428: }
429:
430: /**
431: * Print an array of characters and then terminate the line. This method
432: * behaves as though it invokes <code>{@link #print(char[])}</code> and
433: * then <code>{@link #println()}</code>.
434: *
435: * @throws IOException
436: */
437: public void println(char x[]) throws IOException {
438: print(x);
439: println();
440: }
441:
442: /**
443: * Print a String and then terminate the line. This method behaves as
444: * though it invokes <code>{@link #print(String)}</code> and then
445: * <code>{@link #println()}</code>.
446: *
447: * @throws IOException
448: */
449: public void println(String x) throws IOException {
450: print(x);
451: println();
452: }
453:
454: /**
455: * Print an Object and then terminate the line. This method behaves as
456: * though it invokes <code>{@link #print(Object)}</code> and then
457: * <code>{@link #println()}</code>.
458: *
459: * @throws IOException
460: */
461: public void println(Object x) throws IOException {
462: print(x);
463: println();
464: }
465:
466: /**
467: * Clear the contents of the buffer. If the buffer has been already
468: * been flushed then the clear operation shall throw an IOException
469: * to signal the fact that some data has already been irrevocably
470: * written to the client response stream.
471: *
472: * @throws IOException If an I/O error occurs
473: */
474: public void clear() throws IOException {
475: if (writer != null) {
476: throw new IOException();
477: } else {
478: nextChar = 0;
479: if (LIMIT_BUFFER
480: && (cb.length > Constants.DEFAULT_TAG_BUFFER_SIZE)) {
481: bufferSize = Constants.DEFAULT_TAG_BUFFER_SIZE;
482: cb = new char[bufferSize];
483: }
484: }
485: }
486:
487: /**
488: * Clears the current contents of the buffer. Unlike clear(), this
489: * mehtod will not throw an IOException if the buffer has already been
490: * flushed. It merely clears the current content of the buffer and
491: * returns.
492: *
493: * @throws IOException If an I/O error occurs
494: */
495: public void clearBuffer() throws IOException {
496: if (writer == null) {
497: this .clear();
498: }
499: }
500:
501: /**
502: * Close the stream, flushing it first. Once a stream has been closed,
503: * further write() or flush() invocations will cause an IOException to be
504: * thrown. Closing a previously-closed stream, however, has no effect.
505: *
506: * @throws IOException If an I/O error occurs
507: */
508: public void close() throws IOException {
509: if (writer != null) {
510: writer.close();
511: } else {
512: closed = true;
513: }
514: }
515:
516: /**
517: * @return the number of bytes unused in the buffer
518: */
519: public int getRemaining() {
520: return (writer == null) ? bufferSize - nextChar : 0;
521: }
522:
523: /**
524: * Return the value of this BodyJspWriter as a Reader.
525: * Note: this is after evaluation!! There are no scriptlets,
526: * etc in this stream.
527: *
528: * @return the value of this BodyJspWriter as a Reader
529: */
530: public Reader getReader() {
531: return (writer == null) ? new CharArrayReader(cb, 0, nextChar)
532: : null;
533: }
534:
535: /**
536: * Return the value of the BodyJspWriter as a String.
537: * Note: this is after evaluation!! There are no scriptlets,
538: * etc in this stream.
539: *
540: * @return the value of the BodyJspWriter as a String
541: */
542: public String getString() {
543: return (writer == null) ? new String(cb, 0, nextChar) : null;
544: }
545:
546: /**
547: * Write the contents of this BodyJspWriter into a Writer.
548: * Subclasses are likely to do interesting things with the
549: * implementation so some things are extra efficient.
550: *
551: * @param out The writer into which to place the contents of this body
552: * evaluation
553: */
554: public void writeOut(Writer out) throws IOException {
555: if (writer == null) {
556: out.write(cb, 0, nextChar);
557: // Flush not called as the writer passed could be a BodyContent and
558: // it doesn't allow to flush.
559: }
560: }
561:
562: /**
563: * Sets the writer to which all output is written.
564: */
565: void setWriter(Writer writer) {
566: this .writer = writer;
567: closed = false;
568: if (writer != null) {
569: // According to the spec, the JspWriter returned by
570: // JspContext.pushBody(java.io.Writer writer) must behave as
571: // though it were unbuffered. This means that its getBufferSize()
572: // must always return 0. The implementation of
573: // JspWriter.getBufferSize() returns the value of JspWriter's
574: // 'bufferSize' field, which is inherited by this class.
575: // Therefore, we simply save the current 'bufferSize' (so we can
576: // later restore it should this BodyContentImpl ever be reused by
577: // a call to PageContext.pushBody()) before setting it to 0.
578: if (bufferSize != 0) {
579: bufferSizeSave = bufferSize;
580: bufferSize = 0;
581: }
582: } else {
583: bufferSize = bufferSizeSave;
584: clearBody();
585: }
586: }
587:
588: private void ensureOpen() throws IOException {
589: if (closed)
590: throw new IOException("Stream closed");
591: }
592:
593: /**
594: * Reallocates buffer since the spec requires it to be unbounded.
595: */
596: private void reAllocBuff(int len) {
597:
598: if (bufferSize + len <= cb.length) {
599: bufferSize = cb.length;
600: return;
601: }
602:
603: if (len < cb.length) {
604: len = cb.length;
605: }
606:
607: bufferSize = cb.length + len;
608: char[] tmp = new char[bufferSize];
609:
610: System.arraycopy(cb, 0, tmp, 0, cb.length);
611: cb = tmp;
612: tmp = null;
613:
614: }
615:
616: }
|