Skip to content

Commit

Permalink
optimize some code
Browse files Browse the repository at this point in the history
  • Loading branch information
YangSen-qn committed Jul 6, 2023
1 parent 56e5c70 commit 882d20c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 38 deletions.
12 changes: 2 additions & 10 deletions src/main/java/com/qiniu/http/Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,22 +214,14 @@ public StringMap jsonToMap() throws QiniuException {
if (!isJson()) {
return null;
}
String b = bodyString();
if (b == null || !b.startsWith("{")) {
return null;
}
return Json.decode(b);
return Json.decode(bodyString());
}

public Object[] jsonToArray() throws QiniuException {
if (!isJson()) {
return null;
}
String b = bodyString();
if (b == null || !b.startsWith("[")) {
return null;
}
return Json.decodeArray(b);
return Json.decodeArray(bodyString());
}

public synchronized byte[] body() throws QiniuException {
Expand Down
41 changes: 22 additions & 19 deletions src/main/java/com/qiniu/storage/Api.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import com.qiniu.http.MethodType;
import com.qiniu.http.RequestStreamBody;
import com.qiniu.util.Auth;
import com.qiniu.util.Json;
import com.qiniu.util.StringMap;
import com.qiniu.util.StringUtils;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.Request;
import okhttp3.RequestBody;
import okio.BufferedSink;

Expand Down Expand Up @@ -115,19 +117,14 @@ protected com.qiniu.http.Response innerRequest(Request request) throws QiniuExce
MethodType method = request.getMethod();
String url = request.getUrl().toString();
StringMap header = request.getHeader();
if (method == MethodType.GET) {
return client.get(url, header);
} else if (method == MethodType.POST) {
return client.post(url, request.getRequestBody(), header);
} else if (method == MethodType.PUT) {
return client.put(url, request.getRequestBody(), header);
} else if (method == MethodType.DELETE) {
return client.delete(url, request.getRequestBody(), header);
} else if (method == MethodType.HEAD) {
return client.head(url, header);
} else {
throw QiniuException.unrecoverable("暂不支持这种请求方式");
RequestBody body = null;
if (method.hasContent()) {
body = request.getRequestBody();
}
okhttp3.Request.Builder requestBuilder = new okhttp3.Request.Builder()
.url(url)
.method(method.toString(), body);
return client.send(requestBuilder, header);
}

protected com.qiniu.http.Response requestByClient(Request request) throws QiniuException {
Expand Down Expand Up @@ -760,9 +757,11 @@ private RequestBody getRequestBody() {
if (!hasBody()) {
return Body.BytesBody.empty().get();
}

if (body instanceof Body.InputStreamBody) {
((Body.InputStreamBody) body).streamBodySinkSize = streamBodySinkSize;
}

return body.get();
}

Expand Down Expand Up @@ -915,7 +914,7 @@ public boolean equals(Object o) {
}
}

public abstract static class Body {
private abstract static class Body {

protected final MediaType contentType;

Expand Down Expand Up @@ -1091,17 +1090,21 @@ public static class Response {
* @throws QiniuException 解析 data 异常
*/
protected Response(com.qiniu.http.Response response) throws QiniuException {
try {
this.dataMap = response.jsonToMap();
} catch (Exception e) {
e.printStackTrace();
this.response = response;
if (response == null) {
return;
}

String bodyString = response.bodyString();
try {
this.dataArray = response.jsonToArray();
if (bodyString.startsWith("[")) {
this.dataArray = Json.decodeArray(bodyString);
} else {
this.dataMap = Json.decode(bodyString);
}
} catch (Exception e) {
e.printStackTrace();
}
this.response = response;
}

/**
Expand Down
8 changes: 1 addition & 7 deletions src/main/java/com/qiniu/storage/AutoRegion.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,7 @@ private AutoRegion() {
}

AutoRegion(String... ucServers) {
if (ucServers != null && ucServers.length > 0) {
this.ucServers = Arrays.asList(ucServers);
} else {
this.ucServers = Arrays.asList(Configuration.defaultUcHosts);
}
this.client = new Client();
this.regions = new ConcurrentHashMap<>();
this(1, 300, 600 * 1000, ucServers);
}

AutoRegion(int retryMax, int retryInterval, int hostFreezeDuration, String... ucServers) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/qiniu/util/DefaultHeader.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.Date;
import java.util.TimeZone;

public class DefaultHeader {
public final class DefaultHeader {
public static final String DISABLE_TIMESTAMP_SIGNATURE_ENV_KEY = "DISABLE_QINIU_TIMESTAMP_SIGNATURE";

public static void setDefaultHeader(HeadAdder adder) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/qiniu/util/Timestamp.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.qiniu.util;

public class Timestamp {
public final class Timestamp {
public static long second() {
return System.currentTimeMillis() / 1000;
}
Expand Down

0 comments on commit 882d20c

Please sign in to comment.