1 2 29 30 package com.jcraft.jhttptunnel; 31 32 import java.net.*; 33 import java.io.*; 34 35 public class InBoundURL extends InBound{ 36 private InputStream in=null; 37 private URLConnection con=null; 38 39 public void connect() throws IOException{ 40 close(); 41 String host=getHost(); 42 int port=getPort(); 43 URL url=new URL("http://"+host+":"+port+"/index.html?crap=1"); 44 con=url.openConnection(); 45 con.setUseCaches(false); 46 con.setDoOutput(false); 47 con.connect(); 48 in=con.getInputStream(); 49 } 50 public int receiveData(byte[] buf, int s, int l) throws IOException{ 51 if(l<=0){ System.out.println("receiveData: "+l); } 53 if(l<=0) return -1; 54 while(true){ 55 try{ 57 if(buf==null){ 58 if(l<=0) return -1; 59 long bar=in.skip((long)l); 60 l-=bar; 61 continue; 62 } 63 int i=in.read(buf, s, l); 64 if(i>0){ 65 return i; 66 } 67 connect(); 70 } 71 catch(SocketException e){ 72 throw e; 73 } 74 catch(IOException e){ 75 throw e; 77 } 79 } 80 } 81 public void close() throws IOException{ 82 if(con!=null){ 83 if(in!=null){ try{in.close();}catch(IOException e){} } 84 con=null; 85 } 86 } 87 } 88 | Popular Tags |