com.ejlchina.http.internal.ProcessInputStream Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of httputils Show documentation
Show all versions of httputils Show documentation
Http工具包,封装 OkHttp,链式用法、支持异步同步、响应JSON自动解析,回调线程切换、配置灵活、支持URL占位符、支持BaseUrl、外部依赖少、适用 JavaWeb 和 Android,教程全面、简单易用
package com.ejlchina.http.internal;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.Executor;
import com.ejlchina.http.OnCallback;
import com.ejlchina.http.Process;
public class ProcessInputStream extends InputStream {
private InputStream input;
private OnCallback onProcess;
private Executor callbackExecutor;
private long stepBytes;
private long step = 0;
private RealProcess process;
private boolean doneCalled = false;
public ProcessInputStream(InputStream input, OnCallback onProcess, long totalBytes, long stepBytes,
long doneBytes, Executor callbackExecutor) {
this.input = input;
this.onProcess = onProcess;
this.stepBytes = stepBytes;
this.callbackExecutor = callbackExecutor;
this.process = new RealProcess(totalBytes, doneBytes);
this.step = doneBytes / stepBytes;
}
@Override
public int read() throws IOException {
int data = input.read();
if (data > -1) {
process.increaseDoneBytes();
}
if (process.notDoneOrReached(step * stepBytes)) {
return data;
}
if (process.isDone()) {
if (doneCalled) {
return data;
}
doneCalled = true;
}
step++;
callbackExecutor.execute(() -> {
onProcess.on(process);
});
return data;
}
}