forked from dart-lang/http
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pkgs/ok_http: Stream response bodies. (dart-lang#1233)
- Loading branch information
1 parent
e2e2170
commit 8c325b9
Showing
5 changed files
with
447 additions
and
26 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
pkgs/ok_http/android/src/main/kotlin/com/example/ok_http/AsyncInputStreamReader.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.