2 // ========================================================================
3 // Copyright (c) 1995-2014 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);
137 protected void setError()
139 setError(new IOException());
142 /* ------------------------------------------------------------ */
143 /** Check to make sure that the stream has not been closed */
144 private void isOpen() throws IOException
146 if (_ioException!=null)
147 throw new RuntimeIOException(_ioException);
150 throw new IOException("Stream closed");
153 /* ------------------------------------------------------------ */
168 catch (IOException ex)
174 /* ------------------------------------------------------------ */
189 catch (IOException ex)
195 /* ------------------------------------------------------------ */
197 * Write a single character.
200 * int specifying a character to be written.
203 public void write(int c)
213 catch (InterruptedIOException x)
215 Thread.currentThread().interrupt();
217 catch (IOException ex)
223 /* ------------------------------------------------------------ */
225 * Write a portion of an array of characters.
228 * Array of characters
230 * Offset from which to start writing characters
232 * Number of characters to write
235 public void write(char buf[], int off, int len)
242 out.write(buf,off,len);
245 catch (InterruptedIOException x)
247 Thread.currentThread().interrupt();
249 catch (IOException ex)
255 /* ------------------------------------------------------------ */
257 * Write an array of characters. This method cannot be inherited from the
258 * Writer class because it must suppress I/O exceptions.
261 * Array of characters to be written
264 public void write(char buf[])
266 this.write(buf,0,buf.length);
269 /* ------------------------------------------------------------ */
271 * Write a portion of a string.
276 * Offset from which to start writing characters
278 * Number of characters to write
281 public void write(String s, int off, int len)
288 out.write(s,off,len);
291 catch (InterruptedIOException x)
293 Thread.currentThread().interrupt();
295 catch (IOException ex)
301 /* ------------------------------------------------------------ */
303 * Write a string. This method cannot be inherited from the Writer class
304 * because it must suppress I/O exceptions.
307 * String to be written
310 public void write(String s)
312 this.write(s,0,s.length());
315 private void newLine()
322 out.write(_lineSeparator);
327 catch (InterruptedIOException x)
329 Thread.currentThread().interrupt();
331 catch (IOException ex)
337 /* ------------------------------------------------------------ */
339 * Print a boolean value. The string produced by <code>{@link
340 * java.lang.String#valueOf(boolean)}</code> is translated into bytes
341 * according to the platform's default character encoding, and these bytes
342 * are written in exactly the manner of the <code>{@link
343 * #write(int)}</code> method.
346 * The <code>boolean</code> to be printed
349 public void print(boolean b)
351 this.write(b?"true":"false");
354 /* ------------------------------------------------------------ */
356 * Print a character. The character is translated into one or more bytes
357 * according to the platform's default character encoding, and these bytes
358 * are written in exactly the manner of the <code>{@link
359 * #write(int)}</code> method.
362 * The <code>char</code> to be printed
365 public void print(char c)
370 /* ------------------------------------------------------------ */
372 * Print an integer. The string produced by <code>{@link
373 * java.lang.String#valueOf(int)}</code> is translated into bytes according
374 * to the platform's default character encoding, and these bytes are written
375 * in exactly the manner of the <code>{@link #write(int)}</code> method.
378 * The <code>int</code> to be printed
379 * @see java.lang.Integer#toString(int)
382 public void print(int i)
384 this.write(String.valueOf(i));
387 /* ------------------------------------------------------------ */
389 * Print a long integer. The string produced by <code>{@link
390 * java.lang.String#valueOf(long)}</code> is translated into bytes according
391 * to the platform's default character encoding, and these bytes are written
392 * in exactly the manner of the <code>{@link #write(int)}</code> method.
395 * The <code>long</code> to be printed
396 * @see java.lang.Long#toString(long)
399 public void print(long l)
401 this.write(String.valueOf(l));
404 /* ------------------------------------------------------------ */
406 * Print a floating-point number. The string produced by <code>{@link
407 * java.lang.String#valueOf(float)}</code> is translated into bytes
408 * according to the platform's default character encoding, and these bytes
409 * are written in exactly the manner of the <code>{@link #write(int)}</code>
413 * The <code>float</code> to be printed
414 * @see java.lang.Float#toString(float)
417 public void print(float f)
419 this.write(String.valueOf(f));
422 /* ------------------------------------------------------------ */
424 * Print a double-precision floating-point number. The string produced by
425 * <code>{@link java.lang.String#valueOf(double)}</code> is translated into
426 * bytes according to the platform's default character encoding, and these
427 * bytes are written in exactly the manner of the <code>{@link
428 * #write(int)}</code> method.
431 * The <code>double</code> to be printed
432 * @see java.lang.Double#toString(double)
435 public void print(double d)
437 this.write(String.valueOf(d));
440 /* ------------------------------------------------------------ */
442 * Print an array of characters. The characters are converted into bytes
443 * according to the platform's default character encoding, and these bytes
444 * are written in exactly the manner of the <code>{@link #write(int)}</code>
448 * The array of chars to be printed
450 * @throws NullPointerException
451 * If <code>s</code> is <code>null</code>
454 public void print(char s[])
459 /* ------------------------------------------------------------ */
461 * Print a string. If the argument is <code>null</code> then the string
462 * <code>"null"</code> is printed. Otherwise, the string's characters are
463 * converted into bytes according to the platform's default character
464 * encoding, and these bytes are written in exactly the manner of the
465 * <code>{@link #write(int)}</code> method.
468 * The <code>String</code> to be printed
471 public void print(String s)
480 /* ------------------------------------------------------------ */
482 * Print an object. The string produced by the <code>{@link
483 * java.lang.String#valueOf(Object)}</code> method is translated into bytes
484 * according to the platform's default character encoding, and these bytes
485 * are written in exactly the manner of the <code>{@link #write(int)}</code>
489 * The <code>Object</code> to be printed
490 * @see java.lang.Object#toString()
493 public void print(Object obj)
495 this.write(String.valueOf(obj));
498 /* ------------------------------------------------------------ */
500 * Terminate the current line by writing the line separator string. The line
501 * separator string is defined by the system property
502 * <code>line.separator</code>, and is not necessarily a single newline
503 * character (<code>'\n'</code>).
506 public void println()
511 /* ------------------------------------------------------------ */
513 * Print a boolean value and then terminate the line. This method behaves as
514 * though it invokes <code>{@link #print(boolean)}</code> and then
515 * <code>{@link #println()}</code>.
518 * the <code>boolean</code> value to be printed
521 public void println(boolean x)
530 /* ------------------------------------------------------------ */
532 * Print a character and then terminate the line. This method behaves as
533 * though it invokes <code>{@link #print(char)}</code> and then <code>{@link
534 * #println()}</code>.
537 * the <code>char</code> value to be printed
540 public void println(char x)
549 /* ------------------------------------------------------------ */
551 * Print an integer and then terminate the line. This method behaves as
552 * though it invokes <code>{@link #print(int)}</code> and then <code>{@link
553 * #println()}</code>.
556 * the <code>int</code> value to be printed
559 public void println(int x)
568 /* ------------------------------------------------------------ */
570 * Print a long integer and then terminate the line. This method behaves as
571 * though it invokes <code>{@link #print(long)}</code> and then
572 * <code>{@link #println()}</code>.
575 * the <code>long</code> value to be printed
578 public void println(long x)
587 /* ------------------------------------------------------------ */
589 * Print a floating-point number and then terminate the line. This method
590 * behaves as though it invokes <code>{@link #print(float)}</code> and then
591 * <code>{@link #println()}</code>.
594 * the <code>float</code> value to be printed
597 public void println(float x)
606 /* ------------------------------------------------------------ */
608 * Print a double-precision floating-point number and then terminate the
609 * line. This method behaves as though it invokes <code>{@link
610 * #print(double)}</code> and then <code>{@link #println()}</code>.
613 * the <code>double</code> value to be printed
615 /* ------------------------------------------------------------ */
617 public void println(double x)
626 /* ------------------------------------------------------------ */
628 * Print an array of characters and then terminate the line. This method
629 * behaves as though it invokes <code>{@link #print(char[])}</code> and then
630 * <code>{@link #println()}</code>.
633 * the array of <code>char</code> values to be printed
636 public void println(char x[])
645 /* ------------------------------------------------------------ */
647 * Print a String and then terminate the line. This method behaves as though
648 * it invokes <code>{@link #print(String)}</code> and then
649 * <code>{@link #println()}</code>.
652 * the <code>String</code> value to be printed
655 public void println(String x)
664 /* ------------------------------------------------------------ */
666 * Print an Object and then terminate the line. This method behaves as
667 * though it invokes <code>{@link #print(Object)}</code> and then
668 * <code>{@link #println()}</code>.
671 * the <code>Object</code> value to be printed
674 public void println(Object x)