/
IOManager.java
102 lines (87 loc) · 3.5 KB
/
IOManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package javinator9889.securepass.io;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import com.google.android.gms.common.util.IOUtils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import javinator9889.securepass.R;
import javinator9889.securepass.util.cipher.PasswordCipher;
import javinator9889.securepass.util.cipher.PasswordSaver;
/**
* Created by Javinator9889 on 26/03/2018.
* Manage inputs-outputs from application
*/
public class IOManager {
private Context activityContext;
private File filesCache;
private IOManager(@NonNull Context activityContext) {
this.activityContext = activityContext;
this.filesCache = activityContext.getCacheDir();
}
@NonNull
public static IOManager newInstance(Context activityContext) {
return new IOManager(activityContext);
}
public List<String> loadSQLScript() throws IOException {
List<String> result = new ArrayList<>(5);
int[] sqlScripts = new int[]{R.raw.create_category, R.raw.create_entry, R.raw.create_qrcode,
R.raw.create_security_code, R.raw.create_field};
for (int sqlScript : sqlScripts) {
InputStream sqlScriptInputFile = this.activityContext.getResources()
.openRawResource(sqlScript);
StringBuilder builder = new StringBuilder();
BufferedReader sqlStringsInFile = new BufferedReader(
new InputStreamReader(sqlScriptInputFile));
String currentLine;
while ((currentLine = sqlStringsInFile.readLine()) != null)
builder.append(currentLine).append("\n");
result.add(builder.toString());
}
return result;
}
public void storePassword(@NonNull String userPassword) {
PasswordCipher passwordSaver = PasswordSaver.instantiate(activityContext);
passwordSaver.putPassword(userPassword);
}
@Nullable
public String readPassword() {
PasswordCipher passwordReader = PasswordSaver.instantiate(activityContext);
return passwordReader.getPassword();
}
public void writeDownloadedClass(@NonNull InputStream from) {
String filename = filesCache.getAbsolutePath() + "/class.bck";
try {
OutputStream to = new FileOutputStream(filename);
IOUtils.copyStream(from, to);
} catch (IOException e) {
Log.e("Copy IO", "There was an error while trying to copy the InputStream" +
" to an OutputStream. Full trace: ", e);
}
}
public InputStream readDownloadedClass() {
String filename = filesCache.getAbsolutePath() + "/class.bck";
try {
return new FileInputStream(filename);
} catch (FileNotFoundException e) {
Log.e("Read class", "File not found when trying to recover. Full trace: ", e);
return null;
}
}
public void deleteDownloadedClass() {
String filename = filesCache.getAbsolutePath() + "/class.bck";
File fileToDelete = new File(filename);
if (!fileToDelete.delete())
Log.e("Delete IO", "There was an error while trying to delete class.bck");
}
}