Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Readline but '\n' is not the seprator  RSS feed

 
Amit A. Patil
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I want to read lines from a Socket.The trouble is the lines are seprated by a Ctrl character and not '\n'.
I checked BufferedReader but it supports '\n' separated lines.The other option is to use read which fills char[] and then parse for the delimiter.

Any ideas??
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you'll need to examine each character yourself, looking for the separators. You could encapsulate all of that in a class with your own readLine() method, so that your actual application could just call your readLine(). All the messy stuff would be hidden in your utility class.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you get Scanner to read to the next delimiter?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stan James:
Could you get Scanner to read to the next delimiter?


(/me goes to look at API...)

Man, I need to get on the ball with this Java 5 thing. Yep, in Java 5 you can use java.util.Scanner to do this nicely.
 
Amit A. Patil
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any Performance benefit in using the Scanner?
Basically i have a stream of messages and i thought of using Some Ctrl character as message seprator.To ensure that data doesnt have the Seperator i thought i will use a Ctrl Character say ASCII value 1.

So i will use
//Reads from socket,A buffered Stream Reader
while( (ch=read())!=SEPERATOR) {
StringBuff.append(ch);
}
process(StringBuff)
Any way i can optimize this?
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Performance? Over 99% of the time, that code will be waiting for a byte to come into the socket. Don't worry about optimizing the other less than 1%.
 
Amit A. Patil
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using a BufferedReader,so most of times i will be reading from Buffer and the read will not be waiting most of times.
 
Amit A. Patil
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any performance hit as i am appending to StringBuffer again and again?
Shall i redesign to read 1024 bytes and then parse on the control char?
 
Amit A. Patil
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somebody PLease answer
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do the simplest thing that works, make the code as sensible and easy to read as possible. Later if it seems slow, add some profiling and make dead certain you know exactly where and why it is slow. Then worry about what is faster.

Reading a chunk (line) at a time and appending to a StringBuffer will very likely be plenty fast enough.
 
Amit A. Patil
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a ton!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!