[ https://issues.apache.org/jira/browse/IO-546?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16128181#comment-16128181 ]
Elijah Zupancic edited comment on IO-546 at 8/16/17 1:14 AM: ------------------------------------------------------------- This sample application illustrates the inconsistency: {code:java} import org.apache.commons.io.IOUtils; import org.apache.commons.io.output.CloseShieldOutputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; public class BrokenShield { public static void main(String[] argv) throws IOException { File file = File.createTempFile("broken-shield", "txt"); byte[] arbitraryData = "Hello World ".getBytes(StandardCharsets.UTF_8); FileOutputStream fout = new FileOutputStream(file); CloseShieldOutputStream cout = new CloseShieldOutputStream(fout); try { // This should work because we haven't tried to close the stream cout.write(arbitraryData); // Here we pretend this is some stupid library that insists on // closing a stream when it shouldn't. cout.close(); // After we try to close the stream, new data can't be written to // the stream. For example: cout.write(arbitraryData); // Would throw an exception like: // java.io.IOException: write(72) failed: stream is closed // However, if we call flush(), no exception is thrown - this is // inconsistent with the behavior of write() cout.flush(); } finally { // We properly close the stream we have to use the underlying // stream like you would expect. fout.close(); } try (FileInputStream fin = new FileInputStream(file)) { String data = IOUtils.toString(fin, StandardCharsets.UTF_8); System.out.println(data); } } } {code} was (Author: elijah): This sample application illustrates the inconsistency: {code:java} import org.apache.commons.io.IOUtils; import org.apache.commons.io.output.CloseShieldOutputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; public class BrokenShield { public static void main(String[] argv) throws IOException { File file = File.createTempFile("broken-shield", "txt"); byte[] arbitraryData = "Hello World ".getBytes(StandardCharsets.UTF_8); FileOutputStream fout = new FileOutputStream(file); BufferedOutputStream bout = new BufferedOutputStream(fout, 99999); CloseShieldOutputStream cout = new CloseShieldOutputStream(fout); try { // This should work because we haven't tried to close the stream cout.write(arbitraryData); // Here we pretend this is some stupid library that insists on // closing a stream when it shouldn't. cout.close(); // After we try to close the stream, new data can't be written to // the stream. For example: cout.write(arbitraryData); // Would throw an exception like: // java.io.IOException: write(72) failed: stream is closed // However, if we call flush(), no exception is thrown - this is // inconsistent with the behavior of write() cout.flush(); } finally { // We properly close the stream we have to use the underlying // stream like you would expect. fout.close(); } try (FileInputStream fin = new FileInputStream(file)) { String data = IOUtils.toString(fin, StandardCharsets.UTF_8); System.out.println(data); } } } {code} > ClosedOutputStream#flush should throw > ------------------------------------- > > Key: IO-546 > URL: https://issues.apache.org/jira/browse/IO-546 > Project: Commons IO > Issue Type: Improvement > Components: Streams/Writers > Reporter: Tomas Celaya > Priority: Minor > Attachments: IO-546.patch > > > While debugging an issue involving usage of {{CloseShieldOutputStream}} I > discovered that {{ClosedOutputStream#flush}} is not overridden and will > silently succeed. Not sure how much of a breaking change this might be but I > think it makes more sense for {{ClosedOutputStream#flush}} to throw. This is > only really meaningful in contexts where multiple streams are being chained > together and some of the streams before {{CloseShieldOutputStream}} perform > buffering, but it would make behavior more consistent for these more complex > use-cases. > No patches are included because I'm interested in hearing what the opinion > would be on this issue. I can provide a patch if this seems like desired > behavior. -- This message was sent by Atlassian JIRA (v6.4.14#64029)