6
6
package com .facebook .thrift .transport ;
7
7
8
8
import com .facebook .thrift .utils .Logger ;
9
+ import java .io .ByteArrayInputStream ;
9
10
import java .io .ByteArrayOutputStream ;
10
11
import javax .net .ssl .SSLSocketFactory ;
11
12
import javax .net .ssl .TrustManager ;
16
17
import okhttp3 .Response ;
17
18
import okhttp3 .ResponseBody ;
18
19
import java .io .IOException ;
20
+ import java .io .InputStream ;
19
21
import java .util .HashMap ;
20
22
import java .util .Map ;
21
23
22
24
public class THttp2Client extends TTransport {
23
25
private static final Logger LOGGER = Logger .getLogger (THttp2Client .class .getName ());
24
26
25
27
private final ByteArrayOutputStream requestBuffer = new ByteArrayOutputStream ();
26
- private ResponseBody responseBody = null ;
28
+
29
+ private InputStream inputStream = null ;
27
30
private Map <String , String > customHeaders = null ;
28
31
private static final Map <String , String > defaultHeaders = getDefaultHeaders ();
29
32
@@ -75,12 +78,8 @@ public void open() {
75
78
76
79
public void close () {
77
80
try {
78
- if (responseBody != null ) {
79
- responseBody .close ();
80
- responseBody = null ;
81
- }
82
-
83
81
requestBuffer .close ();
82
+ inputStream .close ();
84
83
} catch (IOException e ) {
85
84
LOGGER .warn (e .getMessage ());
86
85
}
@@ -92,11 +91,11 @@ public boolean isOpen() {
92
91
}
93
92
94
93
public int read (byte [] buf , int off , int len ) throws TTransportException {
95
- if (responseBody == null ) {
94
+ if (inputStream == null ) {
96
95
throw new TTransportException ("Response buffer is empty, no request." );
97
96
}
98
97
try {
99
- int ret = responseBody . byteStream () .read (buf , off , len );
98
+ int ret = inputStream .read (buf , off , len );
100
99
if (ret == -1 ) {
101
100
throw new TTransportException ("No more data available." );
102
101
}
@@ -118,6 +117,7 @@ public void flush() throws TTransportException {
118
117
// Extract request and reset buffer
119
118
byte [] data = requestBuffer .toByteArray ();
120
119
requestBuffer .reset ();
120
+ Response response = null ;
121
121
try {
122
122
123
123
// Create request object
@@ -133,15 +133,22 @@ public void flush() throws TTransportException {
133
133
Request request = requestBuilder .build ();
134
134
135
135
// Make the request
136
- Response response = client .newCall (request ).execute ();
136
+ response = client .newCall (request ).execute ();
137
137
if (!response .isSuccessful ()) {
138
138
throw new TTransportException ("HTTP Response code: " + response .code ());
139
139
}
140
140
141
+ if (response .body () == null ) {
142
+ throw new TTransportException ("response body is null" );
143
+ }
141
144
// Read the response
142
- responseBody = response .body ();
145
+ inputStream = new ByteArrayInputStream ( response .body (). bytes () );
143
146
} catch (IOException iox ) {
144
147
throw new TTransportException (iox );
148
+ } finally {
149
+ if (response != null ) {
150
+ response .close ();
151
+ }
145
152
}
146
153
}
147
154
0 commit comments