PrintStream.java 15.7 KB
Newer Older
1
/* PrintStream.java -- OutputStream for printing output
2
   Copyright (C) 1998, 1999, 2001, 2003 Free Software Foundation, Inc.
Tom Tromey committed
3

4
This file is part of GNU Classpath.
Tom Tromey committed
5

6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
 
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */
Tom Tromey committed
37 38 39 40


package java.io;

41 42
import gnu.gcj.convert.UnicodeToBytes;

Tom Tromey committed
43 44
/* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
 * "The Java Language Specification", ISBN 0-201-63451-1
45
 * Status:  Believed complete and correct to 1.3
Tom Tromey committed
46 47
 */

48 49 50 51 52 53 54 55 56 57 58 59 60
/**
 * This class prints Java primitive values and object to a stream as
 * text.  None of the methods in this class throw an exception.  However,
 * errors can be detected by calling the <code>checkError()</code> method.
 * Additionally, this stream can be designated as "autoflush" when 
 * created so that any writes are automatically flushed to the underlying
 * output sink when the current line is terminated.
 * <p>
 * This class converts char's into byte's using the system default encoding.
 *
 * @author Aaron M. Renn <arenn@urbanophile.com>
 * @author Tom Tromey <tromey@cygnus.com>
 */
Tom Tromey committed
61 62
public class PrintStream extends FilterOutputStream
{
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
  /* Notice the implementation is quite similar to OutputStreamWriter.
   * This leads to some minor duplication, because neither inherits
   * from the other, and we want to maximize performance. */

  // Line separator string.
  private static final char[] line_separator
    = System.getProperty("line.separator").toCharArray();
  
  UnicodeToBytes converter;

  // Work buffer of characters for converter.
  char[] work = new char[100];
  // Work buffer of bytes where we temporarily keep converter output.
  byte[] work_bytes = new byte[100];

78 79 80 81 82 83
  /**
   * This boolean indicates whether or not an error has ever occurred
   * on this stream.
   */
  private boolean error_occurred = false;

84 85 86 87
  /**
   * This is <code>true</code> if auto-flush is enabled, 
   * <code>false</code> otherwise
   */
88 89
  private boolean auto_flush;

90 91
  /**
   * This method intializes a new <code>PrintStream</code> object to write
92
   * to the specified output sink.
93 94 95
   *
   * @param out The <code>OutputStream</code> to write to.
   */
96 97
  public PrintStream (OutputStream out)
  {
98
    this (out, false);
99 100
  }

101 102 103 104
  /**
   * This method intializes a new <code>PrintStream</code> object to write
   * to the specified output sink.  This constructor also allows "auto-flush"
   * functionality to be specified where the stream will be flushed after
105 106 107
   * every <code>print</code> or <code>println</code> call, when the 
   * <code>write</code> methods with array arguments are called, or when a 
   * single new-line character is written.
108 109 110 111 112 113
   * <p>
   *
   * @param out The <code>OutputStream</code> to write to.
   * @param auto_flush <code>true</code> to flush the stream after every 
   * line, <code>false</code> otherwise
   */
114 115
  public PrintStream (OutputStream out, boolean auto_flush)
  {
116
    super (out);
117

118
    converter = UnicodeToBytes.getDefaultEncoder();
119 120 121
    this.auto_flush = auto_flush;
  }

122
  /**
123 124 125
   * This method intializes a new <code>PrintStream</code> object to write
   * to the specified output sink.  This constructor also allows "auto-flush"
   * functionality to be specified where the stream will be flushed after
126 127 128
   * every <code>print</code> or <code>println</code> call, when the 
   * <code>write</code> methods with array arguments are called, or when a 
   * single new-line character is written.
129 130 131 132 133 134 135 136 137 138 139 140 141
   * <p>
   *
   * @param out The <code>OutputStream</code> to write to.
   * @param auto_flush <code>true</code> to flush the stream after every 
   * line, <code>false</code> otherwise
   * @param encoding The name of the character encoding to use for this
   * object.
   */
  public PrintStream (OutputStream out, boolean auto_flush, String encoding)
    throws UnsupportedEncodingException
  {
    super (out);

142
    converter = UnicodeToBytes.getEncoder (encoding);
143 144 145 146
    this.auto_flush = auto_flush;
  }

