More robust data downloading
This commit is contained in:
@@ -183,15 +183,25 @@ class DataDownloader extends Thread
|
|||||||
(new File( outFilesDir )).mkdirs();
|
(new File( outFilesDir )).mkdirs();
|
||||||
} catch( SecurityException e ) { };
|
} catch( SecurityException e ) { };
|
||||||
}
|
}
|
||||||
|
|
||||||
HttpResponse response = null;
|
|
||||||
String [] downloadUrls = Globals.DataDownloadUrl.split("[|]");
|
String [] downloadUrls = Globals.DataDownloadUrl.split("[|]");
|
||||||
int downloadUrlIndex = 0;
|
int downloadUrlIndex = 0;
|
||||||
|
|
||||||
|
downloading:
|
||||||
|
while(true)
|
||||||
|
{
|
||||||
|
|
||||||
|
HttpResponse response = null;
|
||||||
|
HttpGet request;
|
||||||
|
long totalLen;
|
||||||
|
CountingInputStream stream;
|
||||||
|
byte[] buf = new byte[16384];
|
||||||
|
|
||||||
while( downloadUrlIndex < downloadUrls.length && response == null )
|
while( downloadUrlIndex < downloadUrls.length && response == null )
|
||||||
{
|
{
|
||||||
System.out.println("Connecting to " + downloadUrls[downloadUrlIndex]);
|
System.out.println("Connecting to " + downloadUrls[downloadUrlIndex]);
|
||||||
Status.setText( "Connecting to " + downloadUrls[downloadUrlIndex] );
|
Status.setText( "Connecting to " + downloadUrls[downloadUrlIndex] );
|
||||||
HttpGet request = new HttpGet(downloadUrls[downloadUrlIndex]);
|
request = new HttpGet(downloadUrls[downloadUrlIndex]);
|
||||||
request.addHeader("Accept", "*/*");
|
request.addHeader("Accept", "*/*");
|
||||||
try {
|
try {
|
||||||
DefaultHttpClient client = new DefaultHttpClient();
|
DefaultHttpClient client = new DefaultHttpClient();
|
||||||
@@ -219,8 +229,7 @@ class DataDownloader extends Thread
|
|||||||
}
|
}
|
||||||
|
|
||||||
Status.setText( "Downloading data from " + Globals.DataDownloadUrl );
|
Status.setText( "Downloading data from " + Globals.DataDownloadUrl );
|
||||||
long totalLen = response.getEntity().getContentLength();
|
totalLen = response.getEntity().getContentLength();
|
||||||
CountingInputStream stream;
|
|
||||||
try {
|
try {
|
||||||
stream = new CountingInputStream(response.getEntity().getContent());
|
stream = new CountingInputStream(response.getEntity().getContent());
|
||||||
} catch( java.io.IOException e ) {
|
} catch( java.io.IOException e ) {
|
||||||
@@ -228,16 +237,11 @@ class DataDownloader extends Thread
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ZipInputStream zip = null;
|
ZipInputStream zip = new ZipInputStream(stream);
|
||||||
zip = new ZipInputStream(stream);
|
|
||||||
|
|
||||||
byte[] buf = new byte[16384];
|
|
||||||
|
|
||||||
ZipEntry entry = null;
|
|
||||||
|
|
||||||
while(true)
|
while(true)
|
||||||
{
|
{
|
||||||
entry = null;
|
ZipEntry entry = null;
|
||||||
try {
|
try {
|
||||||
entry = zip.getNextEntry();
|
entry = zip.getNextEntry();
|
||||||
} catch( java.io.IOException e ) {
|
} catch( java.io.IOException e ) {
|
||||||
@@ -253,10 +257,25 @@ class DataDownloader extends Thread
|
|||||||
} catch( SecurityException e ) { };
|
} catch( SecurityException e ) { };
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
OutputStream out = null;
|
OutputStream out = null;
|
||||||
path = getOutFilePath(entry.getName());
|
path = getOutFilePath(entry.getName());
|
||||||
|
|
||||||
|
try {
|
||||||
|
CheckedInputStream check = new CheckedInputStream( new FileInputStream(path), new CRC32() );
|
||||||
|
while( check.read(buf, 0, buf.length) > 0 ) {};
|
||||||
|
check.close();
|
||||||
|
if( check.getChecksum().getValue() != entry.getCrc() )
|
||||||
|
{
|
||||||
|
File ff = new File(path);
|
||||||
|
ff.delete();
|
||||||
|
throw new Exception();
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
} catch( Exception e )
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
out = new FileOutputStream( path );
|
out = new FileOutputStream( path );
|
||||||
} catch( FileNotFoundException e ) {
|
} catch( FileNotFoundException e ) {
|
||||||
@@ -295,12 +314,18 @@ class DataDownloader extends Thread
|
|||||||
try {
|
try {
|
||||||
CheckedInputStream check = new CheckedInputStream( new FileInputStream(path), new CRC32() );
|
CheckedInputStream check = new CheckedInputStream( new FileInputStream(path), new CRC32() );
|
||||||
while( check.read(buf, 0, buf.length) > 0 ) {};
|
while( check.read(buf, 0, buf.length) > 0 ) {};
|
||||||
|
check.close();
|
||||||
if( check.getChecksum().getValue() != entry.getCrc() )
|
if( check.getChecksum().getValue() != entry.getCrc() )
|
||||||
|
{
|
||||||
|
File ff = new File(path);
|
||||||
|
ff.delete();
|
||||||
throw new Exception();
|
throw new Exception();
|
||||||
|
}
|
||||||
} catch( Exception e )
|
} catch( Exception e )
|
||||||
{
|
{
|
||||||
Status.setText( "CRC32 check failed for file " + path );
|
Status.setText( "CRC32 check failed for file " + path );
|
||||||
return;
|
continue downloading; // Start download over from the same URL
|
||||||
|
//return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -333,6 +358,8 @@ class DataDownloader extends Thread
|
|||||||
};
|
};
|
||||||
|
|
||||||
initParent();
|
initParent();
|
||||||
|
break;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
private void initParent()
|
private void initParent()
|
||||||
|
|||||||
Reference in New Issue
Block a user