Skip to content

Commit 781cccf

Browse files
committed
update httpclient
1 parent 02aa895 commit 781cccf

File tree

4 files changed

+113
-110
lines changed

4 files changed

+113
-110
lines changed

xbox/src/main/java/com/xbox/httpclient/HttpClientRequest.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.xbox.httpclient;
22

3+
import org.apache.http.client.methods.HttpPost;
4+
import org.apache.http.client.methods.HttpPut;
5+
36
import java.io.IOException;
47
import java.net.UnknownHostException;
58

@@ -27,34 +30,30 @@ public class HttpClientRequest {
2730
public native void OnRequestFailed(long j, String str, boolean z);
2831

2932
public void setHttpUrl(String str) {
30-
this.requestBuilder = this.requestBuilder.url(str);
33+
requestBuilder = requestBuilder.url(str);
3134
}
3235

3336
public void setHttpMethodAndBody(String str, long j, String str2, long j2) {
34-
MediaType mediaType = null;
35-
RequestBody requestBody = null;
37+
RequestBody httpClientRequestBody = null;
3638
if (j2 == 0) {
37-
if ("POST".equals(str) || "PUT".equals(str)) {
38-
if (str2 != null) {
39-
mediaType = MediaType.parse(str2);
40-
}
41-
requestBody = RequestBody.create(NO_BODY, mediaType);
39+
if (HttpPost.METHOD_NAME.equals(str) || HttpPut.METHOD_NAME.equals(str)) {
40+
httpClientRequestBody = RequestBody.create(NO_BODY, str2 != null ? MediaType.parse(str2) : null);
4241
}
4342
} else {
44-
requestBody = new HttpClientRequestBody(j, str2, j2);
43+
httpClientRequestBody = new HttpClientRequestBody(j, str2, j2);
4544
}
46-
this.requestBuilder.method(str, requestBody);
45+
requestBuilder.method(str, httpClientRequestBody);
4746
}
4847

4948
public void setHttpHeader(String str, String str2) {
50-
this.requestBuilder = this.requestBuilder.addHeader(str, str2);
49+
requestBuilder = requestBuilder.addHeader(str, str2);
5150
}
5251

5352
public void doRequestAsync(final long j) {
54-
OK_CLIENT.newCall(this.requestBuilder.build()).enqueue(new Callback() {
53+
OK_CLIENT.newCall(requestBuilder.build()).enqueue(new Callback() {
5554
@Override
5655
public void onFailure(Call call, IOException iOException) {
57-
HttpClientRequest.this.OnRequestFailed(j, iOException.getClass().getCanonicalName(), iOException instanceof UnknownHostException);
56+
OnRequestFailed(j, iOException.getClass().getCanonicalName(), iOException instanceof UnknownHostException);
5857
}
5958

6059
@Override

xbox/src/main/java/com/xbox/httpclient/HttpClientRequestBody.java

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,37 @@ public final class HttpClientRequestBody extends RequestBody {
2222
private final long contentLength;
2323
private final MediaType contentType;
2424

25+
public HttpClientRequestBody(long callHandle, String contentType, long contentLength) {
26+
this.callHandle = callHandle;
27+
this.contentType = contentType != null ? MediaType.parse(contentType) : null;
28+
this.contentLength = contentLength;
29+
}
30+
31+
@Override
32+
public MediaType contentType() {
33+
return contentType;
34+
}
35+
36+
@Override
37+
public long contentLength() {
38+
return contentLength;
39+
}
40+
41+
@Override
42+
public void writeTo(@NonNull BufferedSink bufferedSink) throws IOException {
43+
bufferedSink.writeAll(Okio.source(new NativeInputStream(callHandle)));
44+
}
45+
2546
private final class NativeInputStream extends InputStream {
2647
private final long callHandle;
2748
private long offset = 0;
2849

29-
private native int nativeRead(long j, long j2, byte[] bArr, long j3, long j4) throws IOException;
30-
31-
public NativeInputStream(long j) {
32-
this.callHandle = j;
50+
public NativeInputStream(long callHandle) {
51+
this.callHandle = callHandle;
3352
}
3453

54+
private native int nativeRead(long callHandle, long offset, byte[] b, long off, long len) throws IOException;
55+
3556
@Override
3657
public int read() throws IOException {
3758
byte[] bArr = new byte[1];
@@ -40,52 +61,31 @@ public int read() throws IOException {
4061
}
4162

4263
@Override
43-
public int read(byte[] bArr) throws IOException {
44-
return read(bArr, 0, bArr.length);
64+
public int read(byte[] b) throws IOException {
65+
return read(b, 0, b.length);
4566
}
4667

4768
@Override
48-
public int read(byte[] bArr, int i, int i2) throws IOException {
49-
Objects.requireNonNull(bArr);
50-
if (i < 0 || i2 < 0 || i + i2 > bArr.length) {
69+
public int read(byte[] b, int off, int len) throws IOException {
70+
Objects.requireNonNull(b);
71+
if (off < 0 || len < 0 || off + len > b.length) {
5172
throw new IndexOutOfBoundsException();
5273
}
53-
if (i2 == 0) {
74+
if (len == 0) {
5475
return 0;
5576
}
56-
int nativeRead = nativeRead(this.callHandle, this.offset, bArr, i, i2);
77+
int nativeRead = nativeRead(callHandle, offset, b, off, len);
5778
if (nativeRead == -1) {
5879
return -1;
5980
}
60-
this.offset += nativeRead;
81+
offset += nativeRead;
6182
return nativeRead;
6283
}
6384

6485
@Override
65-
public long skip(long j) throws IOException {
66-
this.offset += j;
67-
return j;
86+
public long skip(long offset) throws IOException {
87+
this.offset += offset;
88+
return offset;
6889
}
6990
}
70-
71-
public HttpClientRequestBody(long j, String str, long j2) {
72-
this.callHandle = j;
73-
this.contentType = str != null ? MediaType.parse(str) : null;
74-
this.contentLength = j2;
75-
}
76-
77-
@Override
78-
public MediaType contentType() {
79-
return this.contentType;
80-
}
81-
82-
@Override
83-
public long contentLength() {
84-
return this.contentLength;
85-
}
86-
87-
@Override
88-
public void writeTo(@NonNull BufferedSink bufferedSink) throws IOException {
89-
bufferedSink.writeAll(Okio.source(new NativeInputStream(this.callHandle)));
90-
}
9191
}

xbox/src/main/java/com/xbox/httpclient/HttpClientResponse.java

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.Objects;
66

77
import okhttp3.Response;
8+
import okhttp3.ResponseBody;
89
import okio.Okio;
910

1011
/**
@@ -17,71 +18,74 @@ public class HttpClientResponse {
1718
private final long callHandle;
1819
private final Response response;
1920

20-
private final class NativeOutputStream extends OutputStream {
21-
private final long callHandle;
22-
23-
private native void nativeWrite(long j, byte[] bArr, int i, int i2) throws IOException;
24-
25-
public NativeOutputStream(long j) {
26-
this.callHandle = j;
27-
}
28-
29-
@Override
30-
public void write(byte[] bArr) throws IOException {
31-
write(bArr, 0, bArr.length);
32-
}
33-
34-
@Override
35-
public void write(byte[] bArr, int i, int i2) throws IOException {
36-
Objects.requireNonNull(bArr);
37-
if (i < 0 || i2 < 0 || i + i2 > bArr.length) {
38-
throw new IndexOutOfBoundsException();
39-
}
40-
nativeWrite(this.callHandle, bArr, i, i2);
41-
}
42-
43-
@Override
44-
public void write(int i) throws IOException {
45-
write(new byte[]{(byte) i});
46-
}
47-
}
48-
4921
public HttpClientResponse(long j, Response response) {
50-
this.callHandle = j;
22+
callHandle = j;
5123
this.response = response;
5224
}
5325

5426
public int getNumHeaders() {
55-
return this.response.headers().size();
27+
return response.headers().size();
5628
}
5729

5830
public String getHeaderNameAtIndex(int i) {
59-
if (i < 0 || i >= this.response.headers().size()) {
31+
if (i < 0 || i >= response.headers().size()) {
6032
return null;
6133
}
62-
return this.response.headers().name(i);
34+
return response.headers().name(i);
6335
}
6436

6537
public String getHeaderValueAtIndex(int i) {
66-
if (i < 0 || i >= this.response.headers().size()) {
38+
if (i < 0 || i >= response.headers().size()) {
6739
return null;
6840
}
69-
return this.response.headers().value(i);
41+
return response.headers().value(i);
7042
}
7143

7244
public void getResponseBodyBytes() {
7345
try {
74-
this.response.body().source().readAll(Okio.sink(new NativeOutputStream(this.callHandle)));
46+
final ResponseBody body = response.body();
47+
if (body != null) {
48+
body.source().readAll(Okio.sink(new NativeOutputStream(callHandle)));
49+
}
7550
} catch (IOException e) {
7651
e.printStackTrace();
7752
} catch (Throwable th) {
78-
this.response.close();
53+
response.close();
7954
throw th;
8055
}
81-
this.response.close();
56+
response.close();
8257
}
8358

8459
public int getResponseCode() {
85-
return this.response.code();
60+
return response.code();
61+
}
62+
63+
private final class NativeOutputStream extends OutputStream {
64+
private final long callHandle;
65+
66+
public NativeOutputStream(long j) {
67+
this.callHandle = j;
68+
}
69+
70+
private native void nativeWrite(long j, byte[] bArr, int i, int i2) throws IOException;
71+
72+
@Override
73+
public void write(byte[] bArr) throws IOException {
74+
write(bArr, 0, bArr.length);
75+
}
76+
77+
@Override
78+
public void write(byte[] bArr, int i, int i2) throws IOException {
79+
Objects.requireNonNull(bArr);
80+
if (i < 0 || i2 < 0 || i + i2 > bArr.length) {
81+
throw new IndexOutOfBoundsException();
82+
}
83+
nativeWrite(callHandle, bArr, i, i2);
84+
}
85+
86+
@Override
87+
public void write(int i) throws IOException {
88+
write(new byte[]{(byte) i});
89+
}
8690
}
87-
}
91+
}

xbox/src/main/java/com/xbox/httpclient/HttpClientWebSocket.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,16 @@ public final class HttpClientWebSocket extends WebSocketListener {
2424
private final long owner;
2525
private WebSocket socket;
2626

27+
HttpClientWebSocket(long owner) {
28+
this.owner = owner;
29+
}
30+
2731
public native void onBinaryMessage(ByteBuffer byteBuffer);
2832

2933
public native void onClose(int i);
3034

3135
@Override
32-
public void onClosing(WebSocket webSocket, int i, String str) {
36+
public void onClosing(WebSocket webSocket, int code, String reason) {
3337
}
3438

3539
public native void onFailure();
@@ -38,29 +42,25 @@ public void onClosing(WebSocket webSocket, int i, String str) {
3842

3943
public native void onOpen();
4044

41-
HttpClientWebSocket(long j) {
42-
this.owner = j;
43-
}
44-
45-
public void addHeader(String str, String str2) {
46-
this.headers.add(str, str2);
45+
public void addHeader(String name, String value) {
46+
headers.add(name, value);
4747
}
4848

49-
public void connect(String str, String str2) {
50-
addHeader("Sec-WebSocket-Protocol", str2);
51-
this.socket = OK_CLIENT.newWebSocket(new Request.Builder().url(str).headers(this.headers.build()).build(), this);
49+
public void connect(String url, String header) {
50+
addHeader("Sec-WebSocket-Protocol", header);
51+
socket = OK_CLIENT.newWebSocket(new Request.Builder().url(url).headers(headers.build()).build(), this);
5252
}
5353

5454
public boolean sendMessage(String str) {
55-
return this.socket.send(str);
55+
return socket.send(str);
5656
}
5757

5858
public boolean sendBinaryMessage(ByteBuffer byteBuffer) {
59-
return this.socket.send(ByteString.of(byteBuffer));
59+
return socket.send(ByteString.of(byteBuffer));
6060
}
6161

6262
public void disconnect(int i) {
63-
this.socket.close(i, null);
63+
socket.close(i, null);
6464
}
6565

6666
@Override
@@ -69,26 +69,26 @@ public void onOpen(WebSocket webSocket, Response response) {
6969
}
7070

7171
@Override
72-
public void onFailure(WebSocket webSocket, Throwable th, Response response) {
72+
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
7373
onFailure();
7474
}
7575

7676
@Override
77-
public void onClosed(WebSocket webSocket, int i, String str) {
78-
onClose(i);
77+
public void onClosed(WebSocket webSocket, int code, String reason) {
78+
onClose(code);
7979
}
8080

8181
@Override
82-
public void onMessage(WebSocket webSocket, String str) {
83-
onMessage(str);
82+
public void onMessage(WebSocket webSocket, String text) {
83+
onMessage(text);
8484
}
8585

8686
@Override
87-
public void onMessage(WebSocket webSocket, @NonNull ByteString byteString) {
88-
onBinaryMessage(byteString.asByteBuffer());
87+
public void onMessage(WebSocket webSocket, @NonNull ByteString bytes) {
88+
onBinaryMessage(bytes.asByteBuffer());
8989
}
9090

9191
protected void finalize() {
92-
this.socket.cancel();
92+
socket.cancel();
9393
}
9494
}

0 commit comments

Comments
 (0)