Created
September 27, 2010 00:44
-
-
Save koush/598448 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void downloadFile(String url, String referer) throws Exception { | |
Log.i(LOGTAG, "Attempting download of " + url); | |
URL downloadUrl = new URL(url); | |
final File filePath = Helper.computeFilePath(downloadUrl); | |
Log.i(LOGTAG, "Downloading: " + downloadUrl); | |
URLConnection conn = downloadUrl.openConnection(); | |
if (!Helper.isJavaScriptNullOrEmpty(referer)) | |
conn.addRequestProperty("Referer", referer); | |
final int totalSize = conn.getContentLength(); | |
DataInputStream input = new DataInputStream(conn.getInputStream()); | |
final File tmpFilePath = new File(filePath.toString() + ".tmp"); | |
filePath.getParentFile().mkdirs(); | |
FileOutputStream fout = new FileOutputStream(tmpFilePath); | |
byte[] buffer = new byte[200000]; | |
int read = 0; | |
int total = 0; | |
int lastUpdate = 0; | |
while ((read = input.read(buffer)) != -1) { | |
if (mCancel) | |
return; | |
total += read; | |
final double fraction = (double) total / (double) totalSize; | |
final int progress = (int) (fraction * 100); | |
if (totalSize != -1 && total > lastUpdate + 100000) { | |
fireProgressNotification(progress); | |
lastUpdate = total; | |
} | |
fout.write(buffer, 0, read); | |
} | |
fout.close(); | |
input.close(); | |
if (!Helper.isValidZip(tmpFilePath.getAbsolutePath())) | |
throw new Exception(getString(R.string.corrupt)); | |
tmpFilePath.renameTo(filePath); | |
Log.i(LOGTAG, "Download complete. Bytes received: " + total); | |
romPackage.mFiles.add(filePath.getAbsolutePath()); | |
Log.i(LOGTAG, "Download complete."); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment