77import java .net .ServerSocket ;
88import java .net .Socket ;
99import java .net .SocketAddress ;
10- import java .net .SocketException ;
1110import java .util .ArrayList ;
1211
1312import com .immortalplayer .HttpGetProxyUtils .ProxyRequest ;
1413import com .immortalplayer .HttpGetProxyUtils .ProxyResponse ;
1514
15+
1616import android .net .Uri ;
1717import android .util .Log ;
1818
@@ -29,12 +29,13 @@ public class HttpGetProxy{
2929 private SocketAddress serverAddress ;
3030 private ProxyResponse proxyResponse =null ;
3131 private String mUrl ;
32- private String mMediaFilePath , newPath ;
32+ private String mMediaFilePath , newPath , newPath1 ;
3333 private File file , file1 ;
3434 private Proxy proxy =null ;
3535 ArrayList <range > ranges = new ArrayList <range >();
3636 private boolean startProxy , error =false ;
3737 private Thread prox ;
38+
3839 /**
3940 * Initialize the proxy server, and start the proxy server
4041 */
@@ -69,29 +70,31 @@ public String getLocalURL() {
6970
7071 public void stopProxy ()
7172 {
72- startProxy =false ;
73+ startProxy =false ;
7374 try {
74- localServer .close ();
75+ if (localServer !=null )
76+ {localServer .close ();
77+ localServer =null ;}
7578 } catch (IOException e ) {
76- // TODO Auto-generated catch block
7779 e .printStackTrace ();
7880 }
7981 }
8082
81- public void setPaths (String dirPath , String file5 , String url , long MaxSize ,int maxnum )
83+ public void setPaths (String dirPath , String file5 , String url , int MaxSize ,int maxnum )
8284 {
8385 new File (dirPath ).mkdirs ();
84- Utils .asynRemoveBufferFile (dirPath , maxnum , MaxSize );
86+ long maxsize1 =MaxSize *1024 *1024 ;
87+ Utils .asynRemoveBufferFile (dirPath , maxnum , maxsize1 );
8588 mUrl =url ;
8689 mMediaFilePath = dirPath + "/" + file5 ;
87- file = new File (dirPath + "/" + file5 );
90+ file = new File (mMediaFilePath );
8891 file1 = new File (dirPath + "/-" + file5 );
8992 error =false ;
9093 }
9194
9295 public void startProxy ()
9396 {
94- startProxy =true ;
97+ startProxy =true ;
9598 Log .i (TAG , "1 start proxy" );
9699 prox =new Thread () {
97100 public void run () {
@@ -151,7 +154,7 @@ public void run() {
151154 HttpGetProxyUtils utils = null ;
152155 int bytes_read ;
153156 byte [] file_buffer = new byte [1024 ];
154-
157+ File file2 , file3 ;
155158 byte [] local_request = new byte [1024 ];
156159 byte [] remote_reply = new byte [1024 * 50 ];
157160 ProxyRequest request = null ;
@@ -173,7 +176,7 @@ public void run() {
173176 serverAddress = new InetSocketAddress (remoteHost , HTTP_PORT );
174177 utils = new HttpGetProxyUtils (sckPlayer , serverAddress );
175178 isExists = file .exists ();
176- if (mMediaFilePath !=newPath ) {error =false ; urlsize =0 ; ranges .clear (); ranges .trimToSize (); newPath =mMediaFilePath ;}
179+ if (mMediaFilePath !=newPath ) {error =false ; urlsize =0 ; ranges .clear (); ranges .trimToSize (); newPath =mMediaFilePath ; newPath1 = file1 . getAbsolutePath (); }
177180
178181 //Read from file
179182 if ((isExists ) || ((file1 .exists ()) && (error ==true )))
@@ -243,7 +246,7 @@ public void run() {
243246 }
244247 return ;
245248 }
246- error =false ;
249+ error =false ;
247250
248251 os = new RandomAccessFile (file1 , "rwd" );
249252 } else {// MediaPlayer's request is invalid
@@ -254,6 +257,9 @@ public void run() {
254257 // ------------------------------------------------------
255258 // The feedback network server sent to the MediaPlayer, network server -> Proxy -> MediaPlayer
256259 // ------------------------------------------------------
260+
261+ //sckServer.setSoTimeout(200); // this parameter is experimental
262+ //sckPlayer.setSoTimeout(200);
257263 while ((sckServer != null ) &&
258264 ((bytes_read = sckServer .getInputStream ().read (remote_reply )) != -1 ) &&
259265 (sckPlayer .isClosed ()==false ))
@@ -279,7 +285,7 @@ public void run() {
279285
280286
281287 // Send the binary data
282- if (proxyResponse ._other != null ) {
288+ if (proxyResponse ._other != null ) {
283289 utils .sendToMP (proxyResponse ._other );
284290 os .seek (proxyResponse ._currentPosition );
285291 os .write (proxyResponse ._other , 0 , proxyResponse ._other .length );
@@ -298,8 +304,9 @@ public void run() {
298304 if (os != null )
299305 {
300306 os .close ();
301-
302- if (isExists ==false ) {
307+ file2 =new File (newPath );
308+ file3 =new File (newPath1 );
309+ if (file2 .exists ()==false ) {
303310 range r =new range ();
304311 r .setstart (request ._rangePosition );
305312 r .setend (proxyResponse ._currentPosition );
@@ -317,7 +324,7 @@ public void run() {
317324 }
318325 }
319326 if (urlsize ==(h -1 )) {
320- file1 .renameTo (file );
327+ file3 .renameTo (file2 );
321328 }
322329 }
323330 }
@@ -328,4 +335,4 @@ public void run() {
328335 }
329336 }
330337 }
331- }
338+ }
0 commit comments