Skip to content

Instantly share code, notes, and snippets.

@shriduttkothari
Created August 22, 2013 13:22
Show Gist options
  • Save shriduttkothari/6307073 to your computer and use it in GitHub Desktop.
Save shriduttkothari/6307073 to your computer and use it in GitHub Desktop.
On a valid RomInfo Object received, Start the Downloader AsyncTask to download the update zip file from server, the doInBackground() method will have pseudo code as follows:
private int doInBackground(Void... notused) {
final File dest = new File("/cache/update.zip");
InputStream is = null;
OutputStream os = null;
try {
URL getUrl = new URL(romInfo.url);
destFile.getAbsolutePath());
URLConnection conn = getUrl.openConnection();
conn.connect();
is = new BufferedInputStream(conn.getInputStream());
os = new FileOutputStream(destFile);
byte[] buf = new byte[4096];
int nRead = -1;
int totalRead = 0;
while ((nRead = is.read(buf)) != -1) {
os.write(buf, 0, nRead);
totalRead += nRead;
}
// Verify Md5 checksum here
} catch (Exception e) {
e.printStackTrace();
destFile.delete();
} finally {
if (is != null) {
try { is.close(); }
catch (Exception e) { }
}
if (os != null) {
try { os.flush(); os.close(); }
catch (Exception e) { }
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment