• Post Reply Bookmark Topic Watch Topic
  • New Topic

Extending Buffered Reader

 
Kevin Brooks
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have Extended the Buffered Reader class. The purpose was to create a Readline method that will time out. The reason for this is I using Buffered reader to communicate with a device over the network, if the device does not respond I do not want my program to be hung up. Any way below is the code for my method. Is there a better way to check for the time out period? Or maybe there is already a class and/or method that does what I am looking for?

Thanks,
Kevin

/***********************************************
* This function is called to read a line of text from
* a buffered reader. It will read until a Carriage Return and/or
* line feed is read, or until the time limit has expired.
* @param tlimit The amount of time in milliseconds to wait for a end of line
* character.
* @param value This variable will be filled with the characters collected during
* the reading process. It will not contain the end of line character(s)
* @return The return value is boolean indicating if a end of line character was read.
*/
public boolean readLine(int tlimit, StringBuffer value) throws IOException
{
boolean retval = false;

if(lastCharRead != 0 && lastCharRead != 13 && lastCharRead != 10){
value.append(lastCharRead);
}

lastCharRead = 0;
Date finish = new Date();

finish.setTime(finish.getTime() + (tlimit / 10));
Date current = new Date();

while(current.before(finish) && !retval){
if(super.ready()){
char cread = (char) super.read();
if(cread != 10 && cread != 13){
value.append(cread);
}
else{
retval = true;
if(cread == 13 && super.ready()){
lastCharRead = (char) super.read();
lastCharRead = (lastCharRead == 10 ? 0 : lastCharRead);
}
}
}

current = new Date();

}

return retval;
}
 
Paul Clapham
Sheriff
Posts: 21882
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the code you posted won't do that anyway, it will just block when it has to wait for data to arrive from the network partner.

However if your requirement is for the connection to time out if no data is received within a certain period, it would be better to just set that timeout on the socket, rather than trying to hack the system at a completely different level. Then you just write ordinary text-reading code which is prepared for IOExceptions to be thrown when the timeouts occur.

Also, if you don't want your application to be hung up by code waiting for input, it's usual in networking applications to run that code in a separate thread.
 
Kevin Brooks
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Well, the code you posted won't do that anyway, it will just block when it has to wait for data to arrive from the network partner.

<Kevin> Am I wrong in thinking the "if(super.ready())" will prevent my code from attempting to read a character if there is none?

However if your requirement is for the connection to time out if no data is received within a certain period, it would be better to just set that timeout on the socket, rather than trying to hack the system at a completely different level. Then you just write ordinary text-reading code which is prepared for IOExceptions to be thrown when the timeouts occur.

<Kevin> This may work. However, wouldn't the Socket shut down if there is a timeout? If so this would not be a good solution since I have to send the device a command after I finish even if I did not get all the data I was expecting. Also the other Device sends Keep Alive packets to me so wouldn't this prevent the timeout at the Socket level?

Also, if you don't want your application to be hung up by code waiting for input, it's usual in networking applications to run that code in a separate thread.

<Kevin>
I am already running it in a separate thread, so I mislead you a bit. The whole application won't hang up, but that thread would if Data doesn't come across.
 
Paul Clapham
Sheriff
Posts: 21882
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin Brooks wrote:Am I wrong in thinking the "if(super.ready())" will prevent my code from attempting to read a character if there is none?


Sort of.

The API documentation wrote:Note that returning false does not guarantee that the next read will block.


Kevin Brooks wrote:However, wouldn't the Socket shut down if there is a timeout?


Nope.

The API documentation wrote:If the timeout expires, a java.net.SocketTimeoutException is raised, though the Socket is still valid.


Kevin Brooks wrote:Also the other Device sends Keep Alive packets to me so wouldn't this prevent the timeout at the Socket level?


Let's assume it would prevent the timeout, for argument's sake. Then your BufferedReader would be reading them and it wouldn't timeout either.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!