Skip to content

Commit 4e11a9c

Browse files
committed
init
1 parent 79596a9 commit 4e11a9c

File tree

1 file changed

+35
-1
lines changed
  • packages/react-native-fast-io/android/src/main/java/com/margelo/nitro/fastio

1 file changed

+35
-1
lines changed

packages/react-native-fast-io/android/src/main/java/com/margelo/nitro/fastio/HybridNetwork.kt

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,44 @@
11
package com.margelo.nitro.fastio
22

3+
import android.util.Log
34
import com.margelo.nitro.core.Promise
5+
import java.net.HttpURLConnection
6+
import java.net.URL
47

58
class HybridNetwork : HybridNetworkSpec() {
69
override fun request(opts: RequestOptions): Promise<Unit> {
7-
throw NotImplementedError("HybridNetwork.request() not implemented")
10+
return Promise.async {
11+
val connection = URL(opts.url).openConnection() as HttpURLConnection
12+
13+
connection.apply {
14+
requestMethod = opts.method.name.uppercase()
15+
doInput = true
16+
17+
opts.body?.let { hybridStream ->
18+
(hybridStream as HybridInputStream).stream.use { input ->
19+
outputStream.use { output ->
20+
val buffer = ByteArray(HybridStreamFactory.BUFFER_SIZE)
21+
var bytesRead: Int
22+
23+
while (input.read(buffer).also { bytesRead = it } != -1) {
24+
output.write(buffer, 0, bytesRead)
25+
output.flush() // Important: flush each chunk
26+
}
27+
}
28+
}
29+
}
30+
31+
connect()
32+
33+
if (responseCode in 200..299) {
34+
// tbd
35+
} else {
36+
throw Error("HTTP Error: $responseCode")
37+
}
38+
}
39+
40+
connection.disconnect()
41+
}
842
}
943

1044
override val memorySize: Long

0 commit comments

Comments
 (0)