Note that there are some explanatory texts on larger screens.

plurals
  1. POwhy does BufferedReader fail to read the final line for an InputStream?
    primarykey
    data
    text
    <p>*Note: To clarify, maybe this question wasn't quite clear on the InputStream being "alive." The connection to the telnet weather service (see below for link) is kept open. The goal is to get <em>all</em> lines coming from the server.*</p> <p>Building from the sample Apache <code>WeatherTelnet</code> <a href="http://svn.apache.org/repos/asf/commons/proper/net/tags/NET_1_0_0/src/java/examples/weatherTelnet.java" rel="nofollow">code</a>, I'm using <a href="http://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read%28%29" rel="nofollow">InputStream.read</a> to output the server results (inspired by a <a href="http://www.javamex.com/tutorials/io/character_stream_reader.shtml" rel="nofollow">javamex tutorial</a> idiom) one char at a time, using the <code>chars</code> method:</p> <pre><code>thufir@dur:~$ thufir@dur:~$ java -jar NetBeansProjects/Teln/dist/Teln.jar ------------------------------------------------------------------------------ * Welcome to THE WEATHER UNDERGROUND telnet service! * ------------------------------------------------------------------------------ * * * National Weather Service information provided by Alden Electronics, Inc. * * and updated each minute as reports come in over our data feed. * * * * **Note: If you cannot get past this opening screen, you must use a * * different version of the "telnet" program--some of the ones for IBM * * compatible PC's have a bug that prevents proper connection. * * * * comments: jmasters@wunderground.com * ------------------------------------------------------------------------------ Press Return to continue: ^Cthufir@dur:~$ </code></pre> <p>which is the desired output. However, reading the <code>InputStream</code> with a <code>BufferedReader</code> results in dropping the last line. (Or, at least, it's not printed to the console.) Bad output, using <code>lines</code> method:</p> <pre><code>thufir@dur:~$ thufir@dur:~$ java -jar NetBeansProjects/Teln/dist/Teln.jar ------------------------------------------------------------------------------ * Welcome to THE WEATHER UNDERGROUND telnet service! * ------------------------------------------------------------------------------ * * * National Weather Service information provided by Alden Electronics, Inc. * * and updated each minute as reports come in over our data feed. * * * * **Note: If you cannot get past this opening screen, you must use a * * different version of the "telnet" program--some of the ones for IBM * * compatible PC's have a bug that prevents proper connection. * * * * comments: jmasters@wunderground.com * ------------------------------------------------------------------------------ ^Cthufir@dur:~$ thufir@dur:~$ </code></pre> <p><code>StreamReader</code>code:</p> <pre><code>package teln; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class StreamReader { private StreamReader() { } StreamReader(InputStream inputStream) throws IOException { lines(inputStream); } private void chars(InputStream inputStream) throws IOException { do { char ch = (char) inputStream.read(); System.out.print(ch); } while (true); } private void lines(InputStream inputStream) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")); String line = ""; do { System.out.println(line); } while ((line = br.readLine()) != null); System.out.println(line); } } </code></pre> <p>Presumably that last line is "null" somehow? Can the logic be altered so that the final line is printed from within <code>lines</code> just as it is from <code>chars</code>?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload