⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 printstream.java

📁 gcc的组建
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* PrintStream.java -- OutputStream for printing output   Copyright (C) 1998, 1999, 2001, 2003, 2004, 2005  Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe 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, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING.  If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library.  Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule.  An independent module is a module which is not derived fromor based on this library.  If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so.  If you do not wish to do so, delete thisexception statement from your version. */package java.io;import gnu.gcj.convert.UnicodeToBytes;/* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3 * "The Java Language Specification", ISBN 0-201-63451-1 * Status:  Believed complete and correct to 1.3 *//** * 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) */public class PrintStream extends FilterOutputStream{  /* 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];  /**   * This boolean indicates whether or not an error has ever occurred   * on this stream.   */  private boolean error_occurred = false;  /**   * This is <code>true</code> if auto-flush is enabled,    * <code>false</code> otherwise   */  private boolean auto_flush;  /**   * This method intializes a new <code>PrintStream</code> object to write   * to the specified output sink.   *   * @param out The <code>OutputStream</code> to write to.   */  public PrintStream (OutputStream out)  {    this (out, false);  }  /**   * 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   * 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.   * <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   */  public PrintStream (OutputStream out, boolean auto_flush)  {    super (out);    converter = UnicodeToBytes.getDefaultEncoder();    this.auto_flush = auto_flush;  }  /**   * 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   * 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.   * <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);    converter = UnicodeToBytes.getEncoder (encoding);    this.auto_flush = auto_flush;  }  /**   * 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   */  public boolean checkError ()  {    flush ();    return error_occurred;  }  /**   * This method can be called by subclasses to indicate that an error   * has occurred and should be reported by <code>checkError</code>.   */  protected void setError ()  {    error_occurred = true;  }  /**   * This method closes this stream and all underlying streams.   */  public void close ()  {    try      {	flush();	out.close();      }    catch (InterruptedIOException iioe)      {	Thread.currentThread().interrupt();      }    catch (IOException e)      {	setError ();      }  }  /**   * This method flushes any buffered bytes to the underlying stream and   * then flushes that stream as well.   */  public void flush ()  {    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);      }  }  /**   * 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 bool The <code>boolean</code> value to print

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -