Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #38955

Re: readLine() and newline problem

From Martin Gregorie <martin@mydomain.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: readLine() and newline problem
Date 2019-05-10 10:22 +0000
Organization albasani.net
Message-ID <qb3jd3$v26$1@news.albasani.net> (permalink)
References <8beb1189-8953-4072-9b4b-0d54cf64eaa5@googlegroups.com> <qb37mg$oj8$1@dont-email.me> <e082e8d0-0d47-4728-a112-f0ab82d4eda1@googlegroups.com>

Show all headers | View raw


On Fri, 10 May 2019 01:29:43 -0700, mike wrote:

> Den fredag 10 maj 2019 kl. 09:02:49 UTC+2 skrev Daniele Futtorovic:
>> On 2019-05-10 08:16, mike wrote:
>> > The problem here is that some devices add a newline after "]]>]]>"
>> > which is the message separator. And of course some devices don't
>> > since it is not required in the RFC6242.
>> > 
>> > So when there is no newline from device then the above method will
>> > hang. Then I thought it would be easy to create a unit test that
>> > recreates the problem. One with a hello message with message
>> > separator and newline and one with only message separator. But
>> > problem is that I get no exception in the test without the newline.
>> > Any ideas what I am missing or how I can re-create the issue?
>> 
>> In your test, an EOF takes the place of the otherwise expected EOL and
>> makes it work.
>> 
>> As for the problem overall, in my view you definitely shouldn't be
>> using readLine() here. You should be reading with read(char[]) and keep
>> doing so until you encounter the separator.
>> 
>> --
>> DF.
> 
> So how can I update test to simulate my error?
> 
> Yes I am also thinking of using read(char []). I think it is more
> appropriate. The only thing that will be a bit tricky is to find my
> separator char.I have to check if we have ] then another ] so there will
> be many ifs.

Two suggestions:

1) Continue to use readLine() but use Scanner.findInLine() to recognise 
the end pattern and DON'T include a newline in the end pattern. 

2) Read everything into a String and then use a Scanner or 
String.indexOf(String.str) to find the end pattern and truncate the 
string at that point. 

Which one is better depends on how much unwanted stuff follows the 
terminator and how much time would be wasted reading it in and discarding 
it.  


-- 
Martin    | martin at
Gregorie  | gregorie dot org

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

readLine() and newline problem mike <mikaelpetterson@hotmail.com> - 2019-05-09 23:16 -0700
  Re: readLine() and newline problem Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2019-05-10 09:02 +0200
    Re: readLine() and newline problem mike <mikaelpetterson@hotmail.com> - 2019-05-10 01:29 -0700
      Re: readLine() and newline problem Martin Gregorie <martin@mydomain.invalid> - 2019-05-10 10:22 +0000
      Re: readLine() and newline problem Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2019-05-10 18:50 +0200

csiph-web