When I use the object OutputSupplier, adding the first line (CharStreams.write) is executed correctly. But then calling CharStreams.write again throws an IOException.
Is this the correct behavior of the object? If so, how can you append a String to the supplier object without closing the inline stream?
...
final Process process = Runtime.getRuntime().exec("su");
OutputSupplier<OutputStreamWriter> writerSupplier = CharStreams.newWriterSupplier(new OutputSupplier<OutputStream>() {
@Override
public OutputStream getOutput() throws IOException {
return process.getOutputStream();
}
}, Charsets.UTF_8);
// ok
CharStreams.write(someCommand, writerSupplier);
...
// IOException
CharStreams.write(otherCommand, writerSupplier);
This is definitely expected behavior.
CharStreams.writewith anOutputSupplieropens the output stream, writes, and closes it. That’s part of the point. Presumably,Process.getOutputStream()doesn’t let you open and close more than once.Either do all the writes at once, or more likely, don’t use
CharStreams.writeand deal with closing the stream yourself.