Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

InputStream problem...

Thread view: 
icky - 23 Mar 2006 17:30 GMT
hi all!

can anybody help me on this, please!!!

i'm using the commons-net package to connect to a switch by telnet, and
using the readUntil method to read the telnet output... it looks like
this:

------------------------------ readUntil ---------------------------

public String readUntil( String pattern ) {
try {
 char lastChar = pattern.charAt( pattern.length() - 1 );
 StringBuffer sb = new StringBuffer();
 char ch = (char) in.read();
 int a = (-1);
 while( ch != (char) a ) {
 // System.out.print( ch );
   sb.append( ch );
   if( ch == lastChar ) {
     if( sb.toString().endsWith( pattern ) ) {
       return sb.toString();
     }
   }
   ch = (char) in.read();
 }
} catch(Exception e) {
 System.out.println(e.toString());
 e.printStackTrace();
}

return null;
}

-------------------------------------------------------------------------------
so... the problem is, that if the pattern does not match any of the
output,
the method gets blocked... on (ch = (char) in.read();) inside the
loop...
it keeps waiting for the InputStream.read()...
dont know how to overcome this... tried to use sockedTimeout... like...
setting the socket timeout for the connection to 4 seconds, and
catching
SocketTimeoutException....
but is there any wiser solution to this???
thanks in advance!! appreciate!!!
Roedy Green - 23 Mar 2006 17:47 GMT
>  char ch = (char) in.read();
>  int a = (-1);
>  while( ch != (char) a ) {

just use in int instead of a char. This is a c-ism that will read more
simply if you write it like this:

int ch;

while ( ( ch = in.read() ) >= 0 )
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.