In Java, how can use the inputStream from a process with Google's guava CharStreams.toString?

I haven't found a way to do this with guava yet. I am sure the developer has a good reason for this. The closest I have gotten without while keep it minimal is: CharStreams.

ToString(newReaderSupplier(new InputSupplier() { @Override public InputStream getInput() throws IOException { return inputStream; } }, Charsets. UTF_16)) I have used newReaderSupplier from CharStreams so you don't have to wrap it with InputStreamReader.

I haven't found a way to do this with guava yet. I am sure the developer has a good reason for this. The closest I have gotten without while keep it minimal is: CharStreams.

ToString(newReaderSupplier(new InputSupplier() { @Override public InputStream getInput() throws IOException { return inputStream; } }, Charsets. UTF_16)); I have used newReaderSupplier from CharStreams so you don't have to wrap it with InputStreamReader.

What about this: CharStreams. ToString(new InputStreamReader(process.getInpusttream())) It is using CharStreams. ToString(Readable).

This doesn't close the stream when done--it only does that with an InputSupplier – Zugwalt Aug 11 at 17:47.

So far this is the best I could do: String commandOutput = CharStreams. ToString(new InputSupplier() { public InputStreamReader getInput() throws IOException { return new InputStreamReader(process.getInputStream()); } }).

The Right Thing to Do -- and what Guava's trying to push you into doing -- is to change the code that gives you the InputStream to give you an InputSupplier instead. The reason for this is that this way, Guava gets the stream, reads the string, and closes it, and you can't accidentally use it after Guava's closed it, because you never had a reference to the InputStream in the first place. This eliminates a lot of potential bugs.

The other overload, CharStreams. ToString(Readable), does not close the Readable. If you want to have your own, special logic for closing the input stream, this is Guava's way of letting you do that.

Guava equivalent for IOUtils. ToString(InputStream) says it better than I could.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions