1 2 29 30 package com.jcraft.jhttptunnel; 31 32 import java.io.*; 33 import javax.microedition.io.*; 34 import com.nttdocomo.io.*; 35 36 public class IOBoundDoJa{ 37 OutBoundDoJa obdj=null; 38 InBoundDoJa ibdj=null; 39 private String sessionid=null; 40 41 public IOBoundDoJa(){ 42 obdj=new OutBoundDoJa(); 43 ibdj=new InBoundDoJa(); 44 } 45 public OutBound getOutBound(){return obdj;} 46 public InBound getInBound(){return ibdj;} 47 48 class OutBoundDoJa extends OutBound{ 49 private InputStream in=null; 50 private OutputStream out=null; 51 private HttpConnection con=null; 52 private final byte[] _TUNNEL_DISCONNECT={(byte)0x47}; 53 54 private int count=0; 55 boolean connected=false; 56 57 public void connect() throws IOException{ 58 60 String host=getHost(); 61 int port=getPort(); 62 63 String uri="http://"+host+":"+port+"/index.html?crap=1"+"&count="+count; 64 if(sessionid!=null){ 65 uri=uri+"&SESSIONID="+sessionid; 66 } 67 con=(HttpConnection)Connector.open(uri, Connector.READ_WRITE, true); 69 count++; 70 con.setRequestMethod(HttpConnection.POST); 71 out=con.openOutputStream(); 72 sendCount=getContentLength(); 73 } 74 75 public synchronized void sendData(byte[] foo, int s, int l, boolean flush) throws IOException{ 76 if(foo!=null && l<=0) return; 78 79 if(con==null){ 80 connect(); 81 } 82 83 if(sendCount<=0){ 84 connect(); 85 } 86 87 int retry=2; 88 while(retry>0){ 89 try{ 90 if(l>0) 91 out.write(foo, s, l); 92 sendCount-=l; 93 94 out.close(); 96 out=null; 97 try{ 98 con.connect(); 99 } 100 catch(ConnectionException e){ 101 try{con.close();}catch(Exception ee){System.out.println(e);} 102 con=null; 103 if(foo==null){ connect(); 105 retry--; 106 continue; 107 } 108 ibdj.connected=false; 109 return; 110 } 111 112 sessionid=con.getHeaderField("x-SESSIONID"); 113 in=con.openInputStream(); 115 readData(); 116 118 121 return; 122 } 123 catch(IOException e){ 124 connect(); 125 } 126 retry--; 127 } 128 } 129 130 private void readData() throws IOException{ 131 if(con!=null){ 133 if(out!=null){ try{out.close(); out=null;}catch(IOException e){} } 134 long datalen=con.getLength(); 135 if(in!=null){ 137 try{ 138 while(true){ 139 int c=in.read(); 141 if(c==-1)break; 143 while(true){ 144 if(ibdj.space()>=1){ 145 break; 146 } 147 try{Thread.sleep(1000);}catch(Exception e){} 148 } 149 ibdj.push(c); 150 } 151 in.close(); in=null; 152 } 153 catch(IOException e){} 154 } 155 try{con.close();}catch(Exception e){System.out.println(e);} 156 con=null; 157 } 158 } 160 161 public void close() throws IOException{ 162 connected=false; 163 } 164 } 165 166 public class InBoundDoJa extends InBound{ 167 private InputStream in=null; 168 private HttpConnection con=null; 169 170 boolean connected=false; 171 public void connect() throws IOException{ 172 connected=true; 173 } 174 175 public int receiveData(byte[] buf, int s, int l) throws IOException{ 176 if(l<=0){ return -1; } 178 int retry=2; 179 while(connected){ 180 synchronized(this){ 181 int bl=be-bs; 182 if(bl>0){ 184 if(buf==null){ 185 if(bl>l){ bl=l; } 186 bs+=bl; 187 System.arraycopy(this.buf, bs, this.buf, 0, be-bs); 188 be=be-bs; 189 bs=0; 190 if(bl==l){ 191 return -1; } 193 l-=bl; 194 continue; 195 } 196 if(bl>l) bl=l; 197 System.arraycopy(this.buf, bs, buf, s, bl); 198 bs+=bl; 199 System.arraycopy(this.buf, bs, this.buf, 0, be-bs); 200 be=be-bs; 201 bs=0; 202 return bl; 203 } 204 } 205 if(retry>0){ 206 retry--; 207 obdj.sendData(null, 0, 0, true); 208 obdj.readData(); 209 if(be-bs>0){ continue; } 211 212 228 229 try{ Thread.sleep(5000); } 230 catch(Exception e){ } 231 continue; 232 233 } 234 else{ 235 return 0; 236 } 237 } 239 return -1; 240 } 241 242 byte[] buf=new byte[4096]; 244 int bs=0; 245 int be=0; 246 public synchronized int space(){ 247 return buf.length-be; 248 } 249 public synchronized void push(int c){ 250 buf[bs+be]=(byte)c; be++; 252 } 254 public void close() throws IOException{ 255 connected=false; 256 } 257 } 258 } 259 | Popular Tags |