import java.net.*; import java.io.*; public class IOManager { public static final int OkResult=220; public static final int ErrorResult=500; private final int MaxNetworkPacket=1024; private final int InvalidPort=-1; private SocketControl mTCPSocket=null; private boolean mIsLoggedIn=false; private StringArray mAckResponseStrings; public IOManager() { mAckResponseStrings=new StringArray(); mAckResponseStrings.insert("220"); } public void shutdown() { if(!mTCPSocket.isConnected())return; if(isLoggedIn())quit(); try{mTCPSocket.closeSocket();} catch(IOException exception){return;} } public boolean isConnected() { if(mTCPSocket==null||!mTCPSocket.isConnected())return false; return true; } private void isLoggedIn(boolean isLoggedIn) { mIsLoggedIn=isLoggedIn; } public boolean isLoggedIn() { return mIsLoggedIn; } public boolean connect(String strHost,int port) { int bytesRead; byte streamBytes[]; int clientPort; String strLine=new String(); if(isConnected())return false; streamBytes=new byte[MaxNetworkPacket]; InetAddress inetAddress=null; SocketControl socket=null; if(0!=strHost.length()) { try{inetAddress=InetAddress.getByName(strHost);} catch(UnknownHostException exception){return false;} try{socket=new SocketControl(inetAddress,port);} catch(IOException exception){return false;} catch(SecurityException exception){return false;} } else { try{socket=new SocketControl("",port);} catch(IOException exception){return false;} catch(SecurityException exception){return false;} } if(!socket.isConnected())return false; strLine=socket.readLine(); if(0==strLine.length())return false; clientPort=getPort(strLine); if(InvalidPort==clientPort)return false; socket.writeLine("OK"); try{socket.closeSocket();} catch(IOException exception){return false;} if(0!=strHost.length()) { try{inetAddress=InetAddress.getByName(strHost);} catch(UnknownHostException exception){return false;} try{mTCPSocket=new SocketControl(inetAddress,clientPort);} catch(IOException exception){return false;} } else { try{mTCPSocket=new SocketControl("",clientPort);} catch(IOException exception){return false;} } strLine=mTCPSocket.readLine(); return isConnected(); } public boolean advise(String strAddress,int port) { String strRequest=null; if(!isConnected())return false; strRequest=new String("ADVISE(")+new String(strAddress)+new String(",")+String.valueOf(port)+new String(")"); mTCPSocket.writeLine(strRequest); strRequest=mTCPSocket.readLine(); return true; } private String readLine(InputStream inputStream) { byte streamByte[]; String strLine=new String(); strLine=new String(); while(true) { streamByte=new byte[1]; try{inputStream.read(streamByte);} catch(IOException exception){break;} if(13==streamByte[0])continue; else if(10==streamByte[0])break; strLine+=new String(streamByte); } return strLine; } private boolean writeLine(String strLine,OutputStream outputStream) { if(0==strLine.length())return false; strLine+=new String("\r\n"); byte streamData[]=strLine.getBytes(); try{outputStream.write(streamData);} catch(IOException exception){return false;} return true; } private int getPort(String strLine) { String strPort; int strIndex; int port; strIndex=strLine.length()-1; port=InvalidPort; strPort=new String(); if(strIndex<0)return -1; while(true) { if(strIndex<0)break; if(' '==strLine.charAt(strIndex))break; strIndex--; } strIndex++; if(strIndex<0)return port; strPort=strLine.substring(strIndex); return Integer.parseInt(strPort); } private boolean isInResponse(String responseString,StringArray responseStrings) { if(0==responseString.length())return false; for(int index=0;index