0

この例外によって null のストリーム値を確認できますか:

(null != サプライヤー)

または、チェックが必要です:

(null != readerSupplier.getInput())

出力ストリームを flush() する必要がありますか? コメントを参照してください。

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())  
        }
4

1 に答える 1

1

の Javadoc から、Processこれらの値のいずれかが null になる可能性はないように見えるため、null、ピリオドのいずれかの値を確認する必要はありません。

OutputSupplierいずれにせよ、 inを使用する目的は、CharStreams.write何もしなくてもストリームを開いたり、書き込んだり、閉じたりすることです。そのため、何もする必要はありflushません。

于 2012-11-05T19:07:08.497 に答える