?
marks are probably because the stream source is returning -1 to indicate the end of the stream. But the while loop is waiting for a carriage return.
The read
call is blocking and will wait for the input. Only if it detects the end of the stream or on some erroneous condition does it throw an exception.
I presume you are reading lines. It would be efficient to use BufferedReader
instead of reading it byte by byte.
So, if you were reading from standard input. You could do something like :
BufferedReader reader = new BufferedReader(
new InputStreamReader(new BufferedInputStream(System.in)));
try {
StringBuilder result = new StringBuilder();
for (String line = null; (line = reader.readLine()) != null;) {
result.append(line);
}
//process result
System.out.println(result);
} catch (IOException e){
e.printStackTrace();
}
The toString
method of InputStreamReader
is not overloaded.