-
Notifications
You must be signed in to change notification settings - Fork 5
/
CommonUtils.java
102 lines (89 loc) · 3.37 KB
/
CommonUtils.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 msdk.java.utils;
import android.content.Context;
import android.util.Base64;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.SecureRandom;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.json.JSONException;
import org.json.JSONObject;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
public class CommonUtils {
private static final int BUFFER_SIZE = 4096;
public static String createWalletKey() {
int lengthOfKey = 128;
SecureRandom random = new SecureRandom();
byte[] bytes = new byte[lengthOfKey];
random.nextBytes(bytes);
return Base64.encodeToString(bytes, Base64.NO_WRAP);
}
public static String getRootDir(Context context) {
return context.getFilesDir().getAbsolutePath() ;
}
public static String makeWalletName(String name) {
return String.format("%s-wallet", name);
}
public static void zipFiles(String sourcePath, String outputPath) throws IOException {
File source = new File(sourcePath);
FileOutputStream dest = new FileOutputStream(outputPath);
try (ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest))) {
if (source.isDirectory()) {
zipFolder(out, source, source.getParent().length());
} else {
writeBytes(out, source, 0);
}
}
}
private static void zipFolder(ZipOutputStream out, File folder, int basePathLength) throws IOException {
File[] files = folder.listFiles();
for (File file : files) {
if (file.isDirectory()) {
zipFolder(out, file, basePathLength);
} else {
writeBytes(out, file, basePathLength);
}
}
}
private static void writeBytes(ZipOutputStream zipOutput, File inputFile, int basePathLength) throws IOException {
String unmodifiedFilePath = inputFile.getPath();
String relativePath = unmodifiedFilePath.substring(basePathLength);
try (BufferedInputStream origin = new BufferedInputStream(new FileInputStream(unmodifiedFilePath), BUFFER_SIZE)) {
ZipEntry entry = new ZipEntry(relativePath);
entry.setTime(inputFile.lastModified());
zipOutput.putNextEntry(entry);
int bytesRead;
byte[] data = new byte[BUFFER_SIZE];
while ((bytesRead = origin.read(data, 0, BUFFER_SIZE)) != -1) {
zipOutput.write(data, 0, bytesRead);
}
}
}
public static JSONObject convertToJSONObject(String init) {
try {
if (init == null) {
return null;
}
return new JSONObject(init);
} catch (JSONException e) {
e.printStackTrace();
}
return new JSONObject();
}
public static String getThreadId(JSONObject message) throws JSONException {
String threadId;
if (message.optJSONObject("~thread") != null) {
threadId = message.getJSONObject("~thread").getString("thid");
} else {
threadId = message.getString("@id");
}
return threadId;
}
}