forked from facebook/react-native
-
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.
Summary: Convert the first few files in the module Changelog: [Internal] Reviewed By: zeyap Differential Revision: D55802173
- Loading branch information
1 parent
37eef32
commit 75e257b
Showing
7 changed files
with
128 additions
and
142 deletions.
There are no files selected for viewing
17 changes: 0 additions & 17 deletions
17
...ive/ReactAndroid/src/main/java/com/facebook/react/modules/network/CookieJarContainer.java
This file was deleted.
Oops, something went wrong.
17 changes: 17 additions & 0 deletions
17
...ative/ReactAndroid/src/main/java/com/facebook/react/modules/network/CookieJarContainer.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,17 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
package com.facebook.react.modules.network | ||
|
||
import okhttp3.CookieJar | ||
|
||
public interface CookieJarContainer : CookieJar { | ||
|
||
public fun setCookieJar(cookieJar: CookieJar?) | ||
|
||
public fun removeCookieJar() | ||
} |
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
14 changes: 0 additions & 14 deletions
14
...ve/ReactAndroid/src/main/java/com/facebook/react/modules/network/CustomClientBuilder.java
This file was deleted.
Oops, something went wrong.
15 changes: 15 additions & 0 deletions
15
...tive/ReactAndroid/src/main/java/com/facebook/react/modules/network/CustomClientBuilder.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,15 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
package com.facebook.react.modules.network | ||
|
||
import okhttp3.OkHttpClient | ||
import okhttp3.OkHttpClient.Builder | ||
|
||
public fun interface CustomClientBuilder { | ||
public fun apply(builder: OkHttpClient.Builder) | ||
} |
79 changes: 0 additions & 79 deletions
79
...ve/ReactAndroid/src/main/java/com/facebook/react/modules/network/ProgressRequestBody.java
This file was deleted.
Oops, something went wrong.
72 changes: 72 additions & 0 deletions
72
...tive/ReactAndroid/src/main/java/com/facebook/react/modules/network/ProgressRequestBody.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,72 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
package com.facebook.react.modules.network | ||
|
||
import java.io.IOException | ||
import okhttp3.MediaType | ||
import okhttp3.RequestBody | ||
import okio.BufferedSink | ||
import okio.Okio | ||
import okio.Okio.sink | ||
import okio.Sink | ||
|
||
internal class ProgressRequestBody( | ||
private val requestBody: RequestBody, | ||
private val progressListener: ProgressListener | ||
) : RequestBody() { | ||
|
||
private var _contentLength = 0L | ||
|
||
override fun contentType(): MediaType? = requestBody.contentType() | ||
|
||
@Throws(IOException::class) | ||
override fun contentLength(): Long { | ||
if (_contentLength == 0L) { | ||
_contentLength = requestBody.contentLength() | ||
} | ||
return _contentLength | ||
} | ||
|
||
@Throws(IOException::class) | ||
override fun writeTo(sink: BufferedSink) { | ||
// In 99% of cases, this method is called strictly once. | ||
// The only case when it is called more than once is internal okhttp upload re-try. | ||
// We need to re-create CountingOutputStream in this case as progress should be re-evaluated. | ||
val sinkWrapper = Okio.buffer(outputStreamSink(sink)) | ||
|
||
// contentLength changes for input streams, since we're using inputStream.available(), | ||
// so get the length before writing to the sink | ||
contentLength() | ||
requestBody.writeTo(sinkWrapper) | ||
sinkWrapper.flush() | ||
} | ||
|
||
private fun outputStreamSink(sink: BufferedSink): Sink = | ||
Okio.sink( | ||
object : CountingOutputStream(sink.outputStream()) { | ||
@Throws(IOException::class) | ||
override fun write(b: ByteArray, off: Int, len: Int) { | ||
super.write(b, off, len) | ||
sendProgressUpdate() | ||
} | ||
|
||
@Throws(IOException::class) | ||
override fun write(b: Int) { | ||
super.write(b) | ||
sendProgressUpdate() | ||
} | ||
|
||
@Throws(IOException::class) | ||
private fun sendProgressUpdate() { | ||
val bytesWritten = count | ||
val contentLength = contentLength() | ||
progressListener.onProgress( | ||
bytesWritten, contentLength, bytesWritten == contentLength) | ||
} | ||
}) | ||
} |