Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.apache.jasper.runtime;
 
 import static org.jboss.web.JasperMessages.MESSAGES;
 
 import java.io.Writer;
 
 
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. This needs revisiting when the buffering problems in the JSP spec are fixed -akv

Author(s):
Anil K. Vijendran
 
 public class JspWriterImpl extends JspWriter {
     
     private Writer out;
     private ServletResponse response;    
     private char cb[];
     private int nextChar;
     private boolean flushed = false;
     private boolean closed = false;
     
     public JspWriterImpl() {
         super.true );
     }
    
    
Create a buffered character-output stream that uses a default-sized output buffer.

Parameters:
response A Servlet Response
 
     public JspWriterImpl(ServletResponse response) {
         this(response.true);
     }
    
    
Create a new buffered character-output stream that uses an output buffer of the given size.

Parameters:
response A Servlet Response
sz Output-buffer size, a positive integer
Throws:
java.lang.IllegalArgumentException If sz is <= 0
 
     public JspWriterImpl(ServletResponse responseint sz
             boolean autoFlush) {
         super(szautoFlush);
         if (sz < 0)
             throw .invalidNegativeBufferSize();
         this. = response;
          = sz == 0 ? null : new char[sz];
          = 0;
     }
     
     void initServletResponse responseint szboolean autoFlush ) {
         this.response;
         ifsz > 0 && (  == null || sz > . ) )
             =new char[sz];
          = 0;
         this.=autoFlush;
         this.=sz;
     }
    
    
Package-level access
 
     void recycle() {
          = false;
          = false;
          = null;
         = 0;
         = null;
    }
    
    
Flush the output buffer to the underlying character stream, without flushing the stream itself. This method is non-private only so that it may be invoked by PrintStream.
    protected final void flushBuffer() throws IOException {
        if ( == 0)
            return;
         = true;
        ensureOpen();
        if ( == 0)
            return;
        initOut();
        .write(, 0, );
         = 0;
    }
    
    private void initOut() throws IOException {
        if ( == null) {
             = .getWriter();
        }
    }
    
    
Discard the output buffer.
    public final void clear() throws IOException {
        if (( == 0) && ( != null))
            // clear() is illegal after any unbuffered output (JSP.5.5)
            throw .cannotClearWithNoBuffer();
        if ()
            throw .cannotClearAfterFlush();
        ensureOpen();
         = 0;
    }
    
    public void clearBuffer() throws IOException {
        if ( == 0)
            throw .cannotClearWithNoBuffer();
        ensureOpen();
         = 0;
    }
    
    private final void bufferOverflow() throws IOException {
        throw .bufferOverflow();
    }
    
    
Flush the stream.
    public void flush()  throws IOException {
        flushBuffer();
        if ( != null) {
            .flush();
        }
    }
    
    
Close the stream.
    public void close() throws IOException {
        if ( == null || )
            // multiple calls to close is OK
            return;
        flush();
        if ( != null)
            .close();
         = null;
         = true;
    }
    
    

Returns:
the number of bytes unused in the buffer
    public int getRemaining() {
        return  - ;
    }
    
    
check to make sure that the stream has not been closed
    private void ensureOpen() throws IOException {
        if ( == null || )
            throw new IOException(.streamClosed());
    }
    
    
    