  /**
147 148 149 150 151 152 153 154
   * This method checks to see if an error has occurred on this stream.  Note
   * that once an error has occurred, this method will continue to report
   * <code>true</code> forever for this stream.  Before checking for an
   * error condition, this method flushes the stream.
   *
   * @return <code>true</code> if an error has occurred, 
   * <code>false</code> otherwise
   */
Tom Tromey committed
155 156
  public boolean checkError ()
  {
157 158
    flush ();
    return error_occurred;
Tom Tromey committed
159 160
  }

161 162 163 164
  /**
   * This method can be called by subclasses to indicate that an error
   * has occurred and should be reported by <code>checkError</code>.
   */
165 166
  protected void setError ()
  {
167
    error_occurred = true;
168 169
  }

170 171 172
  /**
   * This method closes this stream and all underlying streams.
   */
Tom Tromey committed
173 174
  public void close ()
  {
175 176 177 178 179 180 181 182 183 184 185 186 187
    try
      {
	flush();
	out.close();
      }
    catch (InterruptedIOException iioe)
      {
	Thread.currentThread().interrupt();
      }
    catch (IOException e)
      {
	setError ();
      }
Tom Tromey committed
188 189
  }

190 191 192 193
  /**
   * This method flushes any buffered bytes to the underlying stream and
   * then flushes that stream as well.
   */
Tom Tromey committed
194 195
  public void flush ()
  {
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
    try
      {
	out.flush();
      }
    catch (InterruptedIOException iioe)
      {
	Thread.currentThread().interrupt();
      }
    catch (IOException e)
      {
	setError ();
      }
  }

  private synchronized void print (String str, boolean println)
  {
    try
      {
        writeChars(str, 0, str.length());
	if (println)
	  writeChars(line_separator, 0, line_separator.length);
	if (auto_flush)
	  flush();
      }
    catch (InterruptedIOException iioe)
      {
	Thread.currentThread().interrupt();
      }
    catch (IOException e)
      {
	setError ();
      }
  }

  private synchronized void print (char[] chars, int pos, int len,
				   boolean println)
  {
    try
      {
        writeChars(chars, pos, len);
	if (println)
	  writeChars(line_separator, 0, line_separator.length);
	if (auto_flush)
	  flush();
      }
    catch (InterruptedIOException iioe)
      {
	Thread.currentThread().interrupt();
      }
    catch (IOException e)
      {
	setError ();
      }
  }

  private void writeChars(char[] buf, int offset, int count)
    throws IOException
  {
    while (count > 0 || converter.havePendingBytes())
      {
	converter.setOutput(work_bytes, 0);
	int converted = converter.write(buf, offset, count);
	offset += converted;
	count -= converted;
	out.write(work_bytes, 0, converter.count);
      }
  }

