mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-10-16 06:45:08 +00:00
Update net module
This commit is contained in:
51
net/src/main/java/com/topjohnwu/net/BadRequest.java
Normal file
51
net/src/main/java/com/topjohnwu/net/BadRequest.java
Normal file
@@ -0,0 +1,51 @@
|
||||
package com.topjohnwu.net;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
class BadRequest extends Request {
|
||||
|
||||
private IOException ex;
|
||||
|
||||
BadRequest(IOException e) { super(null); ex = e; }
|
||||
|
||||
@Override
|
||||
public Request addHeaders(String key, String value) { return this; }
|
||||
|
||||
@Override
|
||||
public Result<InputStream> execForInputStream() { fail(); return new Result<>(); }
|
||||
|
||||
@Override
|
||||
public void getAsFile(File out, ResponseListener<File> rs) { fail(); }
|
||||
|
||||
@Override
|
||||
public void execForFile(File out) { fail(); }
|
||||
|
||||
@Override
|
||||
public void getAsString(ResponseListener<String> rs) { fail(); }
|
||||
|
||||
@Override
|
||||
public Result<String> execForString() { fail(); return new Result<>(); }
|
||||
|
||||
@Override
|
||||
public void getAsJSONObject(ResponseListener<JSONObject> rs) { fail(); }
|
||||
|
||||
@Override
|
||||
public Result<JSONObject> execForJSONObject() { fail(); return new Result<>(); }
|
||||
|
||||
@Override
|
||||
public void getAsJSONArray(ResponseListener<JSONArray> rs) { fail(); }
|
||||
|
||||
@Override
|
||||
public Result<JSONArray> execForJSONArray() { fail(); return new Result<>(); }
|
||||
|
||||
private void fail() {
|
||||
if (err != null)
|
||||
err.onError(null, ex);
|
||||
}
|
||||
}
|
@@ -27,7 +27,7 @@ public class Networking {
|
||||
conn.setConnectTimeout(CONNECT_TIMEOUT);
|
||||
return new Request(conn);
|
||||
} catch (IOException e) {
|
||||
return new StubRequest();
|
||||
return new BadRequest(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -8,16 +8,12 @@ public class ProgressInputStream extends FilterInputStream {
|
||||
|
||||
private long totalBytes;
|
||||
private long bytesDownloaded;
|
||||
private DownloadProgressListener progress;
|
||||
|
||||
public ProgressInputStream(InputStream in, long total) {
|
||||
public ProgressInputStream(InputStream in, long total, DownloadProgressListener listener) {
|
||||
super(in);
|
||||
totalBytes = total;
|
||||
}
|
||||
|
||||
protected void updateProgress(long bytesDownloaded, long totalBytes) {}
|
||||
|
||||
private void update() {
|
||||
Networking.mainHandler.post(() -> updateProgress(bytesDownloaded, totalBytes));
|
||||
progress = listener;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -25,7 +21,7 @@ public class ProgressInputStream extends FilterInputStream {
|
||||
int b = super.read();
|
||||
if (b >= 0) {
|
||||
bytesDownloaded++;
|
||||
update();
|
||||
Networking.mainHandler.post(() -> progress.onProgress(bytesDownloaded, totalBytes));
|
||||
}
|
||||
return b;
|
||||
}
|
||||
@@ -40,7 +36,7 @@ public class ProgressInputStream extends FilterInputStream {
|
||||
int sz = super.read(b, off, len);
|
||||
if (sz > 0) {
|
||||
bytesDownloaded += sz;
|
||||
update();
|
||||
Networking.mainHandler.post(() -> progress.onProgress(bytesDownloaded, totalBytes));
|
||||
}
|
||||
return sz;
|
||||
}
|
||||
|
@@ -23,8 +23,9 @@ public class Request {
|
||||
private HttpURLConnection conn;
|
||||
private Executor executor = null;
|
||||
private DownloadProgressListener progress = null;
|
||||
private ErrorHandler err = null;
|
||||
private int code;
|
||||
private int code = -1;
|
||||
|
||||
ErrorHandler err = null;
|
||||
|
||||
private interface Requestor<T> {
|
||||
T request() throws Exception;
|
||||
@@ -74,7 +75,7 @@ public class Request {
|
||||
return exec(this::getInputStream);
|
||||
}
|
||||
|
||||
public void getAsFile(ResponseListener<File> rs, File out) {
|
||||
public void getAsFile(File out, ResponseListener<File> rs) {
|
||||
submit(() -> dlFile(out), rs);
|
||||
}
|
||||
|
||||
@@ -110,7 +111,10 @@ public class Request {
|
||||
Result<T> res = new Result<>();
|
||||
try {
|
||||
res.result = req.request();
|
||||
} catch (Exception ignored) {}
|
||||
} catch (Exception e) {
|
||||
if (err != null)
|
||||
err.onError(conn, e);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -135,12 +139,7 @@ public class Request {
|
||||
code = conn.getResponseCode();
|
||||
InputStream in = conn.getInputStream();
|
||||
if (progress != null) {
|
||||
in = new ProgressInputStream(in, conn.getContentLength()) {
|
||||
@Override
|
||||
protected void updateProgress(long bytesDownloaded, long totalBytes) {
|
||||
progress.onProgress(bytesDownloaded, totalBytes);
|
||||
}
|
||||
|
||||
in = new ProgressInputStream(in, conn.getContentLength(), progress) {
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
super.close();
|
||||
|
@@ -1,49 +0,0 @@
|
||||
package com.topjohnwu.net;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
class StubRequest extends Request {
|
||||
|
||||
StubRequest() { super(null); }
|
||||
|
||||
@Override
|
||||
public Request addHeaders(String key, String value) { return this; }
|
||||
|
||||
@Override
|
||||
public Request setDownloadProgressListener(DownloadProgressListener listener) { return this; }
|
||||
|
||||
@Override
|
||||
public Request setErrorHandler(ErrorHandler handler) { return this; }
|
||||
|
||||
@Override
|
||||
public Request setExecutor(Executor e) { return this; }
|
||||
|
||||
@Override
|
||||
public Result<InputStream> execForInputStream() { return new Result<>(); }
|
||||
|
||||
@Override
|
||||
public void getAsFile(ResponseListener<File> rs, File out) {}
|
||||
|
||||
@Override
|
||||
public void getAsString(ResponseListener<String> rs) {}
|
||||
|
||||
@Override
|
||||
public Result<String> execForString() { return new Result<>(); }
|
||||
|
||||
@Override
|
||||
public void getAsJSONObject(ResponseListener<JSONObject> rs){}
|
||||
|
||||
@Override
|
||||
public Result<JSONObject> execForJSONObject() { return new Result<>(); }
|
||||
|
||||
@Override
|
||||
public void getAsJSONArray(ResponseListener<JSONArray> rs){}
|
||||
|
||||
@Override
|
||||
public Result<JSONArray> execForJSONArray() { return new Result<>(); }
|
||||
}
|
Reference in New Issue
Block a user