Skip to content

Commit

Permalink
Let other isolates run if no data is available
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-james-dev committed Aug 21, 2023
1 parent 98b98d0 commit 660daf1
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions pkgs/java_http/lib/src/java_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@ class JavaClient extends BaseClient {
}

// TODO: Rename _isolateMethod to something more descriptive.
void _isolateMethod(
Future<void> _isolateMethod(
({
SendPort sendPort,
Uint8List body,
Map<String, String> headers,
String method,
Uri url,
}) request,
) {
) async {
final httpUrlConnection = URL
.ctor3(request.url.toString().toJString())
.openConnection()
Expand Down Expand Up @@ -141,7 +141,7 @@ class JavaClient extends BaseClient {
responseHeaders,
);

_responseBody(
await _responseBody(
request.url,
httpUrlConnection,
request.sendPort,
Expand Down Expand Up @@ -231,12 +231,12 @@ class JavaClient extends BaseClient {
return contentLength;
}

void _responseBody(
Future<void> _responseBody(
Uri requestUrl,
HttpURLConnection httpUrlConnection,
SendPort sendPort,
int? expectedBodyLength,
) {
) async {
final responseCode = httpUrlConnection.getResponseCode();

final inputStream = (responseCode >= 200 && responseCode <= 299)
Expand All @@ -251,7 +251,11 @@ class JavaClient extends BaseClient {
// TODO: bufferedInputStream.read() could throw IOException.
while ((bytesRead = bufferedInputStream.read1(buffer, 0, buffer.length)) !=
-1) {
if (bytesRead == 0) continue;
if (bytesRead == 0) {
// No more data is available without blocking so give other Isolates an
// opportunity to run.
await Future<void>.delayed(Duration.zero);
}

// Convert from Java array to Dart Uint8List
final byteArray = Uint8List(bytesRead);
Expand Down

0 comments on commit 660daf1

Please sign in to comment.