@@ -12,6 +12,7 @@ import 'package:http/http.dart';
12
12
import 'package:jni/jni.dart' ;
13
13
import 'package:path/path.dart' ;
14
14
15
+ import 'third_party/java/io/BufferedInputStream.dart' ;
15
16
import 'third_party/java/lang/System.dart' ;
16
17
import 'third_party/java/net/HttpURLConnection.dart' ;
17
18
import 'third_party/java/net/URL.dart' ;
@@ -96,15 +97,15 @@ class JavaClient extends BaseClient {
96
97
}
97
98
98
99
// TODO: Rename _isolateMethod to something more descriptive.
99
- void _isolateMethod (
100
+ Future < void > _isolateMethod (
100
101
({
101
102
SendPort sendPort,
102
103
Uint8List body,
103
104
Map <String , String > headers,
104
105
String method,
105
106
Uri url,
106
107
}) request,
107
- ) {
108
+ ) async {
108
109
final httpUrlConnection = URL
109
110
.ctor3 (request.url.toString ().toJString ())
110
111
.openConnection ()
@@ -140,7 +141,7 @@ class JavaClient extends BaseClient {
140
141
responseHeaders,
141
142
);
142
143
143
- _responseBody (
144
+ await _responseBody (
144
145
request.url,
145
146
httpUrlConnection,
146
147
request.sendPort,
@@ -230,35 +231,61 @@ class JavaClient extends BaseClient {
230
231
return contentLength;
231
232
}
232
233
233
- void _responseBody (
234
+ Future < void > _responseBody (
234
235
Uri requestUrl,
235
236
HttpURLConnection httpUrlConnection,
236
237
SendPort sendPort,
237
238
int ? expectedBodyLength,
238
- ) {
239
+ ) async {
239
240
final responseCode = httpUrlConnection.getResponseCode ();
240
241
241
- final inputStream = (responseCode >= 200 && responseCode <= 299 )
242
- ? httpUrlConnection.getInputStream ()
243
- : httpUrlConnection.getErrorStream ();
242
+ final responseBodyStream = (responseCode >= 200 && responseCode <= 299 )
243
+ ? BufferedInputStream ( httpUrlConnection.getInputStream () )
244
+ : BufferedInputStream ( httpUrlConnection.getErrorStream () );
244
245
245
- int byte;
246
246
var actualBodyLength = 0 ;
247
- // TODO: inputStream.read() could throw IOException.
248
- while ((byte = inputStream.read ()) != - 1 ) {
249
- sendPort.send ([byte]);
250
- actualBodyLength++ ;
247
+ final bytesBuffer = JArray (jbyte.type, 4096 );
248
+
249
+ while (true ) {
250
+ // TODO: read1() could throw IOException.
251
+ final bytesCount =
252
+ responseBodyStream.read1 (bytesBuffer, 0 , bytesBuffer.length);
253
+
254
+ if (bytesCount == - 1 ) {
255
+ break ;
256
+ }
257
+
258
+ if (bytesCount == 0 ) {
259
+ // No more data is available without blocking so give other Isolates an
260
+ // opportunity to run.
261
+ await Future <void >.delayed (Duration .zero);
262
+ continue ;
263
+ }
264
+
265
+ sendPort.send (bytesBuffer.toUint8List (length: bytesCount));
266
+ actualBodyLength += bytesCount;
251
267
}
252
268
253
269
if (expectedBodyLength != null && actualBodyLength < expectedBodyLength) {
254
270
sendPort.send (ClientException ('Unexpected end of body' , requestUrl));
255
271
}
256
272
257
- inputStream .close ();
273
+ responseBodyStream .close ();
258
274
}
259
275
}
260
276
261
277
extension on Uint8List {
262
278
JArray <jbyte> toJArray () =>
263
279
JArray (jbyte.type, length)..setRange (0 , length, this );
264
280
}
281
+
282
+ extension on JArray <jbyte> {
283
+ Uint8List toUint8List ({int ? length}) {
284
+ length ?? = this .length;
285
+ final list = Uint8List (length);
286
+ for (var i = 0 ; i < length; i++ ) {
287
+ list[i] = this [i];
288
+ }
289
+ return list;
290
+ }
291
+ }
0 commit comments