0

I have a function that does something like this.

try {
    Process p = runtime.exec(fullCommand, null);

    BufferedReader outReaderOutput = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line = outReaderOutput.readLine();
    String output = "";

    while (line != null) {
        output += line + "\n";
        line = outReaderOutput.readLine();
    }
    return output;
} catch (IOException e) {
    throw new RuntimeException(e);
}

Is there a way to return Stream instead of the entire String? What would be the benefit of that?

rudolph1024
  • 962
  • 1
  • 12
  • 32
redAlert
  • 49
  • 1
  • 7
  • 5
    Why are you asking if something's possible, if you don't know of what use it would be? – Siguza Jul 29 '15 at 15:50
  • 2
    use `append` method of `StringBuilder` class instead `+=` – Andrew Tobilko Jul 29 '15 at 15:52
  • 2
    Voted to reopen this as it is certainly possible to objectively list the advantages and disadvantages of each approach without giving an opinionated answer. This is not likely to end up in a flame war; it's not a choice of implementation or something similar. – Maarten Bodewes Aug 06 '15 at 20:15

3 Answers3

3

A string is basically stored in memory; its contents are completely available at the time you receive the String instance. You can directly search the string or find other information - such as the length - of the string.

If you receive a Reader instance you don't know the size of the string, and many functions such as searching are not (directly) available. On the other hand, you don't need to store the entire string in memory.

Moreover, not all of the contents need to be available at the time you receive the Reader instance. A good example is receiving text over a socket; you may want to handle part of the contents when they are received, not after the full message is received.

The complete output of an application - in your example code - may also not be directly available; your method blocks until it is (i.e. until your fullCommand application exits).


If you want to return a character stream you can simply return the BufferedReader instance. If it makes sense to return a binary stream from a Reader most of the time, but if this is required then read this answer.

EDIT: if you just need binary you should of course directly return p.getInputStream() in your example.

Community
  • 1
  • 1
Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
2

You can easily return the p.getInputStream() That depend on your implementation.

1

In the end, you're better off returning a string if you need the whole file at once.

As it stands, I don't think the Java compiler can optimize this to a StringBuilder, which basically allows amortized constant time string concatenation.

You're better off using a StringBuilder like this:

StringBuilder sb = new StringBuilder();
while(outReaderOutput.hasMoreLines())
{
    sb.append(outReaderOutput.readLine()).append('\n');
}

EDIT: wow, should not have missed that.

nameless912
  • 367
  • 1
  • 12