How to know if a BufferedReader Stream is closed
Solution 1
The method ready()
will throw an exception if closed. But even if you added a closed check method, so long as the lock is released between calls (which it is for BufferedReader
), the reader might be closed by the time you read it.
I see three options:
- Wrap your read call with a try/catch block to handle the closed case.
- Create a subclass of
BufferedReader
that extendsclose()
to set your own variable that can be used to check to see if the reader is closed. This also requires overriding a lot of methods to do whatever the behavior you want with a closed reader if you want it do something beside throw theIOException
. - Add a lock of your own and use it to both close the reader (one thread) and
check that the buffer is ready and read from it. You can either set a variable directly for the check or just group the
ready()
andread()
calls into the same synchronized block.
Solution 2
Another way would be to extend a BufferedReader in your own class, overriding close() method to indicate if it was closed.
Solution 3
If you have already started a read operation, then you will notified with an IOException that a stream is closed. Even if you have called br.ready() before, the exception is happening when your code is blocked at the read method.
There is no way to avoid this. On the contrary, you should expect a read operation to throw an exception and be prepared to handle it appropriately.
Solution 4
I don't think there's any method you can call directly to tell if a stream is closed.
If you really need to have two threads sharing this reader, your best option might be to have the one thread call back to the other thread or set a flag to notify it that the stream has been closed so the other thread knows not to try to read from it.
Admin
Updated on July 22, 2022Comments
-
Admin almost 2 years
I have two threads in Java.
First thread is closing a bufferedreader (
br.close()
)When the second thread does a read on the same reader I get an
IOException (Stream Closed)
I get this exception even if I use
br.ready()
Is there a way to know if the stream is already closed?
-
Robert Munteanu almost 15 yearsCareful - this might be probably slow and might break if the internals of the class change. I suggest using your own 'flag' to guard the buffer.
-
3218801 almost 15 yearsSince Java 1.5 reflection is not as slow as it was before.
-
3218801 almost 15 yearsThat's what Kathy said in 2 :)