import java.util.*; import java.net.*; import java.io.*; public class test { static BufferedInputStream bif; static int blksize; static String thisResponse="nothing"; public static void main(String args[]) { // if(connect("http://osprey4.npac.syr.edu:8001/servlets/PostTest?where=here&what=that")) { if(connect("http://www.npac.syr.edu")) { while(!thisResponse.equals("EOF")){ System.out.println(thisResponse); thisResponse=getBlock(); } closeConnection(); } } public static boolean connect(String UrlString) { boolean status=true; UrlString += "what=what&where=where"; try { URL url = new URL(UrlString); InputStream is = url.openStream(); bif = new BufferedInputStream(is); } catch (Exception e) { System.out.println("cannot connect to "+UrlString); System.out.println(e.toString()); status=false; } return status; } public static void closeConnection() { System.out.println("close connection"); try { if(bif != null) bif.close(); else System.out.println("bif is already closed"); } catch (Exception e) { System.out.println("cannot close input stream"); System.out.println(e.toString()); } bif=null; } public void setBlockSize(int size) { blksize=size; } public static String getBlock() { String response=""; byte[] a = null; byte[] b = new byte[blksize]; try { if(bif==null) System.out.println("bif is null"); int icount = bif.read(b); System.out.println("icount="+icount); if(icount < 1) { response="EOF"; } else { if(icount == blksize) a=b; //buffer filled else { a = new byte[icount]; for(int i =0; i < icount; i++) a[i]=b[i]; } response=new String(a); } } catch(Exception e) { System.out.println("IO Exception in getBlock"); System.out.println(e); } /* you may want to modify a response=checkString(response,TargetHost); */ if(response==null) response="null"; return response; } public byte[] getBinaryBlock() { byte[] a = null; byte[] b = new byte[blksize]; try { if(bif==null) System.out.println("bif is null"); int icount = bif.read(b); System.out.println("icount="+icount); if(icount == -1) { a = new byte[0]; } else { if(icount == blksize) a=b; //buffer filled else { a = new byte[icount]; for(int i =0; i < icount; i++) a[i]=b[i]; } } } catch(Exception e) { System.out.println("IO Exception in getBlock"); System.out.println(e); } return a; } private String checkString(String string, String TargetServer) { /* replaces relative references to absolute ones for example: to */ String newString = removeAbsolute(string,"HREF",TargetServer); newString = removeAbsolute(newString,"href",TargetServer); newString = removeAbsolute(newString,"SRC",TargetServer); newString = removeAbsolute(newString,"src",TargetServer); return newString; } private String removeAbsolute(String string, String Tag, String TargetServer){ String newstring = null; String temp,temp1,temp2; temp1=""; int idx=string.indexOf(Tag); if(idx<0) return string; while(idx>-1) { newstring=string.substring(0,idx); temp=string.substring(idx); /* now temp starts with Tag: move Tag to newstring */ int idx2 = temp.indexOf("=")+1; newstring += temp.substring(0,idx2); temp = temp.substring(idx2); /* now temp starts after = */ if(temp.startsWith("\"/") || temp.startsWith(" \"/") || temp.startsWith("/") || temp.startsWith(" /")) { temp = "http://"+TargetServer+temp; } newstring += temp; temp1 += newstring; string = temp; string=string.substring(Tag.length()); idx=string.indexOf(Tag); } return temp1; } public String SayHello(){ return "Hello from Charon!"; } public synchronized void waitfordata() { try { System.out.println("wait"); wait(100); } catch(Exception e) {}; } }