Skip to content

Commit dfeecf0

Browse files
authored
Convert a java.lang.OutOfMemoryError into a ClientException (#1299)
1 parent a41141d commit dfeecf0

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

pkgs/cronet_http/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 1.3.3-wip
2+
3+
* Throw `ClientException` if `CronetClient.send` runs out of Java heap while
4+
allocating memory for the request body.
5+
16
## 1.3.2
27

38
* Upgrade `package:jni` to 0.10.1 and `package:jnigen` to 0.10.0 to fix method

pkgs/cronet_http/lib/src/cronet_client.dart

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,22 @@ class CronetClient extends BaseClient {
392392
headers.forEach((k, v) => builder.addHeader(k.toJString(), v.toJString()));
393393

394394
if (body.isNotEmpty) {
395+
final JByteBuffer data;
396+
try {
397+
data = body.toJByteBuffer();
398+
} on JniException catch (e) {
399+
// There are no unit tests for this code. You can verify this behavior
400+
// manually by incrementally increasing the amount of body data in
401+
// `CronetClient.post` until you get this exception.
402+
if (e.message.contains('java.lang.OutOfMemoryError:')) {
403+
throw ClientException(
404+
'Not enough memory for request body: ${e.message}', request.url);
405+
}
406+
rethrow;
407+
}
408+
395409
builder.setUploadDataProvider(
396-
jb.UploadDataProviders.create2(body.toJByteBuffer()), _executor);
410+
jb.UploadDataProviders.create2(data), _executor);
397411
}
398412
builder.build().start();
399413
return responseCompleter.future;

pkgs/cronet_http/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: cronet_http
2-
version: 1.3.2
2+
version: 1.3.3-wip
33
description: >-
44
An Android Flutter plugin that provides access to the Cronet HTTP client.
55
repository: https://github.com/dart-lang/http/tree/master/pkgs/cronet_http

0 commit comments

Comments
 (0)