diff --git a/app/src/main/java/protect/card_locker/Utils.java b/app/src/main/java/protect/card_locker/Utils.java index d98439e66..5bbaeeb08 100644 --- a/app/src/main/java/protect/card_locker/Utils.java +++ b/app/src/main/java/protect/card_locker/Utils.java @@ -481,6 +481,23 @@ public class Utils { return new File(context.getCacheDir() + "/" + name); } + public static File copyToTempFile(Context context, InputStream input, String name) { + File file = createTempFile(context, name); + try (FileOutputStream out = new FileOutputStream(file)) { + byte[] buf = new byte[4096]; + int len; + while ((len = input.read(buf)) != -1) { + out.write(buf, 0, len); + } + out.close(); + input.close(); + return file; + } catch (IOException e) { + Log.d("store temp file", "failed writing temp file, name: " + name); + return null; + } + } + public static String saveTempImage(Context context, Bitmap in, String name, Bitmap.CompressFormat format) { File image = createTempFile(context, name); try (FileOutputStream out = new FileOutputStream(image)) { diff --git a/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java b/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java index 52d521781..8a7761179 100644 --- a/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java +++ b/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java @@ -13,6 +13,8 @@ import org.apache.commons.csv.CSVRecord; import java.io.BufferedInputStream; import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @@ -39,7 +41,8 @@ import protect.card_locker.ZipUtils; * A header is expected for the each table showing the names of the columns. */ public class CatimaImporter implements Importer { - public void importData(Context context, SQLiteDatabase database, InputStream input, char[] password) throws IOException, FormatException, InterruptedException { + public void importData(Context context, SQLiteDatabase database, File inputFile, char[] password) throws IOException, FormatException, InterruptedException { + InputStream input = new FileInputStream(inputFile); InputStream bufferedInputStream = new BufferedInputStream(input); bufferedInputStream.mark(100); diff --git a/app/src/main/java/protect/card_locker/importexport/FidmeImporter.java b/app/src/main/java/protect/card_locker/importexport/FidmeImporter.java index b1e411a0f..b7b64560b 100644 --- a/app/src/main/java/protect/card_locker/importexport/FidmeImporter.java +++ b/app/src/main/java/protect/card_locker/importexport/FidmeImporter.java @@ -11,6 +11,8 @@ import org.apache.commons.csv.CSVParser; import org.apache.commons.csv.CSVRecord; import org.json.JSONException; +import java.io.File; +import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.StringReader; @@ -31,7 +33,8 @@ import protect.card_locker.Utils; * A header is expected for the each table showing the names of the columns. */ public class FidmeImporter implements Importer { - public void importData(Context context, SQLiteDatabase database, InputStream input, char[] password) throws IOException, FormatException, JSONException, ParseException { + public void importData(Context context, SQLiteDatabase database, File inputFile, char[] password) throws IOException, FormatException, JSONException, ParseException { + InputStream input = new FileInputStream(inputFile); // We actually retrieve a .zip file ZipInputStream zipInputStream = new ZipInputStream(input, password); @@ -70,6 +73,7 @@ public class FidmeImporter implements Importer { } zipInputStream.close(); + input.close(); } /** diff --git a/app/src/main/java/protect/card_locker/importexport/Importer.java b/app/src/main/java/protect/card_locker/importexport/Importer.java index 41f73df26..72c32d377 100644 --- a/app/src/main/java/protect/card_locker/importexport/Importer.java +++ b/app/src/main/java/protect/card_locker/importexport/Importer.java @@ -5,8 +5,8 @@ import android.database.sqlite.SQLiteDatabase; import org.json.JSONException; +import java.io.File; import java.io.IOException; -import java.io.InputStream; import java.text.ParseException; import protect.card_locker.FormatException; @@ -23,5 +23,5 @@ public interface Importer { * @throws IOException * @throws FormatException */ - void importData(Context context, SQLiteDatabase database, InputStream input, char[] password) throws IOException, FormatException, InterruptedException, JSONException, ParseException; + void importData(Context context, SQLiteDatabase database, File inputFile, char[] password) throws IOException, FormatException, InterruptedException, JSONException, ParseException; } diff --git a/app/src/main/java/protect/card_locker/importexport/MultiFormatImporter.java b/app/src/main/java/protect/card_locker/importexport/MultiFormatImporter.java index a5306dd33..78ee564b0 100644 --- a/app/src/main/java/protect/card_locker/importexport/MultiFormatImporter.java +++ b/app/src/main/java/protect/card_locker/importexport/MultiFormatImporter.java @@ -6,10 +6,14 @@ import android.util.Log; import net.lingala.zip4j.exception.ZipException; +import java.io.File; import java.io.InputStream; +import protect.card_locker.Utils; + public class MultiFormatImporter { private static final String TAG = "Catima"; + private static final String TEMP_ZIP_NAME = MultiFormatImporter.class.getSimpleName() + ".zip"; /** * Attempts to import data from the input stream of the @@ -42,9 +46,10 @@ public class MultiFormatImporter { String error = null; if (importer != null) { + File inputFile = Utils.copyToTempFile(context, input, TEMP_ZIP_NAME); database.beginTransaction(); try { - importer.importData(context, database, input, password); + importer.importData(context, database, inputFile, password); database.setTransactionSuccessful(); return new ImportExportResult(ImportExportResultType.Success); } catch (ZipException e) { @@ -59,6 +64,7 @@ public class MultiFormatImporter { error = e.toString(); } finally { database.endTransaction(); + inputFile.delete(); } } else { error = "Unsupported data format imported: " + format.name(); diff --git a/app/src/main/java/protect/card_locker/importexport/StocardImporter.java b/app/src/main/java/protect/card_locker/importexport/StocardImporter.java index 1aa90e3e0..df0d79451 100644 --- a/app/src/main/java/protect/card_locker/importexport/StocardImporter.java +++ b/app/src/main/java/protect/card_locker/importexport/StocardImporter.java @@ -16,6 +16,8 @@ import org.apache.commons.csv.CSVRecord; import org.json.JSONException; import org.json.JSONObject; +import java.io.File; +import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @@ -42,7 +44,7 @@ import protect.card_locker.ZipUtils; public class StocardImporter implements Importer { private static final String TAG = "Catima"; - public void importData(Context context, SQLiteDatabase database, InputStream input, char[] password) throws IOException, FormatException, JSONException, ParseException { + public void importData(Context context, SQLiteDatabase database, File inputFile, char[] password) throws IOException, FormatException, JSONException, ParseException { HashMap> loyaltyCardHashMap = new HashMap<>(); HashMap> providers = new HashMap<>(); @@ -62,6 +64,7 @@ public class StocardImporter implements Importer { throw new FormatException("Issue parsing CSV data", e); } + InputStream input = new FileInputStream(inputFile); ZipInputStream zipInputStream = new ZipInputStream(input, password); String[] providersFileName = null; @@ -245,6 +248,7 @@ public class StocardImporter implements Importer { } zipInputStream.close(); + input.close(); } private boolean startsWith(String[] full, String[] start, int minExtraLength) { diff --git a/app/src/main/java/protect/card_locker/importexport/VoucherVaultImporter.java b/app/src/main/java/protect/card_locker/importexport/VoucherVaultImporter.java index b0d103366..08454626b 100644 --- a/app/src/main/java/protect/card_locker/importexport/VoucherVaultImporter.java +++ b/app/src/main/java/protect/card_locker/importexport/VoucherVaultImporter.java @@ -12,6 +12,8 @@ import org.json.JSONException; import org.json.JSONObject; import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @@ -36,7 +38,8 @@ import protect.card_locker.Utils; * A header is expected for the each table showing the names of the columns. */ public class VoucherVaultImporter implements Importer { - public void importData(Context context, SQLiteDatabase database, InputStream input, char[] password) throws IOException, FormatException, JSONException, ParseException { + public void importData(Context context, SQLiteDatabase database, File inputFile, char[] password) throws IOException, FormatException, JSONException, ParseException { + InputStream input = new FileInputStream(inputFile); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8)); StringBuilder sb = new StringBuilder(); @@ -130,5 +133,6 @@ public class VoucherVaultImporter implements Importer { } bufferedReader.close(); + input.close(); } } \ No newline at end of file