PrintStreamのソースをご覧ください。
基本的なWriter textOut
とcharOut
の2つの参照、1つの文字ベース、1つのテキストベース(その意味を問わず)があります。また、それはout
と呼ばれるバイトベースのOutputStreamへの3番目の参照を継承します。それはそれらのすべてを閉鎖close()
方法において
/**
* Track both the text- and character-output streams, so that their buffers
* can be flushed without flushing the entire stream.
*/
private BufferedWriter textOut;
private OutputStreamWriter charOut;
(textOut
は基本的charOut
と同じです)。
private boolean closing = false; /* To avoid recursive closing */
/**
* Close the stream. This is done by flushing the stream and then closing
* the underlying output stream.
*
* @see java.io.OutputStream#close()
*/
public void close() {
synchronized (this) {
if (! closing) {
closing = true;
try {
textOut.close();
out.close();
}
catch (IOException x) {
trouble = true;
}
textOut = null;
charOut = null;
out = null;
}
}
}
は今、興味深い部分は、charOutは(ラップ)
private void init(OutputStreamWriter osw) {
this.charOut = osw;
this.textOut = new BufferedWriter(osw);
}
/**
* Create a new print stream.
*
* @param out The output stream to which values and objects will be
* printed
* @param autoFlush A boolean; if true, the output buffer will be flushed
* whenever a byte array is written, one of the
* <code>println</code> methods is invoked, or a newline
* character or byte (<code>'\n'</code>) is written
*
* @see java.io.PrintWriter#PrintWriter(java.io.OutputStream, boolean)
*/
public PrintStream(OutputStream out, boolean autoFlush) {
this(autoFlush, out);
init(new OutputStreamWriter(this));
}
だから、close()
への呼び出しがcharOut.close()
を呼び出しますのPrintStream自体(注意コンストラクタでinit(new OutputStreamWriter(this))
)を基準に含まれていることですこれは元のclose()
を再度呼び出します。そのため、無限再帰を短くする閉じるフラグがあります。
これはデバッガに適したものです。 closeメソッドにブレークポイントを置くと、なぜ呼び出されたのかを知ることができます。 –