62,614
社区成员
发帖
与我相关
我的任务
分享
private boolean waitForLoginFinish() throws IOException
{
StringBuffer sb = new StringBuffer();
char ch;
long start=System.currentTimeMillis() ;
System.out.println("start start");
while(System.currentTimeMillis() - start < timeout){
int i = -1;
while (in.available() >= 0) {
//long s=System.currentTimeMillis() ;
//while(System.currentTimeMillis() - s < 10000){
i=in.read();
if(i == -1) {
return false;
}
ch = (char) i;
sb.append(ch);
System.out.println(sb.toString());
if (sb.indexOf(prompt) != -1) {
return true;
}else if(sb.indexOf(env.getLoginSuccPrompt())!=-1){
return false;
}else {
try {
//这里是防止网络慢的时候,in.available()经常为0,导致大量循环引起过多的indexOf
Thread.sleep(100);
} catch (Exception e) {}
}
}
System.out.println("end read");
}
return false;
public class MyOwnStream2 extends InputStream
{
protected byte[] data;
protected int ptr = 0;// 读取到的位置
private int mark = 0;// 标记位置
public MyOwnStream2(byte[] b)
{
this.data = b;
}
@Override
public int read() throws IOException
{
return ptr < data.length ? data[ptr++] : -1;
}
@Override
public int read(byte[] b, int off, int len) throws IOException
{
if (this.ptr >= data.length || len < 0)
{
return -1;
}
if (this.ptr + len > data.length)
{
len = data.length - this.ptr;
}
if (len == 0)
{
return 0;
}
System.arraycopy(data, ptr, b, off, len);
ptr += len;
return len;
}
/**
* 剩余未读的长度
*/
@Override
public int available() throws IOException
{
return data.length - ptr;
}
/**
* 关闭,简单实现使读取位置等于文件长度
*/
@Override
public void close() throws IOException
{
ptr = data.length;
}
/**
* 标记位置,下次可以跳到这个位置
*/
@Override
public synchronized void mark(int readlimit)
{
this.mark = readlimit;
}
/**
* 跳回标记位置
*/
@Override
public synchronized void reset() throws IOException
{
if (mark < 0 || mark > data.length)
{
throw new IOException("the position is not valid");
}
ptr = mark;
}
/**
* 是否支持标记,这里强制可以
*/
@Override
public boolean markSupported()
{
return true;
}
}