Skip to content

Commit b84426a

Browse files
committed
see 02/12 log
1 parent 0791ebc commit b84426a

File tree

5 files changed

+140
-59
lines changed

5 files changed

+140
-59
lines changed

utilcode/lib/src/main/java/com/blankj/utilcode/util/CrashUtils.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
import java.io.File;
1414
import java.io.FileWriter;
1515
import java.io.IOException;
16-
import java.io.PrintWriter;
17-
import java.io.StringWriter;
1816
import java.lang.Thread.UncaughtExceptionHandler;
1917
import java.text.Format;
2018
import java.text.SimpleDateFormat;
@@ -88,17 +86,8 @@ public void uncaughtException(final Thread t, final Throwable e) {
8886
"\nApp VersionName : " + versionName +
8987
"\nApp VersionCode : " + versionCode +
9088
"\n************* Log Head ****************\n\n";
91-
sb.append(head);
92-
StringWriter sw = new StringWriter();
93-
PrintWriter pw = new PrintWriter(sw);
94-
e.printStackTrace(pw);
95-
Throwable cause = e.getCause();
96-
while (cause != null) {
97-
cause.printStackTrace(pw);
98-
cause = cause.getCause();
99-
}
100-
pw.flush();
101-
sb.append(sw.toString());
89+
sb.append(head)
90+
.append(ThreadUtils.getFullStackTrace(e));
10291
final String crashInfo = sb.toString();
10392
final String fullPath = (dir == null ? defaultDir : dir) + time + ".txt";
10493
if (createOrExistsFile(fullPath)) {

utilcode/lib/src/main/java/com/blankj/utilcode/util/HttpUtils.java

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
import android.annotation.SuppressLint;
55
import android.support.annotation.NonNull;
66

7-
import java.io.File;
87
import java.io.IOException;
98
import java.io.InputStream;
109
import java.io.UnsupportedEncodingException;
1110
import java.net.HttpURLConnection;
1211
import java.net.URL;
1312
import java.nio.charset.Charset;
13+
import java.nio.charset.IllegalCharsetNameException;
1414
import java.security.cert.X509Certificate;
1515
import java.util.HashMap;
1616
import java.util.Map;
@@ -32,6 +32,9 @@
3232
*/
3333
public class HttpUtils {
3434

35+
private static final String BOUNDARY = java.util.UUID.randomUUID().toString();
36+
private static final String TWO_HYPHENS = "--";
37+
3538
private static final int CONNECT_TIMEOUT_TIME = 15000;
3639
private static final int READ_TIMEOUT_TIME = 19000;
3740

@@ -108,6 +111,11 @@ public static void doPost(@NonNull final Request request,
108111
// printWriter.write(sb.toString());
109112
// printWriter.flush();
110113
// printWriter.close();
114+
115+
// DataOutputStream os;
116+
// BufferedOutputStream outputStream = new BufferedOutputStream(conn.getOutputStream());
117+
// outputStream.write(request.body.content);
118+
// outputStream.close();
111119
int responseCode = conn.getResponseCode();
112120
if (responseCode == 200) {
113121
InputStream is = conn.getInputStream();
@@ -158,6 +166,11 @@ private static void addHeader(final HttpURLConnection conn, final Map<String, St
158166
}
159167
}
160168

169+
private static String checkCharset(final String charset) {
170+
if (Charset.isSupported(charset)) return charset;
171+
throw new IllegalCharsetNameException(charset);
172+
}
173+
161174
private static boolean isSpace(final String s) {
162175
if (s == null) return true;
163176
for (int i = 0, len = s.length(); i < len; ++i) {
@@ -209,10 +222,11 @@ public Request post(@NonNull final RequestBody body) {
209222
}
210223
}
211224

212-
public static final class RequestBody {
213-
String mediaType;
214-
byte[] content;
215-
long length;
225+
public static class RequestBody {
226+
String mediaType;
227+
byte[] content;
228+
InputStream is;
229+
long length;
216230

217231
private RequestBody(String mediaType, byte[] content) {
218232
this.mediaType = mediaType;
@@ -226,18 +240,24 @@ private static String getCharsetFromMediaType(String mediaType) {
226240
if (index == -1) return "utf-8";
227241
int st = index + 8;
228242
int end = mediaType.length();
243+
if (st >= end) {
244+
throw new IllegalArgumentException("MediaType is not correct: \"" + mediaType + "\"");
245+
}
229246
for (int i = st; i < end; i++) {
230247
char c = mediaType.charAt(i);
231-
if ((c < 'a' || c > 'z') && (c < '0' || c > '9') && c != '-') {
232-
end = i;
233-
break;
234-
}
235-
}
236-
if (st < end) {
237-
String charset = mediaType.substring(st, end);
238-
if (Charset.isSupported(charset)) return charset;
248+
if (c >= 'A' && c <= 'Z') continue;
249+
if (c >= 'a' && c <= 'z') continue;
250+
if (c >= '0' && c <= '9') continue;
251+
if (c == '-' && i != 0) continue;
252+
if (c == '+' && i != 0) continue;
253+
if (c == ':' && i != 0) continue;
254+
if (c == '_' && i != 0) continue;
255+
if (c == '.' && i != 0) continue;
256+
end = i;
257+
break;
239258
}
240-
throw new IllegalArgumentException("MediaType is not correct: \"" + mediaType + "\"");
259+
String charset = mediaType.substring(st, end);
260+
return checkCharset(charset);
241261
}
242262

243263
public static RequestBody create(String mediaType, byte[] content) {
@@ -249,13 +269,11 @@ public static RequestBody form(final Map<String, String> form) {
249269
}
250270

251271
public static RequestBody form(final Map<String, String> form, String charset) {
252-
String mediaType = "application/x-www-form-urlencoded;charset=" + charset;
272+
String mediaType = "application/x-www-form-urlencoded;charset=" + checkCharset(charset);
253273
if (form != null) {
254274
final StringBuilder sb = new StringBuilder();
255275
for (String key : form.keySet()) {
256-
if (sb.length() != 0) {
257-
sb.append("&");
258-
}
276+
if (sb.length() > 0) sb.append("&");
259277
sb.append(key).append("=").append(form.get(key));
260278
}
261279
try {
@@ -272,7 +290,7 @@ public static RequestBody json(final String json) {
272290
}
273291

274292
public static RequestBody json(final String json, String charset) {
275-
String mediaType = "application/json;charset=" + charset;
293+
String mediaType = "application/json;charset=" + checkCharset(charset);
276294
if (json != null) {
277295
try {
278296
return new RequestBody(mediaType, json.getBytes(charset));
@@ -283,9 +301,11 @@ public static RequestBody json(final String json, String charset) {
283301
return new RequestBody(mediaType, null);
284302
}
285303

286-
public static RequestBody file(String mediaType, final File file) {
287-
return file(mediaType, file);
288-
}
304+
// public static RequestBody file(String mediaType, final File file) {
305+
//
306+
// return new RequestBody(mediaType, );
307+
// }
308+
289309
}
290310

291311

utilcode/lib/src/main/java/com/blankj/utilcode/util/LogUtils.java

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,12 @@
2828
import java.io.FileWriter;
2929
import java.io.FilenameFilter;
3030
import java.io.IOException;
31-
import java.io.PrintWriter;
3231
import java.io.StringReader;
3332
import java.io.StringWriter;
3433
import java.lang.annotation.Retention;
3534
import java.lang.annotation.RetentionPolicy;
3635
import java.lang.reflect.ParameterizedType;
3736
import java.lang.reflect.Type;
38-
import java.net.UnknownHostException;
3937
import java.text.ParseException;
4038
import java.text.SimpleDateFormat;
4139
import java.util.Arrays;
@@ -877,23 +875,7 @@ static String formatXml(String xml) {
877875
}
878876

879877
private static String throwable2String(final Throwable e) {
880-
Throwable t = e;
881-
while (t != null) {
882-
if (t instanceof UnknownHostException) {
883-
return "";
884-
}
885-
t = t.getCause();
886-
}
887-
StringWriter sw = new StringWriter();
888-
PrintWriter pw = new PrintWriter(sw);
889-
e.printStackTrace(pw);
890-
Throwable cause = e.getCause();
891-
while (cause != null) {
892-
cause.printStackTrace(pw);
893-
cause = cause.getCause();
894-
}
895-
pw.flush();
896-
return sw.toString();
878+
return ThrowableUtils.getFullStackTrace(e);
897879
}
898880

899881
private static String bundle2String(Bundle bundle) {
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.blankj.utilcode.util;
2+
3+
import java.io.PrintWriter;
4+
import java.io.StringWriter;
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import java.util.StringTokenizer;
8+
9+
/**
10+
* <pre>
11+
* author: blankj
12+
* blog : http://blankj.com
13+
* time : 2019/02/12
14+
* desc : utils about exception
15+
* </pre>
16+
*/
17+
public class ThrowableUtils {
18+
19+
private static final String LINE_SEP = System.getProperty("line.separator");
20+
21+
private ThrowableUtils() {
22+
throw new UnsupportedOperationException("u can't instantiate me...");
23+
}
24+
25+
public static String getFullStackTrace(Throwable throwable) {
26+
final List<Throwable> throwableList = new ArrayList<>();
27+
while (throwable != null && !throwableList.contains(throwable)) {
28+
throwableList.add(throwable);
29+
throwable = throwable.getCause();
30+
}
31+
final int size = throwableList.size();
32+
final List<String> frames = new ArrayList<>();
33+
List<String> nextTrace = getStackFrameList(throwableList.get(size - 1));
34+
for (int i = size; --i >= 0; ) {
35+
final List<String> trace = nextTrace;
36+
if (i != 0) {
37+
nextTrace = getStackFrameList(throwableList.get(i - 1));
38+
removeCommonFrames(trace, nextTrace);
39+
}
40+
if (i == size - 1) {
41+
frames.add(throwableList.get(i).toString());
42+
} else {
43+
frames.add(" Caused by: " + throwableList.get(i).toString());
44+
}
45+
frames.addAll(trace);
46+
}
47+
StringBuilder sb = new StringBuilder();
48+
for (final String element : frames) {
49+
sb.append(element).append(LINE_SEP);
50+
}
51+
return sb.toString();
52+
}
53+
54+
private static List<String> getStackFrameList(final Throwable throwable) {
55+
final StringWriter sw = new StringWriter();
56+
final PrintWriter pw = new PrintWriter(sw, true);
57+
throwable.printStackTrace(pw);
58+
final String stackTrace = sw.toString();
59+
final StringTokenizer frames = new StringTokenizer(stackTrace, LINE_SEP);
60+
final List<String> list = new ArrayList<>();
61+
boolean traceStarted = false;
62+
while (frames.hasMoreTokens()) {
63+
final String token = frames.nextToken();
64+
// Determine if the line starts with <whitespace>at
65+
final int at = token.indexOf("at");
66+
if (at != -1 && token.substring(0, at).trim().isEmpty()) {
67+
traceStarted = true;
68+
list.add(token);
69+
} else if (traceStarted) {
70+
break;
71+
}
72+
}
73+
return list;
74+
}
75+
76+
private static void removeCommonFrames(final List<String> causeFrames, final List<String> wrapperFrames) {
77+
int causeFrameIndex = causeFrames.size() - 1;
78+
int wrapperFrameIndex = wrapperFrames.size() - 1;
79+
while (causeFrameIndex >= 0 && wrapperFrameIndex >= 0) {
80+
// Remove the frame from the cause trace if it is the same
81+
// as in the wrapper trace
82+
final String causeFrame = causeFrames.get(causeFrameIndex);
83+
final String wrapperFrame = wrapperFrames.get(wrapperFrameIndex);
84+
if (causeFrame.equals(wrapperFrame)) {
85+
causeFrames.remove(causeFrameIndex);
86+
}
87+
causeFrameIndex--;
88+
wrapperFrameIndex--;
89+
}
90+
}
91+
}

utilcode/lib/src/main/java/com/blankj/utilcode/util/Utils.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,13 @@
4242
*/
4343
public final class Utils {
4444

45-
@SuppressLint("StaticFieldLeak")
46-
private static Application sApplication;
47-
48-
private static final ActivityLifecycleImpl ACTIVITY_LIFECYCLE = new ActivityLifecycleImpl();
49-
5045
private static final String PERMISSION_ACTIVITY_CLASS_NAME =
5146
"com.blankj.utilcode.util.PermissionUtils$PermissionActivity";
5247

48+
private static final ActivityLifecycleImpl ACTIVITY_LIFECYCLE = new ActivityLifecycleImpl();
49+
50+
@SuppressLint("StaticFieldLeak")
51+
private static Application sApplication;
5352

5453
private Utils() {
5554
throw new UnsupportedOperationException("u can't instantiate me...");

0 commit comments

Comments
 (0)