  private void writeChars(String str, int offset, int count)
    throws IOException
  {
    while (count > 0 || converter.havePendingBytes())
      {
	converter.setOutput(work_bytes, 0);
	int converted = converter.write(str, offset, count, work);
	offset += converted;
	count -= converted;
	out.write(work_bytes, 0, converter.count);
      }
275 276
  }

277 278 279 280 281 282 283
  /**
   * This methods prints a boolean value to the stream.  <code>true</code>
   * values are printed as "true" and <code>false</code> values are printed
   * as "false".
   *
   * @param b The <code>boolean</code> value to print
   */
Tom Tromey committed
284 285
  public void print (boolean bool)
  {
286
    print(String.valueOf(bool), false);
Tom Tromey committed
287 288
  }

289 290 291 292 293 294
  /**
   * This method prints an integer to the stream.  The value printed is
   * determined using the <code>String.valueOf()</code> method.
   *
   * @param inum The <code>int</code> value to be printed
   */
Tom Tromey committed
295 296
  public void print (int inum)
  {
297
    print(String.valueOf(inum), false);
Tom Tromey committed
298 299
  }

300 301 302 303 304 305
  /**
   * This method prints a long to the stream.  The value printed is
   * determined using the <code>String.valueOf()</code> method.
   *
   * @param lnum The <code>long</code> value to be printed
   */
Tom Tromey committed
306 307
  public void print (long lnum)
  {
308
    print(String.valueOf(lnum), false);
Tom Tromey committed
309 310
  }

311 312 313 314 315 316
  /**
   * This method prints a float to the stream.  The value printed is
   * determined using the <code>String.valueOf()</code> method.
   *
   * @param fnum The <code>float</code> value to be printed
   */
Tom Tromey committed
317 318
  public void print (float fnum)
  {
319
    print(String.valueOf(fnum), false);
Tom Tromey committed
320 321
  }

322 323 324 325 326 327
  /**
   * This method prints a double to the stream.  The value printed is
   * determined using the <code>String.valueOf()</code> method.
   *
   * @param dnum The <code>double</code> value to be printed
   */
Tom Tromey committed
328 329
  public void print (double dnum)
  {
330
    print(String.valueOf(dnum), false);
Tom Tromey committed
331 332
  }

333 334 335 336 337 338 339
  /**
   * This method prints an <code>Object</code> to the stream.  The actual
   * value printed is determined by calling the <code>String.valueOf()</code>
   * method.
   *
   * @param obj The <code>Object</code> to print.
   */
Tom Tromey committed
340 341
  public void print (Object obj)
  {
342
    print(obj == null ? "null" : obj.toString(), false);
Tom Tromey committed
343 344
  }

345 346 347 348 349 350
  /**
   * This method prints a <code>String</code> to the stream.  The actual
   * value printed depends on the system default encoding.
   *
   * @param str The <code>String</code> to print.
   */
Tom Tromey committed
351 352
  public void print (String str)
  {
353
    print(str == null ? "null" : str, false);
Tom Tromey committed
354 355
  }

356 357 358 359 360 361
  /**
   * This method prints a char to the stream.  The actual value printed is
   * determined by the character encoding in use.
   *
   * @param ch The <code>char</code> value to be printed
   */
362
  public synchronized void print (char ch)
Tom Tromey committed
363
  {
364 365
    work[0] = ch;
    print(work, 0, 1, false);
Tom Tromey committed
366 367
  }

368 369 370 371 372 373
  /**
   * This method prints an array of characters to the stream.  The actual
   * value printed depends on the system default encoding.
   *
   * @param s The array of characters to print.
   */
Tom Tromey committed
374 375
  public void print (char[] charArray)
  {
376
    print(charArray, 0, charArray.length, false);
Tom Tromey committed
377 378
  }

379 380 381 382 383
  /**
   * This method prints a line separator sequence to the stream.  The value
   * printed is determined by the system property <xmp>line.separator</xmp>
   * and is not necessarily the Unix '\n' newline character.
   */
Tom Tromey committed
384 385
  public void println ()
  {
386
    print(line_separator, 0, line_separator.length, false);
Tom Tromey committed
387 388
  }

389 390 391 392 393 394 395 396 397
  /**
   * This methods prints a boolean value to the stream.  <code>true</code>
   * values are printed as "true" and <code>false</code> values are printed
   * as "false".
   * <p>
   * This method prints a line termination sequence after printing the value.
   *
   * @param b The <code>boolean</code> value to print
   */
Tom Tromey committed
398 399
  public void println (boolean bool)
  {
400
    print(String.valueOf(bool), true);
Tom Tromey committed
401 402
  }

403 404 405 406 407 408 409 410
  /**
   * This method prints an integer to the stream.  The value printed is
   * determined using the <code>String.valueOf()</code> method.
   * <p>
   * This method prints a line termination sequence after printing the value.
   *
   * @param inum The <code>int</code> value to be printed
   */
Tom Tromey committed
411 412
  public void println (int inum)
  {
413
    print(String.valueOf(inum), true);
Tom Tromey committed
414 415
  }

416 417 418 419 420 421 422 423
  /**
   * This method prints a long to the stream.  The value printed is
   * determined using the <code>String.valueOf()</code> method.
   * <p>
   * This method prints a line termination sequence after printing the value.
   *
   * @param lnum The <code>long</code> value to be printed
   */
Tom Tromey committed
424 425
  public void println (long lnum)
  {
426
    print(String.valueOf(lnum), true);
Tom Tromey committed
427 428
  }

429 430 431 432 433 434 435 436
  /**
   * This method prints a float to the stream.  The value printed is
   * determined using the <code>String.valueOf()</code> method.
   * <p>
   * This method prints a line termination sequence after printing the value.
   *
   * @param fnum The <code>float</code> value to be printed
   */
Tom Tromey committed
437 438
  public void println (float fnum)
  {
439
    print(String.valueOf(fnum), true);
Tom Tromey committed
440 441
  }

442 443 444 445 446 447 448 449
  /**
   * This method prints a double to the stream.  The value printed is
   * determined using the <code>String.valueOf()</code> method.
   * <p>
   * This method prints a line termination sequence after printing the value.
   *
   * @param dnum The <code>double</code> value to be printed
   */
Tom Tromey committed
450 451
  public void println (double dnum)
  {
452
    print(String.valueOf(dnum), true);
Tom Tromey committed
453 454
  }

455 456 457 458 459 460 461 462 463
  /**
   * This method prints an <code>Object</code> to the stream.  The actual
   * value printed is determined by calling the <code>String.valueOf()</code>
   * method.
   * <p>
   * This method prints a line termination sequence after printing the value.
   *
   * @param obj The <code>Object</code> to print.
   */
Tom Tromey committed
464 465
  public void println (Object obj)
  {
466
    print(obj == null ? "null" : obj.toString(), true);
Tom Tromey committed
467 468
  }

469 470 471 472 473 474 475 476
  /**
   * This method prints a <code>String</code> to the stream.  The actual
   * value printed depends on the system default encoding.
   * <p>
   * This method prints a line termination sequence after printing the value.
   *
   * @param str The <code>String</code> to print.
   */
Tom Tromey committed
477 478
  public void println (String str)
  {
479
    print (str == null ? "null" : str, true);
Tom Tromey committed
480 481
  }

482 483 484 485 486 487 488 489
  /**
   * This method prints a char to the stream.  The actual value printed is
   * determined by the character encoding in use.
   * <p>
   * This method prints a line termination sequence after printing the value.
   *
   * @param ch The <code>char</code> value to be printed
   */
490
  public synchronized void println (char ch)
Tom Tromey committed
491
  {
492 493
    work[0] = ch;
    print(work, 0, 1, true);
Tom Tromey committed
494 495
  }

496 497 498 499 500 501 502 503
  /**
   * This method prints an array of characters to the stream.  The actual
   * value printed depends on the system default encoding.
   * <p>
   * This method prints a line termination sequence after printing the value.
   *
   * @param s The array of characters to print.
   */
Tom Tromey committed
504 505
  public void println (char[] charArray)
  {
506
    print(charArray, 0, charArray.length, true);
Tom Tromey committed
507 508
  }

509 510 511 512 513 514 515
  /**
   * This method writes a byte of data to the stream.  If auto-flush is
   * enabled, printing a newline character will cause the stream to be
   * flushed after the character is written.
   * 
   * @param b The byte to be written
   */
Tom Tromey committed
516 517
  public void write (int oneByte)
  {
518 519 520 521 522 523 524
    try
      {
        out.write (oneByte & 0xff);
        
        if (auto_flush && (oneByte == '\n'))
          flush ();
      }
525 526 527 528
    catch (InterruptedIOException iioe)
      {
	Thread.currentThread ().interrupt ();
      }
529 530 531 532
    catch (IOException e)
      {
        setError ();
      }
Tom Tromey committed
533 534
  }

535 536 537 538 539 540 541 542 543
  /**
   * This method writes <code>len</code> bytes from the specified array
   * starting at index <code>offset</code> into the array.
   *
   * @param buffer The array of bytes to write
   * @param offset The index into the array to start writing from
   * @param len The number of bytes to write
   */
  public void write (byte[] buffer, int offset, int len)
Tom Tromey committed
544 545 546
  {
    try
      {
547
        out.write (buffer, offset, len);
548
        
549
        if (auto_flush)
550
          flush ();
Tom Tromey committed
551
      }
552 553 554 555
    catch (InterruptedIOException iioe)
      {
	Thread.currentThread ().interrupt ();
      }
Tom Tromey committed
556 557
    catch (IOException e)
      {
558
        setError ();
Tom Tromey committed
559 560
      }
  }
561 562
} // class PrintStream