InputStream problem...

I

icky

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!!!
 
R

Roedy Green

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 )
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top