BufferedWriter.write does not throw exception if it is already closed.
Alan Bateman
Alan.Bateman at oracle.com
Fri Jan 28 15:44:06 UTC 2011
Jing LV wrote:
> Thanks for reply Alan, sorry didn't see this mail before I raise an
> issue on https://bugs.openjdk.java.net/show_bug.cgi?id=100169 (my mail
> server seems some problem these days)
>
> Consider it is using underlying writer, one thing I need to mention is
> that in the testcase I post in the first mail, it was trying to throw
> an exception[1] in its write method when it's closed already. I am ok
> with the documentation modification myself, however I wonder it may
> confuse customer if he found his own writer has no problem at all?
>
> [1]
> @Override
> public void write(char[] buf, int offset, int count) throws
> IOException {
> if (closed) {
> throw new IOException("Already closed");
> }
> }
I'll add S=7015589 to that bugzilla bug so that it links to the links to
the "Sun" bug that I created after the previous mail.
I don't think I fully understand what you are saying in the above but
let me just repeat that I think we should look at clarifying close so
that the BufferedWriter is considered closed even if the close method
fails with an IOException. That would mean that a subsequence attempt to
write to the closed stream (irrespective of whether close completed with
or without an exception) would fail consistently rather than being
dependent on the buffer size. See attached as an initial patch to see
how it would work. I was just mentioning that we can fix a few other
issues while we are there - for example the exception from the
underlying writer's close supplants the exception from the flush.
-Alan
diff --git a/src/share/classes/java/io/BufferedReader.java
b/src/share/classes/java/io/BufferedReader.java
--- a/src/share/classes/java/io/BufferedReader.java
+++ b/src/share/classes/java/io/BufferedReader.java
@@ -514,9 +514,12 @@ public class BufferedReader extends Read
synchronized (lock) {
if (in == null)
return;
- in.close();
- in = null;
- cb = null;
+ try {
+ in.close();
+ } finally {
+ in = null;
+ cb = null;
+ }
}
}
}
diff --git a/src/share/classes/java/io/BufferedWriter.java
b/src/share/classes/java/io/BufferedWriter.java
--- a/src/share/classes/java/io/BufferedWriter.java
+++ b/src/share/classes/java/io/BufferedWriter.java
@@ -260,10 +260,9 @@ public class BufferedWriter extends Writ
if (out == null) {
return;
}
- try {
+ try (Writer w = out) {
flushBuffer();
} finally {
- out.close();
out = null;
cb = null;
}
diff --git a/src/share/classes/java/io/FilterOutputStream.java
b/src/share/classes/java/io/FilterOutputStream.java
--- a/src/share/classes/java/io/FilterOutputStream.java
+++ b/src/share/classes/java/io/FilterOutputStream.java
@@ -153,10 +153,8 @@ class FilterOutputStream extends OutputS
* @see java.io.FilterOutputStream#out
*/
public void close() throws IOException {
- try {
- flush();
- } catch (IOException ignored) {
+ try (OutputStream ostream = out) {
+ flush();
}
- out.close();
}
}
More information about the core-libs-dev
mailing list