Can I check the streams value for null by this exception:
(null != supplier)
Or i need check:
(null != readerSupplier.getInput())
Should I flush() output stream?
See comments.
public void run() {
try {
final Process process = Runtime.getRuntime().exec("su");
InputSupplier<InputStreamReader> readerSupplier = CharStreams.newReaderSupplier(new InputSupplier<InputStream>() {
@Override
public InputStream getInput() throws IOException {
return process.getInputStream();
}
}, Charsets.UTF_8);
OutputSupplier<OutputStreamWriter> writerSupplier = CharStreams.newWriterSupplier(new OutputSupplier<OutputStream>() {
@Override
public OutputStream getOutput() throws IOException {
return process.getOutputStream();
}
}, Charsets.UTF_8);
// can I check the stream objects for null readerSupplier/writerSupplier?
// or need: readerSupplier.getInput()/writerSupplier.getOutput()?
if (null != readerSupplier && null != writerSupplier) {
...
CharStreams.write(someCommand, writerSupplier);
// should I flush() output stream? How better? Flushables.flush(writerSupplier) or Flushables.flush(writerSupplier.getOutput())
}
From the Javadoc of
Process, it doesn’t look like any of these values could ever be null, so you shouldn’t need to check any of the values for null, full stop.In any event, the purpose of using
OutputSupplierinCharStreams.writeis that it will open, write, and close the stream without you having to do anything, so there’s no need toflushor do anything at all.