-1

I was told that BufferedReader.readLine() is not a suitable method to read data from clientSocket, because it consideres a line as a String ending with \n or \r. However, my ending symbol for line is \r\n. What should I use insted of buffered reader?

Jakub Gruber
  • 745
  • 1
  • 11
  • 27
  • 2
    BufferedReader is just fine. Just reading the javadoc would tell: https://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html#readLine(): "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**. – JB Nizet Nov 12 '16 at 14:26
  • Yes, but if I get a line with `\r` or `\n` inside, BufferedReader will return me shorter line than I expected, right? – Jakub Gruber Nov 12 '16 at 14:27
  • 1
    If you are dealing with text, `\r` or `\n` means a new line. If you are not dealing with text, you should treat the data as bytes, forget the concept of "line" and use an `InputStream`. – SJuan76 Nov 12 '16 at 14:33
  • Ok, that will be probably better option. Please, answer the question so I can accept it – Jakub Gruber Nov 12 '16 at 14:34
  • 1
    Correct, but I agree 100% with SJuan76. Don't use a BufferedReader if you're not dealing with text. Text doesn't contain "lines" with a \r or a \n in the middle. – JB Nizet Nov 12 '16 at 14:36

1 Answers1

2

I was told that BufferedReader.readLine() is not a suitable method to read data from clientSocket, because it consideres a line as a String ending with \n or \r.

You were told wrong. See the Javadoc.

However, my ending symbol for line is \r\n. What should I use insted of buffered reader?

You don't need anything instead of BufferedReader. It will recognize that as a line terminator. See the Javadoc.

... if I get a line with \r or \n inside

There is no such thing.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Actually, there is such as thing. I call it line, but it is a message that can contain \r or \n and all I know is that it ends with \r\n – Jakub Gruber Nov 12 '16 at 16:26
  • 1
    There is only such a thing if you personally redefine what you personally mean by a line, as you have done. Out here in the real world, the concept of a line that contains line terminators is meaningless. – user207421 Nov 13 '16 at 08:55