FileDocCategorySizeDatePackage
BodyContentImpl.javaAPI DocGlassfish v2 API19499Fri May 04 22:32:54 BST 2007org.apache.jasper.runtime

BodyContentImpl

public class BodyContentImpl extends javax.servlet.jsp.tagext.BodyContent
Write text to a character-output stream, buffering characters so as to provide for the efficient writing of single characters, arrays, and strings. Provide support for discarding for the output that has been buffered.
author
Rajiv Mordani
author
Jan Luehe

Fields Summary
private static final String
LINE_SEPARATOR
private char[]
cb
private int
nextChar
private boolean
closed
private Writer
writer
private int
bufferSizeSave
Constructors Summary
public BodyContentImpl(javax.servlet.jsp.JspWriter enclosingWriter)
Constructor.


          
       
        super(enclosingWriter);
	bufferSize = Constants.DEFAULT_TAG_BUFFER_SIZE;
	cb = new char[bufferSize];
	nextChar = 0;
	closed = false;
    
Methods Summary
public voidclear()
Clear the contents of the buffer. If the buffer has been already been flushed then the clear operation shall throw an IOException to signal the fact that some data has already been irrevocably written to the client response stream.

throws
IOException If an I/O error occurs

	if (writer != null) {
	    throw new IOException();
	} else {
	    nextChar = 0;
	}
    
public voidclearBuffer()
Clears the current contents of the buffer. Unlike clear(), this mehtod will not throw an IOException if the buffer has already been flushed. It merely clears the current content of the buffer and returns.

throws
IOException If an I/O error occurs

        if (writer == null) {
	    this.clear();
	}
    
public voidclose()
Close the stream, flushing it first. Once a stream has been closed, further write() or flush() invocations will cause an IOException to be thrown. Closing a previously-closed stream, however, has no effect.

throws
IOException If an I/O error occurs

	if (writer != null) {
	    writer.close();
	} else {
	    cb = null;	
	    closed = true;
	}
    
private voidensureOpen()

	if (closed) throw new IOException("Stream closed");
    
public java.io.ReadergetReader()
Return the value of this BodyJspWriter as a Reader. Note: this is after evaluation!! There are no scriptlets, etc in this stream.

return
the value of this BodyJspWriter as a Reader

	return (writer == null) ? new CharArrayReader (cb, 0, nextChar) : null;
    
public intgetRemaining()

return
the number of bytes unused in the buffer

	return (writer == null) ? bufferSize-nextChar : 0;
    
public java.lang.StringgetString()
Return the value of the BodyJspWriter as a String. Note: this is after evaluation!! There are no scriptlets, etc in this stream.

return
the value of the BodyJspWriter as a String

	return (writer == null) ? new String(cb, 0, nextChar) : null;
    
