-
Notifications
You must be signed in to change notification settings - Fork 528
Expand file tree
/
Copy pathUtils.java
More file actions
83 lines (74 loc) · 2.81 KB
/
Copy pathUtils.java
File metadata and controls
83 lines (74 loc) · 2.81 KB
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
package com.readystatesoftware.sqliteasset;
import android.util.Log;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
class Utils {
private static final String TAG = SQLiteAssetHelper.class.getSimpleName();
public static List<String> splitSqlScript(String script, char delim) {
List<String> statements = new ArrayList<String>();
StringBuilder sb = new StringBuilder();
boolean inLiteral = false;
char[] content = script.toCharArray();
int openDelimiter = -1, lastCharacter = -1;
for (int i = 0; i < script.length(); i++) {
if (content[i] == '"' || content[i] == '\'') {
if (openDelimiter == -1) {
// We were not inside a literal; store the delimiter's value.
openDelimiter = content[i];
inLiteral = true;
} else if (openDelimiter == content[i] && lastCharacter != '\\') {
// We exit from the literal only on the same character
// AND unless we're being escaped by a bachslash.
inLiteral = false;
openDelimiter = -1;
}
}
if (content[i] == delim && !inLiteral) {
if (sb.length() > 0) {
statements.add(sb.toString().trim());
sb = new StringBuilder();
}
} else {
sb.append(content[i]);
}
if (lastCharacter == '\\' && content[i] == '\\') {
// It was an escaped backslash, don't accumulate.
lastCharacter = -1;
} else {
lastCharacter = content[i];
}
}
if (sb.length() > 0) {
statements.add(sb.toString().trim());
}
return statements;
}
public static void writeExtractedFileToDisk(InputStream in, OutputStream outs) throws IOException {
byte[] buffer = new byte[1024];
int length;
while ((length = in.read(buffer))>0){
outs.write(buffer, 0, length);
}
outs.flush();
outs.close();
in.close();
}
public static ZipInputStream getFileFromZip(InputStream zipFileStream) throws IOException {
ZipInputStream zis = new ZipInputStream(zipFileStream);
ZipEntry ze;
while ((ze = zis.getNextEntry()) != null) {
Log.w(TAG, "extracting file: '" + ze.getName() + "'...");
return zis;
}
return null;
}
public static String convertStreamToString(InputStream is) {
return new Scanner(is).useDelimiter("\\A").next();
}
}