Skip to content

Commit 05f964c

Browse files
Improve ICU data expansion process
Always close streams when expanding ICU dat file. Delete dat file when exception occurs expanding ICU zip. Synchronize outer calling method loadLibs to prevent errors with multiple threads operating on the SQLCipher Java API.
1 parent 7e704f0 commit 05f964c

File tree

1 file changed

+39
-23
lines changed

1 file changed

+39
-23
lines changed

src/net/sqlcipher/database/SQLiteDatabase.java

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import java.io.File;
2727
import java.io.FileOutputStream;
28+
import java.io.IOException;
2829
import java.io.OutputStream;
2930
import java.lang.ref.WeakReference;
3031
import java.text.SimpleDateFormat;
@@ -85,36 +86,51 @@ public void changePassword(char[] password) throws SQLiteException {
8586
}
8687

8788
private static void loadICUData(Context context, File workingDir) {
88-
89+
OutputStream out = null;
90+
ZipInputStream in = null;
91+
File icuDir = new File(workingDir, "icu");
92+
File icuDataFile = new File(icuDir, "icudt46l.dat");
93+
try {
94+
if(!icuDir.exists()) icuDir.mkdirs();
95+
if(!icuDataFile.exists()) {
96+
in = new ZipInputStream(context.getAssets().open("icudt46l.zip"));
97+
in.getNextEntry();
98+
out = new FileOutputStream(icuDataFile);
99+
byte[] buf = new byte[1024];
100+
int len;
101+
while ((len = in.read(buf)) > 0) {
102+
out.write(buf, 0, len);
103+
}
104+
}
105+
}
106+
catch (Exception ex) {
107+
Log.e(TAG, "Error copying icu dat file", ex);
108+
if(icuDataFile.exists()){
109+
icuDataFile.delete();
110+
}
111+
throw new RuntimeException(ex);
112+
}
113+
finally {
89114
try {
90-
File icuDir = new File(workingDir, "icu");
91-
if(!icuDir.exists()) icuDir.mkdirs();
92-
File icuDataFile = new File(icuDir, "icudt46l.dat");
93-
if(!icuDataFile.exists()) {
94-
ZipInputStream in = new ZipInputStream(context.getAssets().open("icudt46l.zip"));
95-
in.getNextEntry();
96-
OutputStream out = new FileOutputStream(icuDataFile);
97-
byte[] buf = new byte[1024];
98-
int len;
99-
while ((len = in.read(buf)) > 0) {
100-
out.write(buf, 0, len);
101-
}
102-
in.close();
103-
out.flush();
104-
out.close();
105-
}
106-
}
107-
catch (Exception e) {
108-
Log.e(TAG, "Error copying icu data file", e);
115+
if(in != null){
116+
in.close();
117+
}
118+
if(out != null){
119+
out.flush();
120+
out.close();
121+
}
122+
} catch (IOException ioe){
123+
Log.e(TAG, "Error in closing streams IO streams after expanding ICU dat file", ioe);
124+
throw new RuntimeException(ioe);
109125
}
126+
}
110127
}
111128

112-
public static void loadLibs (Context context) {
129+
public static synchronized void loadLibs (Context context) {
113130
loadLibs(context, context.getFilesDir());
114131
}
115132

116-
public static void loadLibs (Context context, File workingDir)
117-
{
133+
public static synchronized void loadLibs (Context context, File workingDir) {
118134
System.loadLibrary("stlport_shared");
119135
System.loadLibrary("sqlcipher_android");
120136
System.loadLibrary("database_sqlcipher");

0 commit comments

Comments
 (0)