Skip to content

Commit 3339a52

Browse files
committed
delete IOParser.kt
1 parent 56c7ba2 commit 3339a52

5 files changed

Lines changed: 51 additions & 65 deletions

File tree

app/build/generated/source/kapt/debug/rxhttp/wrapper/param/ObservableParser.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import rxhttp.wrapper.callback.ProgressCallback;
2525
import rxhttp.wrapper.entity.Progress;
2626
import rxhttp.wrapper.entity.ProgressT;
27-
import rxhttp.wrapper.parse.IOParser;
27+
import rxhttp.wrapper.parse.StreamParser;
2828
import rxhttp.wrapper.parse.Parser;
2929

3030
public final class ObservableParser<T> extends Observable<T> {
@@ -65,8 +65,8 @@ private static final class SyncParserObserver<T> implements Observer<Progress>,
6565
this.parser = parser;
6666
this.progressConsumer = progressConsumer;
6767

68-
if (progressConsumer != null && parser instanceof IOParser) {
69-
((IOParser) parser).setCallback(this);
68+
if (progressConsumer != null && parser instanceof StreamParser) {
69+
((StreamParser) parser).setCallback(this);
7070
}
7171
}
7272

@@ -176,8 +176,8 @@ private static final class AsyncParserObserver<T> extends AtomicInteger
176176
this.progressConsumer = progressConsumer;
177177
queue = new SpscLinkedArrayQueue<>(128);
178178

179-
if (progressConsumer != null && parser instanceof IOParser) {
180-
((IOParser) parser).setCallback(this);
179+
if (progressConsumer != null && parser instanceof StreamParser) {
180+
((StreamParser) parser).setCallback(this);
181181
}
182182
}
183183

rxhttp-compiler/src/main/java/com/rxhttp/compiler/ClassHelper.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ object ClassHelper {
492492
import rxhttp.wrapper.callback.ProgressCallback;
493493
import rxhttp.wrapper.entity.Progress;
494494
import rxhttp.wrapper.entity.ProgressT;
495-
import rxhttp.wrapper.parse.IOParser;
495+
import rxhttp.wrapper.parse.StreamParser;
496496
import rxhttp.wrapper.parse.Parser;
497497
498498
public final class ObservableParser<T> extends Observable<T> {
@@ -533,8 +533,8 @@ object ClassHelper {
533533
this.parser = parser;
534534
this.progressConsumer = progressConsumer;
535535
536-
if (progressConsumer != null && parser instanceof IOParser) {
537-
((IOParser) parser).setCallback(this);
536+
if (progressConsumer != null && parser instanceof StreamParser) {
537+
((StreamParser) parser).setCallback(this);
538538
}
539539
}
540540
@@ -644,8 +644,8 @@ object ClassHelper {
644644
this.progressConsumer = progressConsumer;
645645
queue = new SpscLinkedArrayQueue<>(128);
646646
647-
if (progressConsumer != null && parser instanceof IOParser) {
648-
((IOParser) parser).setCallback(this);
647+
if (progressConsumer != null && parser instanceof StreamParser) {
648+
((StreamParser) parser).setCallback(this);
649649
}
650650
}
651651

rxhttp/src/main/java/rxhttp/wrapper/parse/IOParser.kt

Lines changed: 0 additions & 46 deletions
This file was deleted.
Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,32 @@
11
package rxhttp.wrapper.parse
22

33
import okhttp3.Response
4+
import okhttp3.ResponseBody
5+
import rxhttp.wrapper.OkHttpCompat
46
import rxhttp.wrapper.callback.FileOutputStreamFactory
57
import rxhttp.wrapper.callback.OutputStreamFactory
8+
import rxhttp.wrapper.callback.ProgressCallback
69
import rxhttp.wrapper.callback.UriOutputStreamFactory
10+
import rxhttp.wrapper.entity.Progress
711
import rxhttp.wrapper.exception.ExceptionHelper
12+
import rxhttp.wrapper.utils.IOUtil
813
import rxhttp.wrapper.utils.LogUtil
14+
import java.io.IOException
15+
import java.io.OutputStream
916

1017
/**
1118
* User: ljx
1219
* Date: 2020/9/4
1320
* Time: 21:39
1421
*/
15-
class StreamParser(
16-
private val osFactory: OutputStreamFactory
17-
) : IOParser() {
22+
class StreamParser @JvmOverloads constructor(
23+
private val osFactory: OutputStreamFactory,
24+
var callback: ProgressCallback? = null
25+
) : Parser<String> {
1826

1927
override fun onParse(response: Response): String {
2028
val body = ExceptionHelper.throwIfFatal(response)
21-
val os = osFactory.getOutputStream(response);
29+
val os = osFactory.getOutputStream(response)
2230
val msg = when (osFactory) {
2331
is FileOutputStreamFactory -> osFactory.localPath
2432
is UriOutputStreamFactory -> osFactory.uri.toString()
@@ -28,4 +36,29 @@ class StreamParser(
2836
response.writeTo(body, os, callback)
2937
return msg
3038
}
39+
}
40+
41+
42+
@Throws(IOException::class)
43+
fun Response.writeTo(
44+
body: ResponseBody,
45+
os: OutputStream,
46+
callback: ProgressCallback? = null
47+
) {
48+
val offsetSize = OkHttpCompat.getDownloadOffSize(this)?.offSize ?: 0
49+
val contentLength = OkHttpCompat.getContentLength(this) + offsetSize
50+
51+
var lastProgress = 0
52+
53+
IOUtil.write(body.byteStream(), os) {
54+
if (callback == null) return@write
55+
val currentSize = it + offsetSize
56+
//当前进度 = 当前已读取的字节 / 总字节
57+
val currentProgress = ((currentSize * 100f / contentLength)).toInt()
58+
if (currentProgress > lastProgress) {
59+
lastProgress = currentProgress
60+
val progress = Progress(currentProgress, currentSize, contentLength)
61+
callback.onProgress(progress)
62+
}
63+
}
3164
}

rxhttp/src/main/java/rxhttp/wrapper/parse/SuspendStreamParser.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class SuspendStreamParser(
3030
@Throws(IOException::class)
3131
suspend fun onParse(response: Response): String {
3232
val body = ExceptionHelper.throwIfFatal(response)
33-
val os = osFactory.getOutputStream(response);
33+
val os = osFactory.getOutputStream(response)
3434
val msg = when (osFactory) {
3535
is FileOutputStreamFactory -> osFactory.localPath
3636
is UriOutputStreamFactory -> osFactory.uri.toString()
@@ -50,19 +50,18 @@ suspend fun Response.writeTo(
5050
context: CoroutineContext? = null,
5151
progress: suspend (Progress) -> Unit
5252
) {
53-
val contentLength = OkHttpCompat.getContentLength(this)
5453
val offsetSize = OkHttpCompat.getDownloadOffSize(this)?.offSize ?: 0
55-
val newContentLength = contentLength + offsetSize
54+
val contentLength = OkHttpCompat.getContentLength(this) + offsetSize
5655

5756
var lastProgress = 0
5857

5958
IOUtil.suspendWrite(body.byteStream(), os) {
6059
val currentSize = it + offsetSize
6160
//当前进度 = 当前已读取的字节 / 总字节
62-
val currentProgress = ((currentSize * 100f / newContentLength)).toInt()
61+
val currentProgress = ((currentSize * 100f / contentLength)).toInt()
6362
if (currentProgress > lastProgress) {
6463
lastProgress = currentProgress
65-
val p = Progress(currentProgress, currentSize, newContentLength)
64+
val p = Progress(currentProgress, currentSize, contentLength)
6665
if (context != null) {
6766
withContext(context) { progress.invoke(p) }
6867
} else {

0 commit comments

Comments
 (0)