org.apache.sling.engine.impl.output
Class BufferedPrintWriter

java.lang.Object
  extended by java.io.Writer
      extended by java.io.PrintWriter
          extended by org.apache.sling.engine.impl.output.BufferedPrintWriter
All Implemented Interfaces:
Closeable, Flushable, Appendable, Buffer

public class BufferedPrintWriter
extends PrintWriter
implements Buffer

The BufferedPrintWriter implements buffering for the PrintWriter returned by the ServletResponse.getWriter() method.

We need this additional buffering class for the ServletResponse.getWriter() class because we wrap the original PrintWriter retrieved from the servlet container with optional caching and link checking writers.

This class overwrites all the base class's write methods, which are used as the base for writing. All other methods of the base class use the write methods after having formatted the parameter.

The buffer is flushed to the wrapped writer in the following cases :

  1. The buffer is full if more characters have to be added. That is the buffer is not flushed when the buffer gets full by adding characters but when the buffer would get 'overfull' with adding characters.
  2. When the flush() method is called.
  3. When the writer is closed

This class is not multithread safe as it is intended to be used on single requests which are assigned to single threads.

This extension to the PrintWriter class does not support automatic flush of the output buffer. That is, the println method never ever flushes output.


Field Summary
protected  boolean closed
          flag to indicate that the writer has been closed
protected  boolean loggedNoBuffering
           
protected  boolean trouble
           
 
Fields inherited from class java.io.PrintWriter
out
 
Fields inherited from class java.io.Writer
lock
 
Constructor Summary
BufferedPrintWriter(PrintWriter servletWriter, int bufferSize)
          Creates an instance wrapping the PrintWriter and providing an initial buffer size.
 
Method Summary
 boolean checkError()
          Flush the stream if it's not closed and check its error state.
 void close()
          Closes the output after flushing the buffer contents to the wrapped writer.
 void flush()
          Writes the contents of the buffer to the wrapped writer and flushes the latter.
 void flushBuffer()
          Writes the contents of the buffer to the wrapped writer.
 int getBufferSize()
          Returns the size of the buffer
 void println()
          Terminate the current line by writing the line separator string without ever flushing the buffer.
 void resetBuffer()
          Clears the buffer.
 void setBufferSize(int bufferSize)
          Sets the buffer size.
protected  void setError()
          Indicate that an error has occurred.
 void write(char[] buf, int off, int len)
          Writes a portion of an array of characters.
 void write(int c)
          Writes a single character.
 void write(String s, int off, int len)
          Writes a portion of a string.
 
Methods inherited from class java.io.PrintWriter
append, append, append, clearError, format, format, print, print, print, print, print, print, print, print, print, printf, printf, println, println, println, println, println, println, println, println, println, write, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

trouble

protected boolean trouble
See Also:
checkError(), setError()

closed

protected boolean closed
flag to indicate that the writer has been closed


loggedNoBuffering

protected boolean loggedNoBuffering
Constructor Detail

BufferedPrintWriter

public BufferedPrintWriter(PrintWriter servletWriter,
                           int bufferSize)
Creates an instance wrapping the PrintWriter and providing an initial buffer size.

The servletWriter is to initially write data to.

The other use of the servletWriter is to check for any errors occurred by forwarding the call to the checkError() to this writer.

Parameters:
servletWriter - The PrintWriter to which data is initially written.
bufferSize - The initial size of the buffer. If negative or zero no buffering is initiallly done.
Method Detail

setBufferSize

public void setBufferSize(int bufferSize)
Sets the buffer size. If the buffer contains data, this method throws an IllegalStateException.

Specified by:
setBufferSize in interface Buffer
Parameters:
bufferSize - The new size of the buffer. If this value is less than or equal to zero, buffering is disabled alltogether.
Throws:
IllegalStateException - if the buffer contains data.

getBufferSize

public int getBufferSize()
Returns the size of the buffer

Specified by:
getBufferSize in interface Buffer
Returns:
the size of the buffer

resetBuffer

public void resetBuffer()
Clears the buffer.

Specified by:
resetBuffer in interface Buffer

flushBuffer

public void flushBuffer()
Writes the contents of the buffer to the wrapped writer.

Specified by:
flushBuffer in interface Buffer

checkError

public boolean checkError()
Flush the stream if it's not closed and check its error state. Errors are cumulative; once the stream encounters an error, this routine will return true on all successive calls.

This implementation checks with the PrintWriter used to create this BufferedPrintWriter.

Overrides:
checkError in class PrintWriter
Returns:
true if the print stream has encountered an error, either on the underlying output stream or during a format conversion.

setError

protected void setError()
Indicate that an error has occurred.

Overrides:
setError in class PrintWriter

flush

public void flush()
Writes the contents of the buffer to the wrapped writer and flushes the latter.

Specified by:
flush in interface Flushable
Overrides:
flush in class PrintWriter

close

public void close()
Closes the output after flushing the buffer contents to the wrapped writer.

Specified by:
close in interface Closeable
Overrides:
close in class PrintWriter

write

public void write(int c)
Writes a single character. If the buffer is already full, it is flushed to the wrapped writer before writing the character to the buffer.

Overrides:
write in class PrintWriter
Parameters:
c - The character to write to the buffer.

write

public void write(char[] buf,
                  int off,
                  int len)
Writes a portion of an array of characters.

If the number of characters is more than the buffer capacity, the buffer is flushed. Due to buffering it is not guaranteed that the complete number of characters is either buffered or written. It is acceptable for part of the characters to be written and the rest to be buffered.

Overrides:
write in class PrintWriter
Parameters:
buf - The characters to write
off - The starting offset into the character array
len - The number of characters to write starting with the offset.
Throws:
NullPointerException - if buf is null
IndexOutOfBoundsException - if either or both off and off+len are outside of the bounds of the character array buf.

write

public void write(String s,
                  int off,
                  int len)
Writes a portion of a string.

If the number of characters is more than the buffer capacity, the buffer is flushed. Due to buffering it is not guaranteed that the complete number of characters is either buffered or written. It is acceptable for part of the characters to be written and the rest to be buffered.

Overrides:
write in class PrintWriter
Parameters:
s - The string to write
off - The starting offset into the string
len - The number of characters to write starting with the offset.
Throws:
NullPointerException - if s is null
IndexOutOfBoundsException - if either or both off and off+len are outside of the bounds of the string s.

println

public void println()
Terminate the current line by writing the line separator string without ever flushing the buffer. The line separator string is defined by the system property line.separator, and is not necessarily a single newline character ('\n').

This duplicates the functionality of the private of the PrintWriter.newLine method.

Overrides:
println in class PrintWriter


Copyright © 2007-2013 The Apache Software Foundation. All Rights Reserved.