mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Migrate ProgressResponseBody to Kotlin (#49752)
Summary: Migrate com.facebook.react.modules.network.ProgressResponseBody to Kotlin. ## Changelog: [INTERNAL] - Migrate com.facebook.react.modules.network.ProgressResponseBody to Kotlin Pull Request resolved: https://github.com/facebook/react-native/pull/49752 Test Plan: ```bash yarn test-android yarn android ``` Reviewed By: javache Differential Revision: D70592532 Pulled By: alanleedev fbshipit-source-id: 2680951a3b5290314d0632eb55b69c9bcf44b96a
This commit is contained in:
committed by
Facebook GitHub Bot
parent
c6a075bcc7
commit
bf7a0e5798
@@ -3214,12 +3214,12 @@ public abstract interface class com/facebook/react/modules/network/ProgressListe
|
||||
public abstract fun onProgress (JJZ)V
|
||||
}
|
||||
|
||||
public class com/facebook/react/modules/network/ProgressResponseBody : okhttp3/ResponseBody {
|
||||
public final class com/facebook/react/modules/network/ProgressResponseBody : okhttp3/ResponseBody {
|
||||
public fun <init> (Lokhttp3/ResponseBody;Lcom/facebook/react/modules/network/ProgressListener;)V
|
||||
public fun contentLength ()J
|
||||
public fun contentType ()Lokhttp3/MediaType;
|
||||
public fun source ()Lokio/BufferedSource;
|
||||
public fun totalBytesRead ()J
|
||||
public final fun totalBytesRead ()J
|
||||
}
|
||||
|
||||
public final class com/facebook/react/modules/network/ReactCookieJarContainer : com/facebook/react/modules/network/CookieJarContainer {
|
||||
|
||||
-68
@@ -1,68 +0,0 @@
|
||||
/*
|
||||
* 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 androidx.annotation.Nullable;
|
||||
import java.io.IOException;
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.ResponseBody;
|
||||
import okio.Buffer;
|
||||
import okio.BufferedSource;
|
||||
import okio.ForwardingSource;
|
||||
import okio.Okio;
|
||||
import okio.Source;
|
||||
|
||||
public class ProgressResponseBody extends ResponseBody {
|
||||
|
||||
private final ResponseBody mResponseBody;
|
||||
private final ProgressListener mProgressListener;
|
||||
private @Nullable BufferedSource mBufferedSource;
|
||||
private long mTotalBytesRead;
|
||||
|
||||
public ProgressResponseBody(ResponseBody responseBody, ProgressListener progressListener) {
|
||||
mResponseBody = responseBody;
|
||||
mProgressListener = progressListener;
|
||||
mTotalBytesRead = 0L;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MediaType contentType() {
|
||||
return mResponseBody.contentType();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long contentLength() {
|
||||
return mResponseBody.contentLength();
|
||||
}
|
||||
|
||||
public long totalBytesRead() {
|
||||
return mTotalBytesRead;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BufferedSource source() {
|
||||
if (mBufferedSource == null) {
|
||||
mBufferedSource = Okio.buffer(source(mResponseBody.source()));
|
||||
}
|
||||
return mBufferedSource;
|
||||
}
|
||||
|
||||
private Source source(Source source) {
|
||||
return new ForwardingSource(source) {
|
||||
@Override
|
||||
public long read(Buffer sink, long byteCount) throws IOException {
|
||||
long bytesRead = super.read(sink, byteCount);
|
||||
// read() returns the number of bytes read, or -1 if this source is exhausted.
|
||||
mTotalBytesRead += bytesRead != -1 ? bytesRead : 0;
|
||||
mProgressListener.onProgress(
|
||||
mTotalBytesRead, mResponseBody.contentLength(), bytesRead == -1);
|
||||
return bytesRead;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* 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.ResponseBody
|
||||
import okio.Buffer
|
||||
import okio.BufferedSource
|
||||
import okio.ForwardingSource
|
||||
import okio.Okio
|
||||
import okio.Source
|
||||
|
||||
public class ProgressResponseBody
|
||||
public constructor(
|
||||
private val responseBody: ResponseBody,
|
||||
private val progressListener: ProgressListener
|
||||
) : ResponseBody() {
|
||||
private lateinit var bufferedSource: BufferedSource
|
||||
private var totalBytesRead = 0L
|
||||
|
||||
public override fun contentType(): MediaType? = responseBody.contentType()
|
||||
|
||||
override fun contentLength(): Long = responseBody.contentLength()
|
||||
|
||||
public fun totalBytesRead(): Long = totalBytesRead
|
||||
|
||||
public override fun source(): BufferedSource {
|
||||
if (!::bufferedSource.isInitialized) {
|
||||
bufferedSource = Okio.buffer(source(responseBody.source()))
|
||||
}
|
||||
return bufferedSource
|
||||
}
|
||||
|
||||
private fun source(source: Source): Source {
|
||||
return object : ForwardingSource(source) {
|
||||
@Throws(IOException::class)
|
||||
override fun read(sink: Buffer, byteCount: Long): Long {
|
||||
// read() returns the number of bytes read, or -1 if this source is exhausted.
|
||||
return super.read(sink, byteCount).also { bytesRead ->
|
||||
if (bytesRead != -1L) {
|
||||
totalBytesRead += bytesRead
|
||||
}
|
||||
progressListener.onProgress(
|
||||
totalBytesRead, responseBody.contentLength(), bytesRead == -1L)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user