Eche un vistazo a la fuente de PrintStream.
Tiene dos referencias al escritor subyacente textOut
y charOut
, una base de caracteres y una basada en texto (lo que sea que eso signifique). Además, hereda una tercera referencia al OutputStream basado en bytes, llamado out
.
/**
* 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;
En el método close()
cierra todos ellos (textOut
es básicamente el mismo que 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;
}
}
}
Ahora, la parte interesante es que charOut contiene un (envuelto) hace referencia a la PrintStream sí (nótese el init(new OutputStreamWriter(this))
en el constructor)
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));
}
Por lo tanto, la llamada a close()
llamarán charOut.close()
, que a su vez vuelve a llamar al close()
original, por lo que tenemos el indicador de cierre para acortar la recursión infinita.
Algo para lo que un depurador es bueno. Coloque un punto de interrupción en el método de cierre y debería poder ver por qué se llama. –