1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-12-17 21:32:26 -05:00

Make sure the InputStream of the import file is always closed

This commit is contained in:
cketti 2011-10-04 00:48:43 +02:00
parent f2a3752930
commit 644571cfe5
2 changed files with 27 additions and 21 deletions

View File

@ -2,6 +2,7 @@
package com.fsck.k9.activity; package com.fsck.k9.activity;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -1281,18 +1282,18 @@ public class Accounts extends K9ListActivity implements OnItemClickListener, OnC
private List<String> mAccountUuids; private List<String> mAccountUuids;
private boolean mOverwrite; private boolean mOverwrite;
private String mEncryptionKey; private String mEncryptionKey;
private InputStream mInputStream; private Uri mUri;
private ImportResults mImportResults; private ImportResults mImportResults;
private ImportAsyncTask(Accounts activity, boolean includeGlobals, private ImportAsyncTask(Accounts activity, boolean includeGlobals,
List<String> accountUuids, boolean overwrite, String encryptionKey, List<String> accountUuids, boolean overwrite, String encryptionKey,
InputStream is) { Uri uri) {
super(activity); super(activity);
mIncludeGlobals = includeGlobals; mIncludeGlobals = includeGlobals;
mAccountUuids = accountUuids; mAccountUuids = accountUuids;
mOverwrite = overwrite; mOverwrite = overwrite;
mEncryptionKey = encryptionKey; mEncryptionKey = encryptionKey;
mInputStream = is; mUri = uri;
} }
@Override @Override
@ -1305,10 +1306,20 @@ public class Accounts extends K9ListActivity implements OnItemClickListener, OnC
@Override @Override
protected Boolean doInBackground(Void... params) { protected Boolean doInBackground(Void... params) {
try { try {
mImportResults = StorageImporter.importSettings(mContext, mInputStream, InputStream is = mContext.getContentResolver().openInputStream(mUri);
mEncryptionKey, mIncludeGlobals, mAccountUuids, mOverwrite); try {
mImportResults = StorageImporter.importSettings(mContext, is,
mEncryptionKey, mIncludeGlobals, mAccountUuids, mOverwrite);
} finally {
try {
is.close();
} catch (IOException e) { /* Ignore */ }
}
} catch (StorageImportExportException e) { } catch (StorageImportExportException e) {
Log.w(K9.LOG_TAG, "Exception during export", e); Log.w(K9.LOG_TAG, "Exception during import", e);
return false;
} catch (FileNotFoundException e) {
Log.w(K9.LOG_TAG, "Couldn't open import file", e);
return false; return false;
} }
return true; return true;
@ -1343,7 +1354,6 @@ public class Accounts extends K9ListActivity implements OnItemClickListener, OnC
private static class ListImportContentsAsyncTask extends ExtendedAsyncTask<Void, Void, Boolean> { private static class ListImportContentsAsyncTask extends ExtendedAsyncTask<Void, Void, Boolean> {
private Uri mUri; private Uri mUri;
private String mEncryptionKey; private String mEncryptionKey;
private InputStream mInputStream;
private ImportContents mImportContents; private ImportContents mImportContents;
private ListImportContentsAsyncTask(Accounts activity, Uri uri, String encryptionKey) { private ListImportContentsAsyncTask(Accounts activity, Uri uri, String encryptionKey) {
@ -1365,12 +1375,14 @@ public class Accounts extends K9ListActivity implements OnItemClickListener, OnC
try { try {
ContentResolver resolver = mContext.getContentResolver(); ContentResolver resolver = mContext.getContentResolver();
InputStream is = resolver.openInputStream(mUri); InputStream is = resolver.openInputStream(mUri);
mImportContents = StorageImporter.getImportStreamContents(mContext, is, try {
mEncryptionKey); mImportContents = StorageImporter.getImportStreamContents(mContext, is,
mEncryptionKey);
// Open another InputStream in the background. This is used later by ImportAsyncTask } finally {
mInputStream = resolver.openInputStream(mUri); try {
is.close();
} catch (IOException e) { /* Ignore */ }
}
} catch (StorageImportExportException e) { } catch (StorageImportExportException e) {
Log.w(K9.LOG_TAG, "Exception during export", e); Log.w(K9.LOG_TAG, "Exception during export", e);
return false; return false;
@ -1458,7 +1470,8 @@ public class Accounts extends K9ListActivity implements OnItemClickListener, OnC
dialog.dismiss(); dialog.dismiss();
Accounts activity = (Accounts) mActivity; Accounts activity = (Accounts) mActivity;
ImportAsyncTask importAsyncTask = new ImportAsyncTask(activity, includeGlobals, accountUuids, overwrite, mEncryptionKey, mInputStream); ImportAsyncTask importAsyncTask = new ImportAsyncTask(activity,
includeGlobals, accountUuids, overwrite, mEncryptionKey, mUri);
activity.mAsyncTask = importAsyncTask; activity.mAsyncTask = importAsyncTask;
importAsyncTask.execute(); importAsyncTask.execute();
} }
@ -1468,9 +1481,6 @@ public class Accounts extends K9ListActivity implements OnItemClickListener, OnC
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
dialog.dismiss(); dialog.dismiss();
try {
mInputStream.close();
} catch (Exception e) { /* Ignore */ }
} }
}); });
builder.show(); builder.show();

View File

@ -497,10 +497,6 @@ public class StorageImporter {
return imported; return imported;
} catch (Exception e) { } catch (Exception e) {
throw new StorageImportExportException(e); throw new StorageImportExportException(e);
} finally {
try {
inputStream.close();
} catch (Exception e) { /* Ignore */ }
} }
} }