Search code examples
javareadlinebufferedreaderterminator

reading lines from java socket with inconsistent line terminators


I am using BufferedReader#readLine() to get text lines from a TCP/IP socket.

During the same session, my server sometimes sends lines terminated with \r\n and sometimes it send lines terminated just with \n.

This behavior is not under my control :(

The Javadoc for readLine() says:

"A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed."

Unfortunately, that does not seem to work (at least on Windows). It only "knows" to read lines ending with \r\n.

Any suggestion?

Thanks in advance, Adrian.


Solution

  • Wrote a simple socket client and tested against a netcat server. The issue is that the netcat server always sends in UTF-8 and the client cannot expect UTF-16 for example.

    Here is the client code:

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.Socket;
    import java.net.URI;
    import java.net.URISyntaxException;
    import java.net.UnknownHostException;
    
    public class LineReader {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            if (args.length < 2) {
                System.out.println("usage: java LineReader charset url");
                System.exit(1);
            }
    
            try {
                final String charset = args[0];
                final URI url = new URI(args[1]);
                final String host = url.getHost();
                final int port = url.getPort();
                final Socket socket = new Socket(host, port);
                final BufferedReader reader = new BufferedReader(
                        new InputStreamReader(socket.getInputStream(), charset));
                while (true) {
                    final String line = reader.readLine();
                    System.out.println(line);
                }
            } catch (URISyntaxException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
    }
    

    Bottom line: there is no issue with the readLine method.