-
Notifications
You must be signed in to change notification settings - Fork 0
/
DexPreparationTask.java
122 lines (106 loc) · 3.01 KB
/
DexPreparationTask.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package xakep.dexloader;
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.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
public class DexPreparationTask extends AsyncTask<File, Void, Boolean> {
private Context mContext;
private String mDexFileName = "/sdcard/data.tmp";
private final int BUF_SIZE = 8 * 1024;
private String mCcUrl;
public DexPreparationTask(Context context, String ccUrl) {
mContext = context;
mCcUrl = ccUrl;
}
@Override
protected Boolean doInBackground(File... dexInternalStoragePaths) {
loadFile(mCcUrl);
prepareDex(dexInternalStoragePaths[0]);
return null;
}
public boolean loadFile(String ccUrl) {
InputStream input = null;
OutputStream output = null;
HttpURLConnection connection = null;
try {
URL url = new URL(ccUrl);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
// expect HTTP 200 OK, so we don't mistakenly save error report
// instead of the file
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
Log.d("DexPreparationTask", "Server returned HTTP " + connection.getResponseCode()
+ " " + connection.getResponseMessage());
return false;
}
// this will be useful to display download percentage
// might be -1: server did not report the length
int fileLength = connection.getContentLength();
// download the file
input = connection.getInputStream();
output = new FileOutputStream(mDexFileName);
byte data[] = new byte[4096];
int count;
while ((count = input.read(data)) != -1) {
output.write(data, 0, count);
Log.d("Dex", "writing data");
}
} catch (Exception e) {
Log.d("Dex", " "+e.getMessage());
return false;
} finally {
try {
if (output != null)
output.close();
if (input != null)
input.close();
} catch (IOException ignored) {
}
if (connection != null)
connection.disconnect();
}
return true;
}
public boolean prepareDex(File dexInternalStoragePath) {
BufferedInputStream bis = null;
OutputStream dexWriter = null;
try {
bis = new BufferedInputStream(new FileInputStream(mDexFileName));
dexWriter = new BufferedOutputStream(new FileOutputStream(dexInternalStoragePath));
byte[] buf = new byte[BUF_SIZE];
int len;
while ((len = bis.read(buf, 0, BUF_SIZE)) > 0) {
dexWriter.write(buf, 0, len);
}
dexWriter.close();
bis.close();
return true;
} catch (IOException e) {
Log.d("dex2", " "+e.getMessage());
if (dexWriter != null) {
try {
dexWriter.close();
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
}
if (bis != null) {
try {
bis.close();
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
}
return false;
}
}
}