Skip to content

Commit

Permalink
Merge pull request #2999 from liquibase/DAT-10419-issue-6
Browse files Browse the repository at this point in the history
do not allow individual commands to close the top-level OutputStream (System.out)
  • Loading branch information
wwillard7800 committed Jun 23, 2022
2 parents 5aacdb4 + 137a92a commit a0d0519
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import liquibase.configuration.*;
import liquibase.exception.CommandExecutionException;
import liquibase.exception.CommandValidationException;
import liquibase.io.UnclosableOutputStream;
import liquibase.util.StringUtil;

import java.io.OutputStream;
Expand Down Expand Up @@ -127,7 +128,12 @@ public <T> T getArgumentValue(CommandArgumentDefinition<T> argument) {
* Think "what would be piped out", not "what the user is told about what is happening".
*/
public CommandScope setOutput(OutputStream outputStream) {
this.outputStream = outputStream;
/*
This is an UncloseableOutputStream because we do not want individual command steps to inadvertently (or
intentionally) close the System.out OutputStream. Closing System.out renders it unusable for other command
steps which expect it to still be open.
*/
this.outputStream = new UnclosableOutputStream(outputStream);

return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package liquibase.io;

import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
* This class is a wrapper around OutputStreams, and makes them impossible for callers to close.
*/
public class UnclosableOutputStream extends FilterOutputStream {
public UnclosableOutputStream(OutputStream out) {
super(out);
}

/**
* This method does not actually close the underlying stream, but rather only flushes it. Callers should not be
* closing the stream they are given.
*/
@Override
public void close() throws IOException {
out.flush();
}
}

0 comments on commit a0d0519

Please sign in to comment.