Skip to content

Commit

Permalink
pkgs/ok_http: Stream response bodies. (#1233)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anikate-De authored Jun 13, 2024
1 parent e2e2170 commit 8c325b9
Show file tree
Hide file tree
Showing 5 changed files with 447 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

package com.example.ok_http

import java.io.IOException
import java.io.InputStream
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import java.util.concurrent.Future


/**
* Callback interface utilized by the [AsyncInputStreamReader].
*/
interface DataCallback {
fun onDataRead(data: ByteArray)
fun onFinished()
fun onError(e: IOException)
}

/**
* Provides functions to read data from an InputStream asynchronously.
*/
class AsyncInputStreamReader {
private val executorService: ExecutorService = Executors.newSingleThreadExecutor()

/**
* Reads data from an InputStream asynchronously using an executor service.
*
* @param inputStream The InputStream to read from
* @param callback The DataCallback to call when data is read, finished, or an error occurs
*
* @return Future<*>
*/
fun readAsync(inputStream: InputStream, callback: DataCallback): Future<*> {
return executorService.submit {
try {
val buffer = ByteArray(4096)
var bytesRead: Int
while (inputStream.read(buffer).also { bytesRead = it } != -1) {
val byteArray = buffer.copyOfRange(0, bytesRead)
callback.onDataRead(byteArray)
}

} catch (e: IOException) {
callback.onError(e)
} finally {
try {
inputStream.close()
} catch (e: IOException) {
callback.onError(e)
}
callback.onFinished()
}
}
}

fun shutdown() {
executorService.shutdown()
}
}
21 changes: 8 additions & 13 deletions pkgs/ok_http/example/integration_test/client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,13 @@ void main() async {

Future<void> testConformance() async {
group('ok_http client', () {
testRequestBody(OkHttpClient());
testResponseBody(OkHttpClient(), canStreamResponseBody: false);
testRequestHeaders(OkHttpClient());
testRequestMethods(OkHttpClient(), preservesMethodCase: true);
testResponseHeaders(OkHttpClient(), supportsFoldedHeaders: false);
testResponseStatusLine(OkHttpClient());
testCompressedResponseBody(OkHttpClient());
testRedirect(OkHttpClient());
testServerErrors(OkHttpClient());
testClose(OkHttpClient.new);
testIsolate(OkHttpClient.new);
testRequestCookies(OkHttpClient(), canSendCookieHeaders: true);
testResponseCookies(OkHttpClient(), canReceiveSetCookieHeaders: true);
testAll(
OkHttpClient.new,
canStreamRequestBody: false,
preservesMethodCase: true,
supportsFoldedHeaders: false,
canSendCookieHeaders: true,
canReceiveSetCookieHeaders: true,
);
});
}
2 changes: 2 additions & 0 deletions pkgs/ok_http/jnigen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ classes:
- "okhttp3.Dispatcher"
- "okhttp3.Cache"
- "com.example.ok_http.RedirectInterceptor"
- "com.example.ok_http.AsyncInputStreamReader"
- "com.example.ok_http.DataCallback"

# Exclude the deprecated methods listed below
# They cause syntax errors during the `dart format` step of JNIGen.
Expand Down
Loading

0 comments on commit 8c325b9

Please sign in to comment.