How to check whether an OutputStream is closed

耗尽温柔 提交于 2019-11-28 10:42:15

The underlying stream may not know it its closed until you attempt to write to it (e.g. if the other end of a socket closes it)

The simplest approach is to use it and handle what happens if it closed then, rather than testing it first as well.

No matter what you test, there is always the chance you will get an IOException, so you cannot avoid the exception handling code. Adding this test is likely to complicate the code.

Unfortunately OutputStream API does not have method like isClosed().

So, I know only one clear way: create your class StatusKnowingOutputStream that wraps any other output stream and implements its close() method as following:

public void close() {
    out.close();
    closed = true;
}

Now add method isClosed()

public boolean isClosed() {
    return closed;
}

The OutputStream itself does not support such a method. The Closable interface is defined in a way that once you call close() you are going to dispose of that OutputStream.

Maybe you should revisit a bit the design of the application and check why you're not doing that and you're ending up with a closed OutputStream instance still running around in your application.

public boolean isStreamClosed(FileOutputStream out){
    try {
        FileChannel fc = out.getChannel();
        return fc.position() >= 0L; // This may throw a ClosedChannelException.
    } catch (java.nio.channels.ClosedChannelException cce) {
        return false;
    } catch (IOException e) {
    }
    return true;
}

This is possible only for a FileOutputStream!

No. If you implement your own, you could write an isClosed method, but if you don't know the concrete class, then no. OutputStream is just an abstract class. Here's it's implementation:

   /**
 * Closes this output stream and releases any system resources 
 * associated with this stream. The general contract of <code>close</code> 
 * is that it closes the output stream. A closed stream cannot perform 
 * output operations and cannot be reopened.
 * <p>
 * The <code>close</code> method of <code>OutputStream</code> does nothing.
 *
 * @exception  IOException  if an I/O error occurs.
 */
public void close() throws IOException {
}
William

by using out.checkError()

while(!System.out.checkError()) {
    System.out.println('hi');
}

found it here: How do I get java to exit when piped to head

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!