BufferedOutputStream does not throw exception after it's closed
Jing LV
lvjing at linux.vnet.ibm.com
Tue Jan 25 15:15:52 UTC 2011
Hello,
Find a similar problem with the one in the last mail(BufferedWriter),
this time is BufferedOutputStream, and it has a delegated output stream
and behave the same with BufferedWriter, I believe the two problems are
just the same - it needs to check the open status before store the
contents even if it does not need to flush. A similar testcase can be:
private int bufferSize = 1024;
public void test() throws Exception {
OutputStream out = new BufferedOutputStream(new
ClosableByteArrayOutputStream(), bufferSize);
out.close();
try {
out.write(new byte[] { 7, 3, 4, 5 });
fail("expected already closed exception");
} catch (IOException expected) {
}
}
private static class ClosableByteArrayOutputStream extends OutputStream {
private final ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
private boolean closed = false;
@Override
public void close() throws IOException {
closed = true;
}
@Override
public void write(int oneByte) throws IOException {
if (closed) {
throw new IOException();
}
bytesOut.write(oneByte);
}
}
Any comments? I'd like to raise this problem on the bug-tracker system.
More information about the core-libs-dev
mailing list