Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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