如果我程序阻塞在inputstream上的时候连接中断了怎么办?
我写的那个程序似乎就永远阻塞下去了...
我程序哪有问题么? private String getXMLFromInputStream(InputStream inStream) throws IOException{
try{
System.out.println("in getXMLFromInputStream");
StringBuffer sbInputXML=new StringBuffer();
FindByRegExp finder=new FindByRegExp();
String charset=DEFAULT_CHARSET;
boolean endOfXML=false;
boolean isXmlContent=false;
InputStreamReader inputStreamReader=new InputStreamReader(inStream,DEFAULT_CHARSET);
System.out.println("InputStreamReader");
BufferedReader reader=new BufferedReader(inputStreamReader);
System.out.println("BufferedReader");
String xmlCharset=null;
while(!endOfXML){
String strLine = null;
System.out.println("before readchar");
char[] end=new char[1];
reader.read(end);
System.out.println(end);
System.out.println("before readLine");
strLine = reader.readLine();
System.out.println("line: "+strLine);
if (strLine == null) {
return null;// break out loop
} else {
strLine = new String(strLine.getBytes(DEFAULT_CHARSET), charset);
// get CharSet of XML
if (xmlCharset == null) {
xmlCharset = finder.find("(?<=encoding\\=\").*(?=\"\\?\\>)", strLine);
if (xmlCharset != null) {
charset = xmlCharset;
}
}
// process head flag and end flag of message
if (strLine.indexOf(BEGIN_OF_XML) != -1) {
isXmlContent = true;
strLine = "";
} else if (strLine.indexOf(END_OF_XML) != -1) {
endOfXML = true;
isXmlContent = false;
} else if (strLine.indexOf(END_OF_CONNECTION) != -1) {
return END_OF_CONNECTION;// break out loop
}
// XML
if (isXmlContent) {
sbInputXML.append(strLine);
sbInputXML.append("\n");
}
}
}
String ret=sbInputXML.toString().trim();
if("".equals(ret.replaceAll("\n", ""))){
return null;
}else{
return ret;
}

} catch (SocketException se) {
// se.printStackTrace();
return null;
} }

解决方案 »

  1.   

    如果是阻塞的话 lz 可以用nio包中的非阻塞的流做操作
      

  2.   

    Socket  clientSocket=new  Socket(server_host,port);   
    clientSocket.setSoTimeout(30000);//三十秒为超时时间   
      其实无论抛出的异常是否唯一都应该中断连接   
      

  3.   

    贴错代码了....而且现在最恶心的是我设置了socket的超时也没用....我怀疑是bufferedinputstream的问题...
    private String getXMLFromInputStream(InputStream inStream) throws IOException{
    try{
    StringBuffer sbInputXML=new StringBuffer();
    FindByRegExp finder=new FindByRegExp();
    String charset=DEFAULT_CHARSET;
    boolean endOfXML=false;
    boolean isXmlContent=false;
    InputStreamReader inputStreamReader=new InputStreamReader(inStream,DEFAULT_CHARSET);
    BufferedReader reader=new BufferedReader(inputStreamReader);
    String xmlCharset=null;
    while(!endOfXML){
    String strLine = null;
    strLine = reader.readLine();
    System.out.println("line: "+strLine);
    if (strLine == null) {
    return null;// break out loop
    } else {
    strLine = new String(strLine.getBytes(DEFAULT_CHARSET), charset);
    // get CharSet of XML
    if (xmlCharset == null) {
    xmlCharset = finder.find("(?<=encoding\\=\").*(?=\"\\?\\>)", strLine);
    if (xmlCharset != null) {
    charset = xmlCharset;
    }
    }
    // process head flag and end flag of message
    if (strLine.indexOf(BEGIN_OF_XML) != -1) {
    isXmlContent = true;
    strLine = "";
    } else if (strLine.indexOf(END_OF_XML) != -1) {
    endOfXML = true;
    isXmlContent = false;
    } else if (strLine.indexOf(END_OF_CONNECTION) != -1) {
    return END_OF_CONNECTION;// break out loop
    }
    // XML
    if (isXmlContent) {
    sbInputXML.append(strLine);
    sbInputXML.append("\n");
    }
    }
    }
    String ret=sbInputXML.toString().trim();
    if("".equals(ret.replaceAll("\n", ""))){
    return null;
    }else{
    return ret;
    }

    } catch (SocketException se) {
    // se.printStackTrace();
    return null;
    } }
      

  4.   

    我另外一个用inputstream直接写的那个就可以在timeout的时候抛异常..private String getXMLFromInputStream(InputStream inStream) throws IOException{
    ByteBuffer buf = ByteBuffer.allocate(1024);
    StringBuffer sbInputXML=new StringBuffer();
    FindByRegExp finder=new FindByRegExp();
    String charset=DEFAULT_CHARSET;
    boolean endOfXML=false;
    boolean isXmlContent=false;
    byte[] buffer=new byte[1024];
    while(!endOfXML){
    int inbyte=inStream.read(buffer);
    buf.put((byte)inbyte);
    if(inbyte=='\n'){
    buf.flip();
    byte[] line=new byte[buf.limit()];
    buf.get(line);
    String strLine=null;
    strLine=new String(line,charset);
    if(strLine.indexOf(BEGIN_OF_XML)!=-1){
    isXmlContent=true;
    strLine="";
    }
    if(strLine.indexOf(END_OF_CONNECTION)!=-1){
    return END_OF_CONNECTION;
    }
    String xmlCharset=finder.find("(?<=encoding\\=\").*(?=\"\\?\\>)", strLine);
    if(xmlCharset!=null){
    charset=xmlCharset;
    }
    if(strLine.indexOf(END_OF_XML)!=-1){
    endOfXML=true;
    isXmlContent=false;
    }
    if(isXmlContent){
    sbInputXML.append(strLine);
    }
    buf.clear();
    }
    if(inbyte==-1&&!endOfXML){
    return null;
    }
    }
    return sbInputXML.toString();
    }