Use helper functions where appropriate.
This commit is contained in:
parent
7b773f94f9
commit
a09587c7e2
@ -48,6 +48,7 @@ import org.xmlpull.v1.XmlPullParserException;
|
|||||||
import java.io.BufferedInputStream;
|
import java.io.BufferedInputStream;
|
||||||
import java.io.Closeable;
|
import java.io.Closeable;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileDescriptor;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -162,16 +163,19 @@ public final class Utils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static boolean copy(File inFile, File outFile) {
|
public static boolean copy(File inFile, File outFile) {
|
||||||
|
InputStream input = null;
|
||||||
|
OutputStream output = null;
|
||||||
try {
|
try {
|
||||||
InputStream input = new FileInputStream(inFile);
|
input = new FileInputStream(inFile);
|
||||||
OutputStream output = new FileOutputStream(outFile);
|
output = new FileOutputStream(outFile);
|
||||||
Utils.copy(input, output);
|
Utils.copy(input, output);
|
||||||
output.close();
|
|
||||||
input.close();
|
|
||||||
return true;
|
return true;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log.e(TAG, "I/O error when copying a file", e);
|
Log.e(TAG, "I/O error when copying a file", e);
|
||||||
return false;
|
return false;
|
||||||
|
} finally {
|
||||||
|
closeQuietly(output);
|
||||||
|
closeQuietly(input);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,6 +10,10 @@ import android.database.Cursor;
|
|||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.ParcelFileDescriptor;
|
import android.os.ParcelFileDescriptor;
|
||||||
|
import android.text.TextUtils;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import org.fdroid.fdroid.Utils;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileDescriptor;
|
import java.io.FileDescriptor;
|
||||||
@ -49,7 +53,7 @@ public class AsyncDownloaderFromAndroid implements AsyncDownloader {
|
|||||||
this.listener = listener;
|
this.listener = listener;
|
||||||
this.localFile = localFile;
|
this.localFile = localFile;
|
||||||
|
|
||||||
if (downloadTitle == null || downloadTitle.trim().length() == 0) {
|
if (TextUtils.isEmpty(downloadTitle)) {
|
||||||
this.downloadTitle = remoteAddress;
|
this.downloadTitle = remoteAddress;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,18 +101,15 @@ public class AsyncDownloaderFromAndroid implements AsyncDownloader {
|
|||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
private void copyFile(FileDescriptor inputFile, File outputFile) throws IOException {
|
private void copyFile(FileDescriptor inputFile, File outputFile) throws IOException {
|
||||||
InputStream is = new FileInputStream(inputFile);
|
InputStream input = null;
|
||||||
OutputStream os = new FileOutputStream(outputFile);
|
OutputStream output = null;
|
||||||
byte[] buffer = new byte[1024];
|
|
||||||
int count = 0;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
while ((count = is.read(buffer, 0, buffer.length)) > 0) {
|
input = new FileInputStream(inputFile);
|
||||||
os.write(buffer, 0, count);
|
output = new FileOutputStream(outputFile);
|
||||||
}
|
Utils.copy(input, output);
|
||||||
} finally {
|
} finally {
|
||||||
os.close();
|
Utils.closeQuietly(output);
|
||||||
is.close();
|
Utils.closeQuietly(input);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user