Skip to content

Commit fed2aa1

Browse files
authored
Merge pull request #409 from YangSen-qn/sdk_v7_upload_gzip
version v7: uplog gzip
2 parents 9232709 + 16237a7 commit fed2aa1

File tree

4 files changed

+121
-3
lines changed

4 files changed

+121
-3
lines changed

library/src/androidTest/java/com/qiniu/android/TestConfig.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public final class TestConfig {
2222
public static final String token_na0 = "bjtWBQXrcxgo7HWwlC_bgHg81j352_GhgBGZPeOW:b46IRIripBu1p2dV1VVBvqM9au4=:eyJzY29wZSI6InNkay1uYTAiLCJkZWFkbGluZSI6MTYwNjQ2MDAzOH0=";
2323
public static final String ak = "bjtWBQXrcxgo7HWwlC_bgHg81j352_GhgBGZPeOW";
2424

25-
2625
//测试通用的token
2726
public static final String commonToken = token_na0;
2827

library/src/main/java/com/qiniu/android/collect/Config.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public final class Config {
1313
/**
1414
* 上传信息收集文件的地址
1515
*/
16-
public final static String serverURL = "https://uplog.qbox.me/log/4";
16+
public final static String serverURL = "https://uplog.qbox.me/log/4?compressed=gzip";
1717
/**
1818
* 是否记录上传状态信息。 true 表示记录,false 表示不记录。
1919
* <p>

library/src/main/java/com/qiniu/android/collect/UploadInfoCollector.java

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
import com.qiniu.android.http.UserAgent;
44
import com.qiniu.android.storage.UpToken;
5+
import com.qiniu.android.utils.GZipUtil;
56

7+
import java.io.ByteArrayOutputStream;
68
import java.io.File;
79
import java.io.FileNotFoundException;
810
import java.io.FileOutputStream;
911
import java.io.IOException;
12+
import java.io.RandomAccessFile;
1013
import java.nio.charset.Charset;
1114
import java.util.Date;
1215
import java.util.concurrent.ExecutorService;
@@ -241,9 +244,17 @@ private void tryUploadAndClean(final UpToken upToken, File recordFile) {
241244

242245
//同步上传
243246
private boolean upload(final UpToken upToken, File recordFile) {
247+
248+
byte[] logData = getLogData(recordFile);
249+
logData = GZipUtil.gZip(logData);
250+
if (logData == null || logData.length == 0){
251+
return false;
252+
}
253+
244254
try {
255+
245256
OkHttpClient client = getHttpClient();
246-
RequestBody reqBody = RequestBody.create(MediaType.parse("text/plain"), recordFile);
257+
RequestBody reqBody = RequestBody.create(MediaType.parse("text/plain"), logData);
247258

248259
Request.Builder requestBuilder = new Request.Builder().url(serverURL).
249260
addHeader("Authorization", "UpToken " + upToken.token).
@@ -271,6 +282,37 @@ private boolean upload(final UpToken upToken, File recordFile) {
271282
}
272283
}
273284

285+
private byte[] getLogData(File recordFile){
286+
if (recordFile == null || recordFile.length() == 0){
287+
return null;
288+
}
289+
290+
int fileSize = (int)recordFile.length();
291+
RandomAccessFile randomAccessFile = null;
292+
byte[] data = null;
293+
try {
294+
randomAccessFile = new RandomAccessFile(recordFile, "r");
295+
ByteArrayOutputStream out = new ByteArrayOutputStream(fileSize);
296+
int len = 0;
297+
byte[] buff = new byte[fileSize];
298+
while ((len = randomAccessFile.read(buff)) >= 0){
299+
out.write(buff, 0, len);
300+
}
301+
data = out.toByteArray();
302+
} catch (FileNotFoundException ignored) {
303+
} catch (IOException e) {
304+
data = null;
305+
} finally {
306+
if (randomAccessFile != null) {
307+
try {
308+
randomAccessFile.close();
309+
} catch (IOException e) {
310+
}
311+
}
312+
}
313+
return data;
314+
}
315+
274316
private boolean isOk(Response res) {
275317
return res.isSuccessful() && res.header("X-Reqid") != null;
276318
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.qiniu.android.utils;
2+
3+
import java.io.ByteArrayInputStream;
4+
import java.io.ByteArrayOutputStream;
5+
import java.io.File;
6+
import java.io.IOException;
7+
import java.io.RandomAccessFile;
8+
import java.util.zip.GZIPInputStream;
9+
import java.util.zip.GZIPOutputStream;
10+
11+
public class GZipUtil {
12+
13+
public static byte[] gZip(String string){
14+
if (string == null){
15+
return null;
16+
}
17+
return gZip(string.getBytes());
18+
}
19+
20+
public static byte[] gZip(byte[] bytes){
21+
if (bytes == null){
22+
return null;
23+
}
24+
if (bytes.length == 0){
25+
return bytes;
26+
}
27+
28+
ByteArrayOutputStream out = new ByteArrayOutputStream();
29+
GZIPOutputStream gzip = null;
30+
try {
31+
gzip = new GZIPOutputStream(out);
32+
gzip.write(bytes);
33+
} catch (IOException e){
34+
} finally {
35+
if (gzip != null){
36+
try {
37+
gzip.close();
38+
} catch (IOException e){
39+
}
40+
}
41+
}
42+
43+
return out.toByteArray();
44+
}
45+
46+
public static byte[] gUnzip(byte[] bytes) {
47+
if (bytes == null){
48+
return null;
49+
}
50+
if (bytes.length == 0){
51+
return bytes;
52+
}
53+
54+
ByteArrayOutputStream out = new ByteArrayOutputStream();
55+
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
56+
57+
GZIPInputStream gunzip = null;
58+
try {
59+
gunzip = new GZIPInputStream(in);
60+
byte[] buffer = new byte[256];
61+
int n;
62+
while ((n = gunzip.read(buffer)) >= 0) {
63+
out.write(buffer, 0, n);
64+
}
65+
} catch (IOException e) {
66+
} finally {
67+
if (gunzip != null) {
68+
try {
69+
gunzip.close();
70+
} catch (IOException e) {
71+
}
72+
}
73+
}
74+
75+
return out.toByteArray();
76+
}
77+
}

0 commit comments

Comments
 (0)