2 // ========================================================================
3 // Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.io;
21 import java.io.BufferedWriter;
22 import java.io.IOException;
23 import java.io.InterruptedIOException;
24 import java.io.OutputStream;
25 import java.io.OutputStreamWriter;
26 import java.io.PrintWriter;
27 import java.io.Writer;
29 import org.eclipse.jetty.util.log.Log;
30 import org.eclipse.jetty.util.log.Logger;
32 /* ------------------------------------------------------------ */
34 * A wrapper for the {@link java.io.PrintWriter} that re-throws the instances of
35 * {@link java.io.IOException} thrown by the underlying implementation of
36 * {@link java.io.Writer} as {@link RuntimeIOException} instances.
38 public class UncheckedPrintWriter extends PrintWriter
40 private static final Logger LOG = Log.getLogger(UncheckedPrintWriter.class);
42 private boolean _autoFlush = false;
43 private IOException _ioException;
44 private boolean _isClosed = false;
46 /* ------------------------------------------------------------ */
48 * Line separator string. This is the value of the line.separator property
49 * at the moment that the stream was created.
51 private String _lineSeparator;
53 public UncheckedPrintWriter(Writer out)
58 /* ------------------------------------------------------------ */
60 * Create a new PrintWriter.
63 * A character-output stream
65 * A boolean; if true, the println() methods will flush the
68 public UncheckedPrintWriter(Writer out, boolean autoFlush)
71 this._autoFlush = autoFlush;
72 this._lineSeparator = System.getProperty("line.separator");
75 /* ------------------------------------------------------------ */
77 * Create a new PrintWriter, without automatic line flushing, from an
78 * existing OutputStream. This convenience constructor creates the necessary
79 * intermediate OutputStreamWriter, which will convert characters into bytes
80 * using the default character encoding.
85 * @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
87 public UncheckedPrintWriter(OutputStream out)
92 /* ------------------------------------------------------------ */
94 * Create a new PrintWriter from an existing OutputStream. This convenience
95 * constructor creates the necessary intermediate OutputStreamWriter, which
96 * will convert characters into bytes using the default character encoding.
101 * A boolean; if true, the println() methods will flush the
104 * @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
106 public UncheckedPrintWriter(OutputStream out, boolean autoFlush)
108 this(new BufferedWriter(new OutputStreamWriter(out)),autoFlush);
112 /* ------------------------------------------------------------ */
113 public boolean checkError()
115 return _ioException!=null || super.checkError();
118 /* ------------------------------------------------------------ */
119 private void setError(Throwable th)
124 if (th instanceof IOException)
125 _ioException=(IOException)th;
128 _ioException=new IOException(String.valueOf(th));
129 _ioException.initCause(th);
132 if (LOG.isDebugEnabled())
138 protected void setError()
140 setError(new IOException());
143 /* ------------------------------------------------------------ */
144 /** Check to make sure that the stream has not been closed */
145 private void isOpen() throws IOException
147 if (_ioException!=null)
148 throw new RuntimeIOException(_ioException);
151 throw new IOException("Stream closed");
154 /* ------------------------------------------------------------ */
169 catch (IOException ex)
175 /* ------------------------------------------------------------ */
190 catch (IOException ex)
196 /* ------------------------------------------------------------ */
198 * Write a single character.
201 * int specifying a character to be written.
204 public void write(int c)
214 catch (InterruptedIOException x)
216 Thread.currentThread().interrupt();
218 catch (IOException ex)
224 /* ------------------------------------------------------------ */
226 * Write a portion of an array of characters.
229 * Array of characters
231 * Offset from which to start writing characters
233 * Number of characters to write
236 public void write(char buf[], int off, int len)
243 out.write(buf,off,len);
246 catch (InterruptedIOException x)
248 Thread.currentThread().interrupt();
250 catch (IOException ex)
256 /* ------------------------------------------------------------ */
258 * Write an array of characters. This method cannot be inherited from the
259 * Writer class because it must suppress I/O exceptions.
262 * Array of characters to be written
265 public void write(char buf[])
267 this.write(buf,0,buf.length);
270 /* ------------------------------------------------------------ */
272 * Write a portion of a string.
277 * Offset from which to start writing characters
279 * Number of characters to write
282 public void write(String s, int off, int len)
289 out.write(s,off,len);
292 catch (InterruptedIOException x)
294 Thread.currentThread().interrupt();
296 catch (IOException ex)
302 /* ------------------------------------------------------------ */
304 * Write a string. This method cannot be inherited from the Writer class
305 * because it must suppress I/O exceptions.
308 * String to be written
311 public void write(String s)
313 this.write(s,0,s.length());
316 private void newLine()
323 out.write(_lineSeparator);
328 catch (InterruptedIOException x)
330 Thread.currentThread().interrupt();
332 catch (IOException ex)
338 /* ------------------------------------------------------------ */
340 * Print a boolean value. The string produced by <code>{@link
341 * java.lang.String#valueOf(boolean)}</code> is translated into bytes
342 * according to the platform's default character encoding, and these bytes
343 * are written in exactly the manner of the <code>{@link
344 * #write(int)}</code> method.
347 * The <code>boolean</code> to be printed
350 public void print(boolean b)
352 this.write(b?"true":"false");
355 /* ------------------------------------------------------------ */
357 * Print a character. The character is translated into one or more bytes
358 * according to the platform's default character encoding, and these bytes
359 * are written in exactly the manner of the <code>{@link
360 * #write(int)}</code> method.
363 * The <code>char</code> to be printed
366 public void print(char c)
371 /* ------------------------------------------------------------ */
373 * Print an integer. The string produced by <code>{@link
374 * java.lang.String#valueOf(int)}</code> is translated into bytes according
375 * to the platform's default character encoding, and these bytes are written
376 * in exactly the manner of the <code>{@link #write(int)}</code> method.
379 * The <code>int</code> to be printed
380 * @see java.lang.Integer#toString(int)
383 public void print(int i)
385 this.write(String.valueOf(i));
388 /* ------------------------------------------------------------ */
390 * Print a long integer. The string produced by <code>{@link
391 * java.lang.String#valueOf(long)}</code> is translated into bytes according
392 * to the platform's default character encoding, and these bytes are written
393 * in exactly the manner of the <code>{@link #write(int)}</code> method.
396 * The <code>long</code> to be printed
397 * @see java.lang.Long#toString(long)
400 public void print(long l)
402 this.write(String.valueOf(l));
405 /* ------------------------------------------------------------ */
407 * Print a floating-point number. The string produced by <code>{@link
408 * java.lang.String#valueOf(float)}</code> is translated into bytes
409 * according to the platform's default character encoding, and these bytes
410 * are written in exactly the manner of the <code>{@link #write(int)}</code>
414 * The <code>float</code> to be printed
415 * @see java.lang.Float#toString(float)
418 public void print(float f)
420 this.write(String.valueOf(f));
423 /* ------------------------------------------------------------ */
425 * Print a double-precision floating-point number. The string produced by
426 * <code>{@link java.lang.String#valueOf(double)}</code> is translated into
427 * bytes according to the platform's default character encoding, and these
428 * bytes are written in exactly the manner of the <code>{@link
429 * #write(int)}</code> method.
432 * The <code>double</code> to be printed
433 * @see java.lang.Double#toString(double)
436 public void print(double d)
438 this.write(String.valueOf(d));
441 /* ------------------------------------------------------------ */
443 * Print an array of characters. The characters are converted into bytes
444 * according to the platform's default character encoding, and these bytes
445 * are written in exactly the manner of the <code>{@link #write(int)}</code>
449 * The array of chars to be printed
451 * @throws NullPointerException
452 * If <code>s</code> is <code>null</code>
455 public void print(char s[])
460 /* ------------------------------------------------------------ */
462 * Print a string. If the argument is <code>null</code> then the string
463 * <code>"null"</code> is printed. Otherwise, the string's characters are
464 * converted into bytes according to the platform's default character
465 * encoding, and these bytes are written in exactly the manner of the
466 * <code>{@link #write(int)}</code> method.
469 * The <code>String</code> to be printed
472 public void print(String s)
481 /* ------------------------------------------------------------ */
483 * Print an object. The string produced by the <code>{@link
484 * java.lang.String#valueOf(Object)}</code> method is translated into bytes
485 * according to the platform's default character encoding, and these bytes
486 * are written in exactly the manner of the <code>{@link #write(int)}</code>
490 * The <code>Object</code> to be printed
491 * @see java.lang.Object#toString()
494 public void print(Object obj)
496 this.write(String.valueOf(obj));
499 /* ------------------------------------------------------------ */
501 * Terminate the current line by writing the line separator string. The line
502 * separator string is defined by the system property
503 * <code>line.separator</code>, and is not necessarily a single newline
504 * character (<code>'\n'</code>).
507 public void println()
512 /* ------------------------------------------------------------ */
514 * Print a boolean value and then terminate the line. This method behaves as
515 * though it invokes <code>{@link #print(boolean)}</code> and then
516 * <code>{@link #println()}</code>.
519 * the <code>boolean</code> value to be printed
522 public void println(boolean x)
531 /* ------------------------------------------------------------ */
533 * Print a character and then terminate the line. This method behaves as
534 * though it invokes <code>{@link #print(char)}</code> and then <code>{@link
535 * #println()}</code>.
538 * the <code>char</code> value to be printed
541 public void println(char x)
550 /* ------------------------------------------------------------ */
552 * Print an integer and then terminate the line. This method behaves as
553 * though it invokes <code>{@link #print(int)}</code> and then <code>{@link
554 * #println()}</code>.
557 * the <code>int</code> value to be printed
560 public void println(int x)
569 /* ------------------------------------------------------------ */
571 * Print a long integer and then terminate the line. This method behaves as
572 * though it invokes <code>{@link #print(long)}</code> and then
573 * <code>{@link #println()}</code>.
576 * the <code>long</code> value to be printed
579 public void println(long x)
588 /* ------------------------------------------------------------ */
590 * Print a floating-point number and then terminate the line. This method
591 * behaves as though it invokes <code>{@link #print(float)}</code> and then
592 * <code>{@link #println()}</code>.
595 * the <code>float</code> value to be printed
598 public void println(float x)
607 /* ------------------------------------------------------------ */
609 * Print a double-precision floating-point number and then terminate the
610 * line. This method behaves as though it invokes <code>{@link
611 * #print(double)}</code> and then <code>{@link #println()}</code>.
614 * the <code>double</code> value to be printed
616 /* ------------------------------------------------------------ */
618 public void println(double x)
627 /* ------------------------------------------------------------ */
629 * Print an array of characters and then terminate the line. This method
630 * behaves as though it invokes <code>{@link #print(char[])}</code> and then
631 * <code>{@link #println()}</code>.
634 * the array of <code>char</code> values to be printed
637 public void println(char x[])
646 /* ------------------------------------------------------------ */
648 * Print a String and then terminate the line. This method behaves as though
649 * it invokes <code>{@link #print(String)}</code> and then
650 * <code>{@link #println()}</code>.
653 * the <code>String</code> value to be printed
656 public void println(String x)
665 /* ------------------------------------------------------------ */
667 * Print an Object and then terminate the line. This method behaves as
668 * though it invokes <code>{@link #print(Object)}</code> and then
669 * <code>{@link #println()}</code>.
672 * the <code>Object</code> value to be printed
675 public void println(Object x)