318551 Optional uncheck Printwriter
git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@2063 7e9141cc-0065-0410-87d8-b60c137991c4
This commit is contained in:
parent
088a93b562
commit
f9640c4116
|
@ -11,7 +11,6 @@
|
|||
// You may elect to redistribute this code under either of these licenses.
|
||||
// ========================================================================
|
||||
|
||||
|
||||
package org.eclipse.jetty.io;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
|
@ -28,73 +27,105 @@ import org.eclipse.jetty.util.log.Log;
|
|||
/**
|
||||
* A wrapper for the {@link java.io.PrintWriter} that re-throws the instances of
|
||||
* {@link java.io.IOException} thrown by the underlying implementation of
|
||||
* {@link java.io.Writer} as unchecked {@link RuntimeIOException} instances.
|
||||
* {@link java.io.Writer} as {@link RuntimeIOException} instances.
|
||||
*/
|
||||
public class UncheckedPrintWriter extends PrintWriter
|
||||
{
|
||||
private boolean autoFlush = false;
|
||||
|
||||
{
|
||||
private boolean _autoFlush = false;
|
||||
private boolean _throwUnchecked=true;
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* Line separator string. This is the value of the line.separator
|
||||
* property at the moment that the stream was created.
|
||||
* Line separator string. This is the value of the line.separator property
|
||||
* at the moment that the stream was created.
|
||||
*/
|
||||
private String lineSeparator;
|
||||
|
||||
public UncheckedPrintWriter (Writer out)
|
||||
private String _lineSeparator;
|
||||
|
||||
public UncheckedPrintWriter(Writer out)
|
||||
{
|
||||
this(out, false);
|
||||
this(out,false);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
/**
|
||||
* Create a new PrintWriter.
|
||||
*
|
||||
* @param out A character-output stream
|
||||
* @param autoFlush A boolean; if true, the println() methods will flush
|
||||
* the output buffer
|
||||
*
|
||||
* @param out
|
||||
* A character-output stream
|
||||
* @param autoFlush
|
||||
* A boolean; if true, the println() methods will flush the
|
||||
* output buffer
|
||||
*/
|
||||
public UncheckedPrintWriter(Writer out, boolean autoFlush)
|
||||
{
|
||||
super(out, autoFlush);
|
||||
this.autoFlush = autoFlush;
|
||||
this.lineSeparator = System.getProperty("line.separator");
|
||||
super(out,autoFlush);
|
||||
this._autoFlush = autoFlush;
|
||||
this._lineSeparator = System.getProperty("line.separator");
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* Create a new PrintWriter, without automatic line flushing, from an
|
||||
* existing OutputStream. This convenience constructor creates the
|
||||
* necessary intermediate OutputStreamWriter, which will convert characters
|
||||
* into bytes using the default character encoding.
|
||||
*
|
||||
* @param out An output stream
|
||||
*
|
||||
* existing OutputStream. This convenience constructor creates the necessary
|
||||
* intermediate OutputStreamWriter, which will convert characters into bytes
|
||||
* using the default character encoding.
|
||||
*
|
||||
* @param out
|
||||
* An output stream
|
||||
*
|
||||
* @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
|
||||
*/
|
||||
public UncheckedPrintWriter(OutputStream out)
|
||||
{
|
||||
this(out, false);
|
||||
this(out,false);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* Create a new PrintWriter from an existing OutputStream. This
|
||||
* convenience constructor creates the necessary intermediate
|
||||
* OutputStreamWriter, which will convert characters into bytes using the
|
||||
* default character encoding.
|
||||
*
|
||||
* @param out An output stream
|
||||
* @param autoFlush A boolean; if true, the println() methods will flush
|
||||
* the output buffer
|
||||
*
|
||||
* Create a new PrintWriter from an existing OutputStream. This convenience
|
||||
* constructor creates the necessary intermediate OutputStreamWriter, which
|
||||
* will convert characters into bytes using the default character encoding.
|
||||
*
|
||||
* @param out
|
||||
* An output stream
|
||||
* @param autoFlush
|
||||
* A boolean; if true, the println() methods will flush the
|
||||
* output buffer
|
||||
*
|
||||
* @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
|
||||
*/
|
||||
public UncheckedPrintWriter(OutputStream out, boolean autoFlush)
|
||||
{
|
||||
this(new BufferedWriter(new OutputStreamWriter(out)), autoFlush);
|
||||
this(new BufferedWriter(new OutputStreamWriter(out)),autoFlush);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
private void setError(Throwable th)
|
||||
{
|
||||
setError();
|
||||
if (_throwUnchecked)
|
||||
throw new RuntimeIOException(th);
|
||||
Log.debug(th);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/** Are unchecked exceptions thrown.
|
||||
* @return True if {@link RuntimeIOException}s are thrown
|
||||
*/
|
||||
public boolean isUncheckedPrintWriter()
|
||||
{
|
||||
return _throwUnchecked;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/** Set if unchecked exceptions are thrown
|
||||
* @param uncheckedPrintWriter True if {@link RuntimeIOException}s are to be thrown
|
||||
*/
|
||||
public void setUncheckedPrintWriter(boolean uncheckedPrintWriter)
|
||||
{
|
||||
_throwUnchecked = uncheckedPrintWriter;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/** Check to make sure that the stream has not been closed */
|
||||
private void isOpen() throws IOException
|
||||
|
@ -104,281 +135,326 @@ public class UncheckedPrintWriter extends PrintWriter
|
|||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* Flush the stream.
|
||||
/**
|
||||
* Flush the stream.
|
||||
*/
|
||||
@Override
|
||||
public void flush() {
|
||||
try {
|
||||
synchronized (lock) {
|
||||
public void flush()
|
||||
{
|
||||
try
|
||||
{
|
||||
synchronized (lock)
|
||||
{
|
||||
isOpen();
|
||||
out.flush();
|
||||
}
|
||||
}
|
||||
catch (IOException ex) {
|
||||
Log.debug(ex);
|
||||
setError();
|
||||
throw new RuntimeIOException(ex);
|
||||
catch (IOException ex)
|
||||
{
|
||||
setError(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* Close the stream.
|
||||
/**
|
||||
* Close the stream.
|
||||
*/
|
||||
@Override
|
||||
public void close() {
|
||||
try {
|
||||
synchronized (lock) {
|
||||
public void close()
|
||||
{
|
||||
try
|
||||
{
|
||||
synchronized (lock)
|
||||
{
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
catch (IOException ex) {
|
||||
Log.debug(ex);
|
||||
setError();
|
||||
throw new RuntimeIOException(ex);
|
||||
catch (IOException ex)
|
||||
{
|
||||
setError(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
/**
|
||||
* Write a single character.
|
||||
* @param c int specifying a character to be written.
|
||||
*
|
||||
* @param c
|
||||
* int specifying a character to be written.
|
||||
*/
|
||||
@Override
|
||||
public void write(int c) {
|
||||
try {
|
||||
synchronized (lock) {
|
||||
public void write(int c)
|
||||
{
|
||||
try
|
||||
{
|
||||
synchronized (lock)
|
||||
{
|
||||
isOpen();
|
||||
out.write(c);
|
||||
}
|
||||
}
|
||||
catch (InterruptedIOException x) {
|
||||
catch (InterruptedIOException x)
|
||||
{
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
catch (IOException ex) {
|
||||
Log.debug(ex);
|
||||
setError();
|
||||
throw new RuntimeIOException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* Write a portion of an array of characters.
|
||||
* @param buf Array of characters
|
||||
* @param off Offset from which to start writing characters
|
||||
* @param len Number of characters to write
|
||||
*/
|
||||
@Override
|
||||
public void write(char buf[], int off, int len) {
|
||||
try {
|
||||
synchronized (lock) {
|
||||
isOpen();
|
||||
out.write(buf, off, len);
|
||||
}
|
||||
}
|
||||
catch (InterruptedIOException x) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
catch (IOException ex) {
|
||||
Log.debug(ex);
|
||||
setError();
|
||||
throw new RuntimeIOException(ex);
|
||||
catch (IOException ex)
|
||||
{
|
||||
setError(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* Write an array of characters. This method cannot be inherited from the
|
||||
* Write a portion of an array of characters.
|
||||
*
|
||||
* @param buf
|
||||
* Array of characters
|
||||
* @param off
|
||||
* Offset from which to start writing characters
|
||||
* @param len
|
||||
* Number of characters to write
|
||||
*/
|
||||
@Override
|
||||
public void write(char buf[], int off, int len)
|
||||
{
|
||||
try
|
||||
{
|
||||
synchronized (lock)
|
||||
{
|
||||
isOpen();
|
||||
out.write(buf,off,len);
|
||||
}
|
||||
}
|
||||
catch (InterruptedIOException x)
|
||||
{
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
setError(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* Write an array of characters. This method cannot be inherited from the
|
||||
* Writer class because it must suppress I/O exceptions.
|
||||
* @param buf Array of characters to be written
|
||||
*
|
||||
* @param buf
|
||||
* Array of characters to be written
|
||||
*/
|
||||
@Override
|
||||
public void write(char buf[]) {
|
||||
this.write(buf, 0, buf.length);
|
||||
public void write(char buf[])
|
||||
{
|
||||
this.write(buf,0,buf.length);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* Write a portion of a string.
|
||||
* @param s A String
|
||||
* @param off Offset from which to start writing characters
|
||||
* @param len Number of characters to write
|
||||
/**
|
||||
* Write a portion of a string.
|
||||
*
|
||||
* @param s
|
||||
* A String
|
||||
* @param off
|
||||
* Offset from which to start writing characters
|
||||
* @param len
|
||||
* Number of characters to write
|
||||
*/
|
||||
@Override
|
||||
public void write(String s, int off, int len) {
|
||||
try {
|
||||
synchronized (lock) {
|
||||
public void write(String s, int off, int len)
|
||||
{
|
||||
try
|
||||
{
|
||||
synchronized (lock)
|
||||
{
|
||||
isOpen();
|
||||
out.write(s, off, len);
|
||||
out.write(s,off,len);
|
||||
}
|
||||
}
|
||||
catch (InterruptedIOException x) {
|
||||
catch (InterruptedIOException x)
|
||||
{
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
catch (IOException ex) {
|
||||
Log.debug(ex);
|
||||
setError();
|
||||
throw new RuntimeIOException(ex);
|
||||
catch (IOException ex)
|
||||
{
|
||||
setError(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* Write a string. This method cannot be inherited from the Writer class
|
||||
* Write a string. This method cannot be inherited from the Writer class
|
||||
* because it must suppress I/O exceptions.
|
||||
* @param s String to be written
|
||||
*
|
||||
* @param s
|
||||
* String to be written
|
||||
*/
|
||||
@Override
|
||||
public void write(String s) {
|
||||
this.write(s, 0, s.length());
|
||||
public void write(String s)
|
||||
{
|
||||
this.write(s,0,s.length());
|
||||
}
|
||||
|
||||
private void newLine() {
|
||||
try {
|
||||
synchronized (lock) {
|
||||
private void newLine()
|
||||
{
|
||||
try
|
||||
{
|
||||
synchronized (lock)
|
||||
{
|
||||
isOpen();
|
||||
out.write(lineSeparator);
|
||||
if (autoFlush)
|
||||
out.write(_lineSeparator);
|
||||
if (_autoFlush)
|
||||
out.flush();
|
||||
}
|
||||
}
|
||||
catch (InterruptedIOException x) {
|
||||
catch (InterruptedIOException x)
|
||||
{
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
catch (IOException ex) {
|
||||
Log.debug(ex);
|
||||
setError();
|
||||
throw new RuntimeIOException(ex);
|
||||
catch (IOException ex)
|
||||
{
|
||||
setError(ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Methods that do not terminate lines */
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* Print a boolean value. The string produced by <code>{@link
|
||||
/**
|
||||
* Print a boolean value. The string produced by <code>{@link
|
||||
* java.lang.String#valueOf(boolean)}</code> is translated into bytes
|
||||
* according to the platform's default character encoding, and these bytes
|
||||
* are written in exactly the manner of the <code>{@link
|
||||
* #write(int)}</code> method.
|
||||
*
|
||||
* @param b The <code>boolean</code> to be printed
|
||||
*
|
||||
* @param b
|
||||
* The <code>boolean</code> to be printed
|
||||
*/
|
||||
@Override
|
||||
public void print(boolean b) {
|
||||
this.write(b ? "true" : "false");
|
||||
public void print(boolean b)
|
||||
{
|
||||
this.write(b?"true":"false");
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* Print a character. The character is translated into one or more bytes
|
||||
* 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 <code>{@link
|
||||
* #write(int)}</code> method.
|
||||
*
|
||||
* @param c The <code>char</code> to be printed
|
||||
*
|
||||
* @param c
|
||||
* The <code>char</code> to be printed
|
||||
*/
|
||||
@Override
|
||||
public void print(char c) {
|
||||
public void print(char c)
|
||||
{
|
||||
this.write(c);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* Print an integer. The string produced by <code>{@link
|
||||
* Print an integer. The string produced by <code>{@link
|
||||
* java.lang.String#valueOf(int)}</code> is translated into bytes according
|
||||
* to the platform's default character encoding, and these bytes are
|
||||
* written in exactly the manner of the <code>{@link #write(int)}</code>
|
||||
* method.
|
||||
*
|
||||
* @param i The <code>int</code> to be printed
|
||||
* @see java.lang.Integer#toString(int)
|
||||
* to the platform's default character encoding, and these bytes are written
|
||||
* in exactly the manner of the <code>{@link #write(int)}</code> method.
|
||||
*
|
||||
* @param i
|
||||
* The <code>int</code> to be printed
|
||||
* @see java.lang.Integer#toString(int)
|
||||
*/
|
||||
@Override
|
||||
public void print(int i) {
|
||||
public void print(int i)
|
||||
{
|
||||
this.write(String.valueOf(i));
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* Print a long integer. The string produced by <code>{@link
|
||||
* java.lang.String#valueOf(long)}</code> is translated into bytes
|
||||
* according to the platform's default character encoding, and these bytes
|
||||
* are written in exactly the manner of the <code>{@link #write(int)}</code>
|
||||
* method.
|
||||
*
|
||||
* @param l The <code>long</code> to be printed
|
||||
* @see java.lang.Long#toString(long)
|
||||
* Print a long integer. The string produced by <code>{@link
|
||||
* java.lang.String#valueOf(long)}</code> is translated into bytes according
|
||||
* to the platform's default character encoding, and these bytes are written
|
||||
* in exactly the manner of the <code>{@link #write(int)}</code> method.
|
||||
*
|
||||
* @param l
|
||||
* The <code>long</code> to be printed
|
||||
* @see java.lang.Long#toString(long)
|
||||
*/
|
||||
@Override
|
||||
public void print(long l) {
|
||||
public void print(long l)
|
||||
{
|
||||
this.write(String.valueOf(l));
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* Print a floating-point number. The string produced by <code>{@link
|
||||
* Print a floating-point number. The string produced by <code>{@link
|
||||
* java.lang.String#valueOf(float)}</code> is translated into bytes
|
||||
* according to the platform's default character encoding, and these bytes
|
||||
* are written in exactly the manner of the <code>{@link #write(int)}</code>
|
||||
* method.
|
||||
*
|
||||
* @param f The <code>float</code> to be printed
|
||||
* @see java.lang.Float#toString(float)
|
||||
*
|
||||
* @param f
|
||||
* The <code>float</code> to be printed
|
||||
* @see java.lang.Float#toString(float)
|
||||
*/
|
||||
@Override
|
||||
public void print(float f) {
|
||||
public void print(float f)
|
||||
{
|
||||
this.write(String.valueOf(f));
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* Print a double-precision floating-point number. The string produced by
|
||||
* Print a double-precision floating-point number. The string produced by
|
||||
* <code>{@link java.lang.String#valueOf(double)}</code> is translated into
|
||||
* bytes according to the platform's default character encoding, and these
|
||||
* bytes are written in exactly the manner of the <code>{@link
|
||||
* #write(int)}</code> method.
|
||||
*
|
||||
* @param d The <code>double</code> to be printed
|
||||
* @see java.lang.Double#toString(double)
|
||||
*
|
||||
* @param d
|
||||
* The <code>double</code> to be printed
|
||||
* @see java.lang.Double#toString(double)
|
||||
*/
|
||||
@Override
|
||||
public void print(double d) {
|
||||
public void print(double d)
|
||||
{
|
||||
this.write(String.valueOf(d));
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* Print an array of characters. The characters are converted into bytes
|
||||
* 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 <code>{@link #write(int)}</code>
|
||||
* method.
|
||||
*
|
||||
* @param s The array of chars to be printed
|
||||
*
|
||||
* @throws NullPointerException If <code>s</code> is <code>null</code>
|
||||
*
|
||||
* @param s
|
||||
* The array of chars to be printed
|
||||
*
|
||||
* @throws NullPointerException
|
||||
* If <code>s</code> is <code>null</code>
|
||||
*/
|
||||
@Override
|
||||
public void print(char s[]) {
|
||||
public void print(char s[])
|
||||
{
|
||||
this.write(s);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* Print a string. If the argument is <code>null</code> then the string
|
||||
* <code>"null"</code> is printed. Otherwise, the string's characters are
|
||||
* Print a string. If the argument is <code>null</code> then the string
|
||||
* <code>"null"</code> 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
|
||||
* <code>{@link #write(int)}</code> method.
|
||||
*
|
||||
* @param s The <code>String</code> to be printed
|
||||
*
|
||||
* @param s
|
||||
* The <code>String</code> to be printed
|
||||
*/
|
||||
@Override
|
||||
public void print(String s) {
|
||||
if (s == null) {
|
||||
public void print(String s)
|
||||
{
|
||||
if (s == null)
|
||||
{
|
||||
s = "null";
|
||||
}
|
||||
this.write(s);
|
||||
|
@ -386,46 +462,49 @@ public class UncheckedPrintWriter extends PrintWriter
|
|||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* Print an object. The string produced by the <code>{@link
|
||||
* Print an object. The string produced by the <code>{@link
|
||||
* java.lang.String#valueOf(Object)}</code> method is translated into bytes
|
||||
* according to the platform's default character encoding, and these bytes
|
||||
* are written in exactly the manner of the <code>{@link #write(int)}</code>
|
||||
* method.
|
||||
*
|
||||
* @param obj The <code>Object</code> to be printed
|
||||
* @see java.lang.Object#toString()
|
||||
*
|
||||
* @param obj
|
||||
* The <code>Object</code> to be printed
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public void print(Object obj) {
|
||||
public void print(Object obj)
|
||||
{
|
||||
this.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
|
||||
* Terminate the current line by writing the line separator string. The line
|
||||
* separator string is defined by the system property
|
||||
* <code>line.separator</code>, and is not necessarily a single newline
|
||||
* character (<code>'\n'</code>).
|
||||
*/
|
||||
@Override
|
||||
public void println() {
|
||||
public void println()
|
||||
{
|
||||
this.newLine();
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* Print a boolean value and then terminate the line. This method behaves
|
||||
* as though it invokes <code>{@link #print(boolean)}</code> and then
|
||||
* Print a boolean value and then terminate the line. This method behaves as
|
||||
* though it invokes <code>{@link #print(boolean)}</code> and then
|
||||
* <code>{@link #println()}</code>.
|
||||
*
|
||||
* @param x the <code>boolean</code> value to be printed
|
||||
*
|
||||
* @param x
|
||||
* the <code>boolean</code> value to be printed
|
||||
*/
|
||||
@Override
|
||||
public void println(boolean x) {
|
||||
synchronized (lock) {
|
||||
public void println(boolean x)
|
||||
{
|
||||
synchronized (lock)
|
||||
{
|
||||
this.print(x);
|
||||
this.println();
|
||||
}
|
||||
|
@ -433,15 +512,18 @@ public class UncheckedPrintWriter extends PrintWriter
|
|||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* Print a character and then terminate the line. This method behaves as
|
||||
* Print a character and then terminate the line. This method behaves as
|
||||
* though it invokes <code>{@link #print(char)}</code> and then <code>{@link
|
||||
* #println()}</code>.
|
||||
*
|
||||
* @param x the <code>char</code> value to be printed
|
||||
*
|
||||
* @param x
|
||||
* the <code>char</code> value to be printed
|
||||
*/
|
||||
@Override
|
||||
public void println(char x) {
|
||||
synchronized (lock) {
|
||||
public void println(char x)
|
||||
{
|
||||
synchronized (lock)
|
||||
{
|
||||
this.print(x);
|
||||
this.println();
|
||||
}
|
||||
|
@ -449,15 +531,18 @@ public class UncheckedPrintWriter extends PrintWriter
|
|||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* Print an integer and then terminate the line. This method behaves as
|
||||
* Print an integer and then terminate the line. This method behaves as
|
||||
* though it invokes <code>{@link #print(int)}</code> and then <code>{@link
|
||||
* #println()}</code>.
|
||||
*
|
||||
* @param x the <code>int</code> value to be printed
|
||||
*
|
||||
* @param x
|
||||
* the <code>int</code> value to be printed
|
||||
*/
|
||||
@Override
|
||||
public void println(int x) {
|
||||
synchronized (lock) {
|
||||
public void println(int x)
|
||||
{
|
||||
synchronized (lock)
|
||||
{
|
||||
this.print(x);
|
||||
this.println();
|
||||
}
|
||||
|
@ -465,15 +550,18 @@ public class UncheckedPrintWriter extends PrintWriter
|
|||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* Print a long integer and then terminate the line. This method behaves
|
||||
* as though it invokes <code>{@link #print(long)}</code> and then
|
||||
* Print a long integer and then terminate the line. This method behaves as
|
||||
* though it invokes <code>{@link #print(long)}</code> and then
|
||||
* <code>{@link #println()}</code>.
|
||||
*
|
||||
* @param x the <code>long</code> value to be printed
|
||||
*
|
||||
* @param x
|
||||
* the <code>long</code> value to be printed
|
||||
*/
|
||||
@Override
|
||||
public void println(long x) {
|
||||
synchronized (lock) {
|
||||
public void println(long x)
|
||||
{
|
||||
synchronized (lock)
|
||||
{
|
||||
this.print(x);
|
||||
this.println();
|
||||
}
|
||||
|
@ -481,15 +569,18 @@ public class UncheckedPrintWriter extends PrintWriter
|
|||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* Print a floating-point number and then terminate the line. This method
|
||||
* Print a floating-point number and then terminate the line. This method
|
||||
* behaves as though it invokes <code>{@link #print(float)}</code> and then
|
||||
* <code>{@link #println()}</code>.
|
||||
*
|
||||
* @param x the <code>float</code> value to be printed
|
||||
*
|
||||
* @param x
|
||||
* the <code>float</code> value to be printed
|
||||
*/
|
||||
@Override
|
||||
public void println(float x) {
|
||||
synchronized (lock) {
|
||||
public void println(float x)
|
||||
{
|
||||
synchronized (lock)
|
||||
{
|
||||
this.print(x);
|
||||
this.println();
|
||||
}
|
||||
|
@ -498,15 +589,18 @@ public class UncheckedPrintWriter extends PrintWriter
|
|||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* Print a double-precision floating-point number and then terminate the
|
||||
* line. This method behaves as though it invokes <code>{@link
|
||||
* line. This method behaves as though it invokes <code>{@link
|
||||
* #print(double)}</code> and then <code>{@link #println()}</code>.
|
||||
*
|
||||
* @param x the <code>double</code> value to be printed
|
||||
*
|
||||
* @param x
|
||||
* the <code>double</code> value to be printed
|
||||
*/
|
||||
/* ------------------------------------------------------------ */
|
||||
@Override
|
||||
public void println(double x) {
|
||||
synchronized (lock) {
|
||||
public void println(double x)
|
||||
{
|
||||
synchronized (lock)
|
||||
{
|
||||
this.print(x);
|
||||
this.println();
|
||||
}
|
||||
|
@ -514,15 +608,18 @@ public class UncheckedPrintWriter extends PrintWriter
|
|||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* Print an array of characters and then terminate the line. This method
|
||||
* Print an array of characters and then terminate the line. This method
|
||||
* behaves as though it invokes <code>{@link #print(char[])}</code> and then
|
||||
* <code>{@link #println()}</code>.
|
||||
*
|
||||
* @param x the array of <code>char</code> values to be printed
|
||||
*
|
||||
* @param x
|
||||
* the array of <code>char</code> values to be printed
|
||||
*/
|
||||
@Override
|
||||
public void println(char x[]) {
|
||||
synchronized (lock) {
|
||||
public void println(char x[])
|
||||
{
|
||||
synchronized (lock)
|
||||
{
|
||||
this.print(x);
|
||||
this.println();
|
||||
}
|
||||
|
@ -530,15 +627,18 @@ public class UncheckedPrintWriter extends PrintWriter
|
|||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* Print a String and then terminate the line. This method behaves as
|
||||
* though it invokes <code>{@link #print(String)}</code> and then
|
||||
* Print a String and then terminate the line. This method behaves as though
|
||||
* it invokes <code>{@link #print(String)}</code> and then
|
||||
* <code>{@link #println()}</code>.
|
||||
*
|
||||
* @param x the <code>String</code> value to be printed
|
||||
*
|
||||
* @param x
|
||||
* the <code>String</code> value to be printed
|
||||
*/
|
||||
@Override
|
||||
public void println(String x) {
|
||||
synchronized (lock) {
|
||||
public void println(String x)
|
||||
{
|
||||
synchronized (lock)
|
||||
{
|
||||
this.print(x);
|
||||
this.println();
|
||||
}
|
||||
|
@ -546,15 +646,18 @@ public class UncheckedPrintWriter extends PrintWriter
|
|||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* Print an Object and then terminate the line. This method behaves as
|
||||
* Print an Object and then terminate the line. This method behaves as
|
||||
* though it invokes <code>{@link #print(Object)}</code> and then
|
||||
* <code>{@link #println()}</code>.
|
||||
*
|
||||
* @param x the <code>Object</code> value to be printed
|
||||
*
|
||||
* @param x
|
||||
* the <code>Object</code> value to be printed
|
||||
*/
|
||||
@Override
|
||||
public void println(Object x) {
|
||||
synchronized (lock) {
|
||||
public void println(Object x)
|
||||
{
|
||||
synchronized (lock)
|
||||
{
|
||||
this.print(x);
|
||||
this.println();
|
||||
}
|
||||
|
|
|
@ -122,7 +122,6 @@ public class HttpConnection implements Connection
|
|||
private boolean _head = false;
|
||||
private boolean _host = false;
|
||||
private boolean _delayedHandling=false;
|
||||
private boolean _uncheckedPrintWriter=false;
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
public static HttpConnection getCurrentConnection()
|
||||
|
@ -173,25 +172,6 @@ public class HttpConnection implements Connection
|
|||
_server = server;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/** Get the uncheckedPrintWriter.
|
||||
* @return True if {@link UncheckedPrintWriter}s are used
|
||||
*/
|
||||
public boolean isUncheckedPrintWriter()
|
||||
{
|
||||
return _uncheckedPrintWriter;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/** Set the uncheckedPrintWriter.
|
||||
* this is reset to false by {@link #reset(boolean)}.
|
||||
* @param uncheckedPrintWriter True if {@link UncheckedPrintWriter}s are to be used
|
||||
*/
|
||||
public void setUncheckedPrintWriter(boolean uncheckedPrintWriter)
|
||||
{
|
||||
_uncheckedPrintWriter = uncheckedPrintWriter;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* @return the parser used by this connection
|
||||
|
@ -386,8 +366,7 @@ public class HttpConnection implements Connection
|
|||
if (_writer==null)
|
||||
{
|
||||
_writer=new OutputWriter();
|
||||
|
||||
_printWriter=_uncheckedPrintWriter?new UncheckedPrintWriter(_writer):new PrintWriter(_writer);
|
||||
_printWriter=new UncheckedPrintWriter(_writer);
|
||||
}
|
||||
_writer.setCharacterEncoding(encoding);
|
||||
return _printWriter;
|
||||
|
@ -563,7 +542,7 @@ public class HttpConnection implements Connection
|
|||
_generator.reset(returnBuffers); // TODO maybe only release when low on resources
|
||||
_responseFields.clear();
|
||||
_response.recycle();
|
||||
_uncheckedPrintWriter=false;
|
||||
|
||||
_uri.clear();
|
||||
}
|
||||
|
||||
|
|
|
@ -46,7 +46,6 @@ import javax.servlet.http.HttpServletResponse;
|
|||
import org.eclipse.jetty.http.HttpException;
|
||||
import org.eclipse.jetty.http.MimeTypes;
|
||||
import org.eclipse.jetty.io.Buffer;
|
||||
import org.eclipse.jetty.io.UncheckedPrintWriter;
|
||||
import org.eclipse.jetty.server.Dispatcher;
|
||||
import org.eclipse.jetty.server.DispatcherType;
|
||||
import org.eclipse.jetty.server.Handler;
|
||||
|
@ -132,7 +131,6 @@ public class ContextHandler extends ScopedHandler implements Attributes, Server.
|
|||
private boolean _shutdown=false;
|
||||
private boolean _available=true;
|
||||
private volatile int _availability; // 0=STOPPED, 1=AVAILABLE, 2=SHUTDOWN, 3=UNAVAILABLE
|
||||
private boolean _uncheckedPrintWriter=false;
|
||||
|
||||
private final static int __STOPPED=0,__AVAILABLE=1,__SHUTDOWN=2,__UNAVAILABLE=3;
|
||||
|
||||
|
@ -211,24 +209,6 @@ public class ContextHandler extends ScopedHandler implements Attributes, Server.
|
|||
_allowNullPathInfo=allowNullPathInfo;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/** Get the uncheckedPrintWriter.
|
||||
* @return True if {@link UncheckedPrintWriter}s are used by requests in this context.
|
||||
*/
|
||||
public boolean isUncheckedPrintWriter()
|
||||
{
|
||||
return _uncheckedPrintWriter;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/** Set the uncheckedPrintWriter.
|
||||
* @param uncheckedPrintWriter True if {@link UncheckedPrintWriter}s are to be used in this context.
|
||||
*/
|
||||
public void setUncheckedPrintWriter(boolean uncheckedPrintWriter)
|
||||
{
|
||||
_uncheckedPrintWriter = uncheckedPrintWriter;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
@Override
|
||||
public void setServer(Server server)
|
||||
|
@ -826,9 +806,6 @@ public class ContextHandler extends ScopedHandler implements Attributes, Server.
|
|||
// Are we already in this context?
|
||||
if (old_context!=_scontext)
|
||||
{
|
||||
// unchecked print writers
|
||||
baseRequest.getConnection().setUncheckedPrintWriter(_uncheckedPrintWriter);
|
||||
|
||||
// check the target.
|
||||
if (DispatcherType.REQUEST.equals(dispatch) || DispatcherType.ASYNC.equals(dispatch))
|
||||
{
|
||||
|
|
|
@ -238,22 +238,12 @@ public class ContextHandlerTest
|
|||
{
|
||||
server.start();
|
||||
|
||||
context.setUncheckedPrintWriter(false);
|
||||
String response = connector.getResponses("GET / HTTP/1.1\n" + "Host: www.example.com.\n\n");
|
||||
|
||||
Assert.assertTrue(response.indexOf("Goodbye")>0);
|
||||
Assert.assertTrue(response.indexOf("dead")<0);
|
||||
Assert.assertTrue(handler.error);
|
||||
Assert.assertTrue(handler.throwable==null);
|
||||
|
||||
context.setUncheckedPrintWriter(true);
|
||||
Assert.assertTrue(response.indexOf("Goodbye")>0);
|
||||
response = connector.getResponses("GET / HTTP/1.1\n" + "Host: www.example.com.\n\n");
|
||||
|
||||
Assert.assertTrue(response.indexOf("Goodbye")>0);
|
||||
Assert.assertTrue(response.indexOf("dead")<0);
|
||||
Assert.assertFalse(handler.error);
|
||||
Assert.assertFalse(handler.throwable==null);
|
||||
Assert.assertTrue(handler.throwable!=null);
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
@ -312,23 +302,23 @@ public class ContextHandlerTest
|
|||
baseRequest.setHandled(true);
|
||||
error = false;
|
||||
throwable=null;
|
||||
|
||||
|
||||
response.setStatus(200);
|
||||
response.setContentType("text/plain; charset=utf-8");
|
||||
response.setHeader("Connection","close");
|
||||
PrintWriter writer = response.getWriter();
|
||||
try
|
||||
{
|
||||
response.setStatus(200);
|
||||
response.setContentType("text/plain; charset=utf-8");
|
||||
response.setHeader("Connection","close");
|
||||
PrintWriter writer = response.getWriter();
|
||||
writer.write("Goodbye cruel world\n");
|
||||
writer.close();
|
||||
response.flushBuffer();
|
||||
writer.write("speaking from the dead");
|
||||
error=writer.checkError();
|
||||
}
|
||||
catch(Throwable th)
|
||||
{
|
||||
throwable=th;
|
||||
}
|
||||
error=writer.checkError();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue