2011-08-11 1 views
4

私はプロセスを実行していて、その出力を文字列に読み込みたいとします。 try/catch/finallyを扱うのではなく、GuavaCharStreams.toString(InputSupplier<R> supplier)を使用したいと考えています。残念ながら、ProcessのgetInputStream()によって返されたストリームは、タイプInputStreamであり、InputSupplierではありません。これを使ってoString()と一緒にInputSupplierを作成するにはどうすればいいですか?Javaでは、Googleのguava CharStreams.toStringを使用してプロセスからinputStreamを使用するにはどうすればよいですか?

理想的には私はこのような何か行うことができます:

CharStreams.toString(CharStreams.newReaderSupplier(process.getInputStream())) 

をしかし、あなたがたInputStreamからInputSupplierを構築することはできませんし、私はこれを行う方法を見つけ出す問題を抱えています。

String commandOutput = CharStreams.toString(new InputSupplier<InputStreamReader>() { 
    public InputStreamReader getInput() throws IOException { 
     return new InputStreamReader(process.getInputStream()); 
    } 
}); 

答えて

3

私はまだグアバでこれを行う方法が見つかりませんでした。私は開発者がこれに正当な理由があると確信しています。私はそれを最小限に保つ一方でなし得ている最も近いです:あなたがInputStreamReaderでそれをラップする必要はありませんので

CharStreams.toString(CharStreams.newReaderSupplier(new InputSupplier<InputStream>() { 
     @Override 
     public InputStream getInput() throws IOException { 
      return inputStream; 
     } 
    }, Charsets.UTF_16)); 

私はCharStreamsからnewReaderSupplierを使用しています。

1

は、これまでのところ、これは私が何ができる最善の方法です。

1

これは何について::それはCharStreams.toString(Readable)を使用している

CharStreams.toString(new InputStreamReader(process.getInpusttream())) 

+0

これは、ストリームを閉じていない - それだけでことを行いますInputSupplier – Zugwalt

1

Guavaがあなたにやりたいことは、代わりにInputSupplierを与えるためのInputStreamを提供するコードを変更することです。

この理由は、Guavaがストリームを取得し、文字列を読み込んで閉じ、Guavaが閉じた後で誤って使用することができないためです。最初の場所。これにより、多くの潜在的なバグが排除されます。

他のオーバーロードCharStreams.toString(読み取り可能)は、読み取り可能を閉じません。入力ストリームを閉じるための独自の特別なロジックが必要な場合は、これを行うためのGuavaの方法です。

Guava equivalent for IOUtils.toString(InputStream)は私よりも優れていると言います。

0

これにそれを実行する必要があります。彼は

HttpURLConnection connection = null; 
URL url; 
InputStream is = null; 
try { 
    url = new URL("https://graph.facebook.com/oauth/access_token?client_id=" + appId + "&client_secret=" + appSecret + "&grant_type=client_credentials"); 
    connection = (HttpURLConnection) url.openConnection(); 
    connection.setRequestProperty("accept-encoding", "gzip"); 

    is = connection.getInputStream(); 
    String content = CharStreams.toString(new InputStreamReader(is, Charsets.UTF_8)); 
    String[] tokens = content.split("="); 
    System.out.println(tokens[1]); 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} finally { 
    if (is != null) try { 
    is.close(); 
    } catch (IOException e) {} 
    connection.disconnect(); 
} 

私は質問で本当にこのinsn'tを知っているが、比較のために:

InputStream is = process.getInputStream(); 
String content = CharStreams.toString(new InputStreamReader(is, Charsets.UTF_8)); 
is.close(); 

そしてここでは、完全な使用の実際の生活の例です。あなたはIOUtilsでそれを行うだろう方法です - IMOは少しクリーナーである:完了時

HttpURLConnection connection = null; 
URL url; 
InputStream is = null; 

try { 
    url = new URL("https://graph.facebook.com/oauth/access_token?client_id=" + appId + "&client_secret=" + appSecret + "&grant_type=client_credentials"); 
    connection = (HttpURLConnection) url.openConnection(); 
    connection.setRequestProperty("accept-encoding", "gzip"); 

    is = connection.getInputStream(); 

    String value = IOUtils.toString(is); 
    if (!Strings.isNullOrEmpty(value)) { 
    String[] splits = value.split("="); 
    System.out.println(splits[1]); 
    } 
} catch (IOException e) { 

} finally { 
    IOUtils.closeQuietly(is); 
    connection.disconnect(); 
} 
関連する問題