A BufferedReader#readLine()
reads the stream until it reaches either \n
, \r
, or the end of the stream, returning what it read up to then or null
. You're not showing us something. Why are there new lines between lines in your output?
As for the null
at the end of your output
private void lines(InputStream inputStream) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
String line = "";
do {
System.out.println(line); // will print an empty line to start
} while ((line = br.readLine()) != null);
System.out.println(line); // will print null
}
Your loop should be
while ((line = br.readLine()) != null) {
System.out.println(line);
}
to avoid printing the first empty one. Also, don't print the line
outside the while
loop. It will always be null
, obviously (you wouldn't be outside the while
otherwise).
Your chars()
method is also looping forever.
It's very possible your server isn't closing the stream and so the underlying read
calls that the BufferedReader
makes aren't returning -1
to indicate EOF. Are you killing the application yourself?