Write a single character.
    public void write(int cthrows IOException {
        ensureOpen();
        if ( == 0) {
            initOut();
            .write(c);
        }
        else {
            if ( >= )
                if ()
                    flushBuffer();
                else
                    bufferOverflow();
            [++] = (charc;
        }
    }
    
    
Our own little min method, to avoid loading java.lang.Math if we've run out of file descriptors and we're trying to print a stack trace.
    private int min(int aint b) {
        if (a < breturn a;
        return b;
    }
    
    
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.

Parameters:
cbuf A character array
off Offset from which to start reading characters
len Number of characters to write
    public void write(char cbuf[], int offint len
    throws IOException 
    {
        ensureOpen();
        
        if ( == 0) {
            initOut();
            .write(cbufofflen);
            return;
        }
        
        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 >= ) {
            /* If the request length exceeds the size of the output buffer,
             flush the buffer and then write the data directly.  In this
             way buffered streams will cascade harmlessly. */
            if ()
                flushBuffer();
            else
                bufferOverflow();
            initOut();
            .write(cbufofflen);
            return;
        }
        
        int b = offt = off + len;
        while (b < t) {
            int d = min( - t - b);
            System.arraycopy(cbufbd);
            b += d;
             += d;
            if ( >= 
                if ()
                    flushBuffer();
                else
                    bufferOverflow();
        }
        
    }
    
    
Write an array of characters. This method cannot be inherited from the Writer class because it must suppress I/O exceptions.
    public void write(char buf[]) throws IOException {
        write(buf, 0, buf.length);
    }
    
    
Write a portion of a String.

Parameters:
s String to be written
off Offset from which to start reading characters
len Number of characters to be written
    public void write(String sint offint lenthrows IOException {
        ensureOpen();
        if ( == 0) {
            initOut();
            .write(sofflen);
            return;
        }
        int b = offt = off + len;
        while (b < t) {
            int d = min( - t - b);
            s.getChars(bb + d);
            b += d;
             += d;
            if ( >= 
                if ()
                    flushBuffer();
                else
                    bufferOverflow();
        }
    }
    
    
    static String lineSeparator = System.getProperty("line.separator");
    
    
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:
java.io.IOException If an I/O error occurs
    
    public void newLine() throws IOException {
        write();
    }
    
    
    /* Methods that do not terminate lines */
    
    
Print a boolean value. The string produced by 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 write(int) method.

Parameters:
b The boolean to be printed
    public void print(boolean bthrows IOException {
        write(b ? "true" : "false");
    }
    
    
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 write(int) method.

Parameters:
c The char to be printed
    public void print(char cthrows IOException {
        write(String.valueOf(c));
    }
    
    
Print an integer. The string produced by 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 write(int) method.

Parameters:
i The int to be printed
    public void print(int ithrows IOException {
        write(String.valueOf(i));
    }
    
    
Print a long integer. The string produced by 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 write(int) method.

Parameters:
l The long to be printed
    public void print(long lthrows IOException {
        write(String.valueOf(l));
    }
    
    
Print a floating-point number. The string produced by 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 write(int) method.

Parameters:
f The float to be printed
    public void print(float fthrows IOException {
        write(String.valueOf(f));
    }
    
    
Print a double-precision floating-point number. The string produced by 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 write(int) method.

Parameters:
d The double to be printed
    public void print(double dthrows IOException {
        write(String.valueOf(d));
    }
    
    
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 write(int) method.

Parameters:
s The array of chars to be printed
Throws:
java.lang.NullPointerException If s is null
    public void print(char s[]) throws IOException {
        write(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 write(int) method.

Parameters:
s The String to be printed
    public void print(String sthrows IOException {
        if (s == null) {
            s = "null";
        }
        write(s);
    }
    
    
Print an object. The string produced by the java.lang.String.valueOf(java.lang.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 write(int) method.

Parameters:
obj The Object to be printed
    public void print(Object objthrows IOException {
        write(String.valueOf(obj));
    }
    
    /* Methods that do terminate lines */
    
    
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'). Need to change this from PrintWriter because the default println() writes to the sink directly instead of through the write method...
    public void println() throws IOException {
        newLine();
    }
    
    
Print a boolean value and then terminate the line. This method behaves as though it invokes print(boolean) and then println().
    public void println(boolean xthrows IOException {
        print(x);
        println();
    }
    
    
Print a character and then terminate the line. This method behaves as though it invokes print(char) and then println().
    public void println(char xthrows IOException {
        print(x);
        println();
    }
    
    
Print an integer and then terminate the line. This method behaves as though it invokes print(int) and then println().
    public void println(int xthrows IOException {
        print(x);
        println();
    }
    
    
Print a long integer and then terminate the line. This method behaves as though it invokes print(long) and then println().
    public void println(long xthrows IOException {
        print(x);
        println();
    }
    
    
Print a floating-point number and then terminate the line. This method behaves as though it invokes print(float) and then println().
    public void println(float xthrows IOException {
        print(x);
        println();
    }
    
    
Print a double-precision floating-point number and then terminate the line. This method behaves as though it invokes print(double) and then println().
    public void println(double xthrows IOException {
        print(x);
        println();
    }
    
    
Print an array of characters and then terminate the line. This method behaves as though it invokes print(char[]) and then println().
    public void println(char x[]) throws IOException {
        print(x);
        println();
    }
    
    
Print a String and then terminate the line. This method behaves as though it invokes print(java.lang.String) and then println().
    public void println(String xthrows IOException {
        print(x);
        println();
    }
    
    
Print an Object and then terminate the line. This method behaves as though it invokes print(java.lang.Object) and then println().
    public void println(Object xthrows IOException {
        print(x);
        println();
    }
    
New to GrepCode? Check out our FAQ X