public static voidmain(java.lang.String[] args)

	char[] buff = {'f",'o",'o",'b",'a",'r",'b",'a",'z",'y"};
   	BodyContentImpl bodyContent
	    = new BodyContentImpl(new JspWriterImpl(null, 100, false));
	bodyContent.println (buff);
	System.out.println (bodyContent.getString ());
	bodyContent.writeOut (new PrintWriter (System.out));
    
public voidnewLine()
Write a line separator. The line separator string is defined by the system property line.separator, and is not necessarily a single newline ('\n') character.

throws
IOException If an I/O error occurs

	if (writer != null) {
	    writer.write(LINE_SEPARATOR);
	} else {
	    write(LINE_SEPARATOR);
	}
    
public voidprint(int i)
Print an integer. The string produced by {@link java.lang.String#valueOf(int)} is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the {@link #write(int)} method.

param
i The int to be printed
throws
IOException

	if (writer != null) {
	    writer.write(String.valueOf(i));
	} else {
	    write(String.valueOf(i));
	}
    
public voidprint(long l)
Print a long integer. The string produced by {@link java.lang.String#valueOf(long)} is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the {@link #write(int)} method.

param
l The long to be printed
throws
IOException

	if (writer != null) {
	    writer.write(String.valueOf(l));
	} else {
	    write(String.valueOf(l));
	}
    
public voidprint(float f)
Print a floating-point number. The string produced by {@link java.lang.String#valueOf(float)} is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the {@link #write(int)} method.

param
f The float to be printed
throws
IOException

	if (writer != null) {
	    writer.write(String.valueOf(f));
	} else {
	    write(String.valueOf(f));
	}
    
public voidprint(double d)
Print a double-precision floating-point number. The string produced by {@link java.lang.String#valueOf(double)} is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the {@link #write(int)} method.

param
d The double to be printed
throws
IOException

	if (writer != null) {
	    writer.write(String.valueOf(d));
	} else {
	    write(String.valueOf(d));
	}
    
public voidprint(char[] s)
Print an array of characters. The characters are converted into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the {@link #write(int)} method.

param
s The array of chars to be printed
throws
NullPointerException If s is null
throws
IOException

	if (writer != null) {
	    writer.write(s);
	} else {
	    write(s);
	}
    
public voidprint(java.lang.String s)
Print a string. If the argument is null then the string "null" is printed. Otherwise, the string's characters are converted into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the {@link #write(int)} method.

param
s The String to be printed
throws
IOException

	if (s == null) s = "null";
	if (writer != null) {
	    writer.write(s);
	} else {
	    write(s);
	}
    
public voidprint(java.lang.Object obj)
Print an object. The string produced by the {@link java.lang.String#valueOf(Object)} method is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the {@link #write(int)} method.

param
obj The Object to be printed
throws
IOException

	if (writer != null) {
	    writer.write(String.valueOf(obj));
	} else {
	    write(String.valueOf(obj));
	}
    
public voidprint(boolean b)
Print a boolean value. The string produced by {@link java.lang.String#valueOf(boolean)} is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the {@link #write(int)} method.

param
b The boolean to be printed
throws
IOException

	if (writer != null) {
	    writer.write(b ? "true" : "false");
	} else {
	    write(b ? "true" : "false");
	}
    
public voidprint(char c)
Print a character. The character is translated into one or more bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the {@link #write(int)} method.

param
c The char to be printed
throws
IOException

	if (writer != null) {
	    writer.write(String.valueOf(c));
	} else {
	    write(String.valueOf(c));
	}
    
public voidprintln()
Terminate the current line by writing the line separator string. The line separator string is defined by the system property line.separator, and is not necessarily a single newline character ('\n').

throws
IOException

	newLine();
    
public voidprintln(boolean x)
Print a boolean value and then terminate the line. This method behaves as though it invokes {@link #print(boolean)} and then {@link #println()}.

throws
IOException

        print(x);
        println();
    
public voidprintln(char x)
Print a character and then terminate the line. This method behaves as though it invokes {@link #print(char)} and then {@link #println()}.

throws
IOException

        print(x);
        println();
    
public voidprintln(int x)
Print an integer and then terminate the line. This method behaves as though it invokes {@link #print(int)} and then {@link #println()}.

throws
IOException

        print(x);
        println();
    
public voidprintln(long x)
Print a long integer and then terminate the line. This method behaves as though it invokes {@link #print(long)} and then {@link #println()}.

throws
IOException

        print(x);
        println();
    
public voidprintln(float x)
Print a floating-point number and then terminate the line. This method behaves as though it invokes {@link #print(float)} and then {@link #println()}.

throws
IOException

        print(x);
        println();
    
public voidprintln(double x)
Print a double-precision floating-point number and then terminate the line. This method behaves as though it invokes {@link #print(double)} and then {@link #println()}.

throws
IOException

        print(x);
        println();
    
public voidprintln(char[] x)
Print an array of characters and then terminate the line. This method behaves as though it invokes {@link #print(char[])} and then {@link #println()}.

throws
IOException

        print(x);
        println();
    
public voidprintln(java.lang.String x)
Print a String and then terminate the line. This method behaves as though it invokes {@link #print(String)} and then {@link #println()}.

throws
IOException

        print(x);
        println();
    
public voidprintln(java.lang.Object x)
Print an Object and then terminate the line. This method behaves as though it invokes {@link #print(Object)} and then {@link #println()}.

throws
IOException

        print(x);
        println();
    
private voidreAllocBuff(int len)
Reallocates buffer since the spec requires it to be unbounded.


        if (bufferSize + len <= cb.length) {
            bufferSize = cb.length;
            return;
        }

        if (len < cb.length) {
            len = cb.length;
        }

        bufferSize = cb.length + len;
        char[] tmp = new char[bufferSize];

	System.arraycopy(cb, 0, tmp, 0, cb.length);
	cb = tmp;
	tmp = null;

    
voidsetWriter(java.io.Writer writer)
Sets the writer to which all output is written.

	this.writer = writer;
	if (writer != null) {
	    // According to the spec, the JspWriter returned by 
	    // JspContext.pushBody(java.io.Writer writer) must behave as
	    // though it were unbuffered. This means that its getBufferSize()
	    // must always return 0. The implementation of
	    // JspWriter.getBufferSize() returns the value of JspWriter's
	    // 'bufferSize' field, which is inherited by this class. 
	    // Therefore, we simply save the current 'bufferSize' (so we can 
	    // later restore it should this BodyContentImpl ever be reused by
	    // a call to PageContext.pushBody()) before setting it to 0.
	    if (bufferSize != 0) {
		bufferSizeSave = bufferSize;
		bufferSize = 0;
	    }
	} else {
	    bufferSize = bufferSizeSave;
	    clearBody();
	}
    
public voidwrite(int c)
Write a single character.

	if (writer != null) {
	    writer.write(c);
	} else {
	    ensureOpen();
	    if (nextChar >= bufferSize) {
		reAllocBuff (1);
	    }
	    cb[nextChar++] = (char) c;
	}
    
public voidwrite(char[] cbuf, int off, int len)
Write a portion of an array of characters.

Ordinarily this method stores characters from the given array into this stream's buffer, flushing the buffer to the underlying stream as needed. If the requested length is at least as large as the buffer, however, then this method will flush the buffer and write the characters directly to the underlying stream. Thus redundant DiscardableBufferedWriters will not copy data unnecessarily.

param
cbuf A character array
param
off Offset from which to start reading characters
param
len Number of characters to write

	if (writer != null) {
	    writer.write(cbuf, off, len);
	} else {
	    ensureOpen();

	    if ((off < 0) || (off > cbuf.length) || (len < 0) ||
		((off + len) > cbuf.length) || ((off + len) < 0)) {
		throw new IndexOutOfBoundsException();
	    } else if (len == 0) {
		return;
	    } 
	    
	    if (len >= bufferSize - nextChar)
		reAllocBuff (len);
	    
	    System.arraycopy(cbuf, off, cb, nextChar, len);
	    nextChar+=len;
	}
    
public voidwrite(char[] buf)
Write an array of characters. This method cannot be inherited from the Writer class because it must suppress I/O exceptions.

	if (writer != null) {
	    writer.write(buf);
	} else {
	    write(buf, 0, buf.length);
	}
    
public voidwrite(java.lang.String s, int off, int len)
Write a portion of a String.

param
s String to be written
param
off Offset from which to start reading characters
param
len Number of characters to be written

	if (writer != null) {
	    writer.write(s, off, len);
	} else {
	    ensureOpen();
	    if (len >= bufferSize - nextChar)
		reAllocBuff(len);

	    s.getChars(off, off + len, cb, nextChar);
	    nextChar += len;
	}
    
public voidwrite(java.lang.String s)
Write a string. This method cannot be inherited from the Writer class because it must suppress I/O exceptions.

	if (writer != null) {
	    writer.write(s);
	} else {
	    write(s, 0, s.length());
	}
    
public voidwriteOut(java.io.Writer out)
Write the contents of this BodyJspWriter into a Writer. Subclasses are likely to do interesting things with the implementation so some things are extra efficient.

param
out The writer into which to place the contents of this body evaluation

	if (writer == null) {
	    out.write(cb, 0, nextChar);
	    // Flush not called as the writer passed could be a BodyContent and
	    // it doesn't allow to flush.
	}