Skip to content

Commit

Permalink
Convert Network Module [1]
Browse files Browse the repository at this point in the history
Summary:
Convert the first few files in the module

Changelog: [Internal]

Reviewed By: zeyap

Differential Revision: D55802173
  • Loading branch information
Thomas Nardone authored and facebook-github-bot committed Apr 23, 2024
1 parent a3ebd92 commit 6c99779
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 142 deletions.

This file was deleted.

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()
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,55 +13,47 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.facebook.react.modules.network

package com.facebook.react.modules.network;

import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.FilterOutputStream
import java.io.IOException
import java.io.OutputStream

/**
* An OutputStream that counts the number of bytes written.
*
* @author Chris Nokleberg
* @since 1.0
*/
class CountingOutputStream extends FilterOutputStream {

private long mCount;

/**
* Constructs a new {@code FilterOutputStream} with {@code out} as its target stream.
*
* @param out the target stream that this stream writes to.
*/
public CountingOutputStream(OutputStream out) {
super(out);
mCount = 0;
}
internal open class CountingOutputStream
/**
* Constructs a new `FilterOutputStream` with `out` as its target stream.
*
* @param out the target stream that this stream writes to.
*/
(out: OutputStream?) : FilterOutputStream(out) {

/** Returns the number of bytes written. */
public long getCount() {
return mCount;
}
var count: Long = 0
private set

@Override
public void write(byte[] b, int off, int len) throws IOException {
out.write(b, off, len);
mCount += len;
@Throws(IOException::class)
override fun write(b: ByteArray, off: Int, len: Int) {
out.write(b, off, len)
count += len.toLong()
}

@Override
public void write(int b) throws IOException {
out.write(b);
mCount++;
@Throws(IOException::class)
override fun write(b: Int) {
out.write(b)
count++
}

// Overriding close() because FilterOutputStream's close() method pre-JDK8 has bad behavior:
// it silently ignores any exception thrown by flush(). Instead, just close the delegate stream.
// It should flush itself if necessary.
@Override
public void close() throws IOException {
out.close();
@Throws(IOException::class)
override fun close() {
out.close()
}
}

This file was deleted.

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)
}

This file was deleted.

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)
}
})
}

0 comments on commit 6c99779

Please sign in to comment.