Merge commit 'refs/merge-requests/22' of git://gitorious.org/f-droid/fdroidclient into merge-requests/22

This commit is contained in:
Ciaran Gultnieks 2013-04-12 14:34:28 +01:00
commit 3011c08bb2
5 changed files with 114 additions and 74 deletions

View File

@ -21,10 +21,10 @@
package org.fdroid.fdroid;
import android.util.Log;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
public class Downloader extends Thread {
@ -94,6 +94,8 @@ public class Downloader extends Thread {
public void run() {
InputStream input = null;
OutputStream output = null;
String apkname = curapk.apkName;
localfile = new File(DB.getDataPath(), apkname);
try {
@ -127,30 +129,23 @@ public class Downloader extends Thread {
status = Status.RUNNING;
}
BufferedInputStream getit = new BufferedInputStream(new URL(
remotefile).openStream(), 8192);
FileOutputStream saveit = new FileOutputStream(localfile);
BufferedOutputStream bout = new BufferedOutputStream(saveit, 1024);
byte data[] = new byte[1024];
int totalRead = 0;
int bytesRead = getit.read(data, 0, 1024);
while (bytesRead != -1) {
input = new URL(remotefile).openStream();
output = new FileOutputStream(localfile);
byte data[] = new byte[Utils.BUFFER_SIZE];
while (true) {
if (isInterrupted()) {
Log.d("FDroid", "Download cancelled!");
break;
}
bout.write(data, 0, bytesRead);
totalRead += bytesRead;
int count = input.read(data);
if (count == -1) {
break;
}
output.write(data, 0, count);
synchronized (this) {
progress = totalRead;
progress += count;
}
bytesRead = getit.read(data, 0, 1024);
}
bout.close();
getit.close();
saveit.close();
if (isInterrupted()) {
localfile.delete();
@ -182,6 +177,9 @@ public class Downloader extends Thread {
status = Status.ERROR;
return;
}
} finally {
Utils.closeQuietly(output);
Utils.closeQuietly(input);
}
Log.d("FDroid", "Download finished: " + localfile);

View File

@ -62,15 +62,16 @@ public class Hasher {
else if (file != null) {
byte[] buffer = new byte[1024];
int read = 0;
InputStream input = null;
try {
InputStream is = new BufferedInputStream(
new FileInputStream(file));
while ((read = is.read(buffer)) > 0) {
input = new BufferedInputStream(new FileInputStream(file));
while ((read = input.read(buffer)) > 0) {
digest.update(buffer, 0, read);
}
is.close();
} catch (Exception e) {
return hashCache = "";
} finally {
Utils.closeQuietly(input);
}
} else {
digest.update(array);

View File

@ -19,8 +19,6 @@
package org.fdroid.fdroid;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
@ -265,23 +263,16 @@ public class RepoXMLHandler extends DefaultHandler {
int totalBytes = 0;
int code = uc.getResponseCode();
if (code == 200) {
FileOutputStream f = ctx.openFileOutput(dest, Context.MODE_PRIVATE);
BufferedInputStream getit = new BufferedInputStream(
new URL(url).openStream());
BufferedOutputStream bout = new BufferedOutputStream(f, 1024);
byte data[] = new byte[1024];
int readed = getit.read(data, 0, 1024);
while (readed != -1) {
totalBytes += readed;
bout.write(data, 0, readed);
readed = getit.read(data, 0, 1024);
InputStream input = null;
OutputStream output = null;
try {
input = new URL(url).openStream();
output = ctx.openFileOutput(dest, Context.MODE_PRIVATE);
Utils.copy(input, output);
} finally {
Utils.closeQuietly(output);
Utils.closeQuietly(input);
}
bout.close();
getit.close();
f.close();
String et = uc.getHeaderField("ETag");
if (et != null)
@ -322,33 +313,33 @@ public class RepoXMLHandler extends DefaultHandler {
repo.lastetag, newetag);
if (code == 200) {
String jarpath = ctx.getFilesDir() + "/tempindex.jar";
JarFile jar;
JarFile jar = null;
JarEntry je;
Certificate[] certs;
try {
jar = new JarFile(jarpath, true);
je = (JarEntry) jar.getEntry("index.xml");
File efile = new File(ctx.getFilesDir(),
"/tempindex.xml");
InputStream in = new BufferedInputStream(
jar.getInputStream(je), 8192);
OutputStream out = new BufferedOutputStream(
new FileOutputStream(efile), 8192);
byte[] buffer = new byte[8192];
while (true) {
int nBytes = in.read(buffer);
if (nBytes <= 0)
break;
out.write(buffer, 0, nBytes);
InputStream input = null;
OutputStream output = null;
try {
input = jar.getInputStream(je);
output = new FileOutputStream(efile);
Utils.copy(input, output);
} finally {
Utils.closeQuietly(output);
Utils.closeQuietly(input);
}
out.flush();
out.close();
in.close();
certs = je.getCertificates();
} catch (SecurityException e) {
Log.e("FDroid", "Invalid hash for index file");
return "Invalid hash for index file";
}
Certificate[] certs = je.getCertificates();
} finally {
if (jar != null) {
jar.close();
}
}
if (certs == null) {
Log.d("FDroid", "No signature found in index");
return "No signature found in index";

View File

@ -18,10 +18,10 @@
package org.fdroid.fdroid;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Vector;
@ -290,21 +290,16 @@ public class UpdateService extends IntentService {
URL u = new URL(server + "/icons/" + app.icon);
HttpURLConnection uc = (HttpURLConnection) u.openConnection();
if (uc.getResponseCode() == 200) {
BufferedInputStream getit = new BufferedInputStream(
uc.getInputStream());
FileOutputStream saveit = new FileOutputStream(f);
BufferedOutputStream bout = new BufferedOutputStream(saveit,
1024);
byte data[] = new byte[1024];
int readed = getit.read(data, 0, 1024);
while (readed != -1) {
bout.write(data, 0, readed);
readed = getit.read(data, 0, 1024);
InputStream input = null;
OutputStream output = null;
try {
input = uc.getInputStream();
output = new FileOutputStream(f);
Utils.copy(input, output);
} finally {
Utils.closeQuietly(output);
Utils.closeQuietly(input);
}
bout.close();
getit.close();
saveit.close();
}
} catch (Exception e) {

View File

@ -0,0 +1,55 @@
/*
* Copyright (C) 2010-12 Ciaran Gultnieks, ciaran@ciarang.com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.fdroid.fdroid;
import java.io.Closeable;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
public final class Utils {
private Utils() {
}
public static final int BUFFER_SIZE = 4096;
public static void copy(InputStream input, OutputStream output)
throws IOException {
byte[] buffer = new byte[BUFFER_SIZE];
while (true) {
int count = input.read(buffer);
if (count == -1) {
break;
}
output.write(buffer, 0, count);
}
output.flush();
}
public static void closeQuietly(Closeable closeable) {
if (closeable == null) {
return;
}
try {
closeable.close();
} catch (IOException ioe) {
// ignore
}
}
}