From 557a280acce73f1f512bfca5d0f15686917113cc Mon Sep 17 00:00:00 2001 From: jhengazuji Date: Mon, 17 Nov 2025 15:59:00 +0800 Subject: [PATCH] Clean code --- .../com/fongmi/android/tv/api/EpgParser.java | 2 +- .../android/tv/api/loader/JarLoader.java | 11 +---- .../android/tv/player/extractor/TVBus.java | 7 ++- .../com/fongmi/android/tv/utils/Download.java | 9 ++++ .../java/com/github/catvod/net/OkHttp.java | 10 ---- .../java/com/github/catvod/utils/Path.java | 35 ++++++++------ .../java/com/fongmi/quickjs/utils/Module.java | 47 +++++-------------- 7 files changed, 47 insertions(+), 74 deletions(-) diff --git a/app/src/main/java/com/fongmi/android/tv/api/EpgParser.java b/app/src/main/java/com/fongmi/android/tv/api/EpgParser.java index e7006346f..afa9bdcc9 100644 --- a/app/src/main/java/com/fongmi/android/tv/api/EpgParser.java +++ b/app/src/main/java/com/fongmi/android/tv/api/EpgParser.java @@ -36,7 +36,7 @@ public class EpgParser { public static boolean start(Live live, String url) throws Exception { File file = Path.epg(Uri.parse(url).getLastPathSegment()); - if (shouldDownload(file)) Download.create(url, file).start(); + Download.create(url, file).get(shouldDownload(file)); if (file.getName().endsWith(".gz")) readGzip(live, file); else readXml(live, file); return true; diff --git a/app/src/main/java/com/fongmi/android/tv/api/loader/JarLoader.java b/app/src/main/java/com/fongmi/android/tv/api/loader/JarLoader.java index cbd6aa010..ed97e858b 100644 --- a/app/src/main/java/com/fongmi/android/tv/api/loader/JarLoader.java +++ b/app/src/main/java/com/fongmi/android/tv/api/loader/JarLoader.java @@ -3,6 +3,7 @@ package com.fongmi.android.tv.api.loader; import android.content.Context; import com.fongmi.android.tv.App; +import com.fongmi.android.tv.utils.Download; import com.fongmi.android.tv.utils.UrlUtil; import com.github.catvod.crawler.Spider; import com.github.catvod.crawler.SpiderNull; @@ -78,14 +79,6 @@ public class JarLoader { } } - private File download(String url) { - try { - return Path.write(Path.jar(url), OkHttp.bytes(url)); - } catch (Exception e) { - return Path.jar(url); - } - } - public synchronized void parseJar(String key, String jar) { if (loaders.containsKey(key)) return; String[] texts = jar.split(";md5;"); @@ -95,7 +88,7 @@ public class JarLoader { if (!md5.isEmpty() && Util.equals(jar, md5)) { load(key, Path.jar(jar)); } else if (jar.startsWith("http")) { - load(key, download(jar)); + load(key, Download.create(jar, Path.jar(jar)).get()); } else if (jar.startsWith("file")) { load(key, Path.local(jar)); } else if (jar.startsWith("assets")) { diff --git a/app/src/main/java/com/fongmi/android/tv/player/extractor/TVBus.java b/app/src/main/java/com/fongmi/android/tv/player/extractor/TVBus.java index ecd91653a..3b803b902 100644 --- a/app/src/main/java/com/fongmi/android/tv/player/extractor/TVBus.java +++ b/app/src/main/java/com/fongmi/android/tv/player/extractor/TVBus.java @@ -9,9 +9,9 @@ import com.fongmi.android.tv.api.config.LiveConfig; import com.fongmi.android.tv.bean.Core; import com.fongmi.android.tv.exception.ExtractException; import com.fongmi.android.tv.player.Source; +import com.fongmi.android.tv.utils.Download; import com.fongmi.android.tv.utils.ResUtil; import com.fongmi.android.tv.utils.UrlUtil; -import com.github.catvod.net.OkHttp; import com.github.catvod.utils.Path; import com.google.gson.JsonObject; import com.orhanobut.logger.Logger; @@ -47,9 +47,8 @@ public class TVBus implements Source.Extractor, Listener { } private String getPath(String url) { - String name = UrlUtil.path(url); - File file = new File(Path.so(), name); - if (file.length() < 10240) Path.write(file, OkHttp.bytes(url)); + File file = new File(Path.so(), UrlUtil.path(url)); + Download.create(url, file).get(!Path.exists(file)); return file.getAbsolutePath(); } diff --git a/app/src/main/java/com/fongmi/android/tv/utils/Download.java b/app/src/main/java/com/fongmi/android/tv/utils/Download.java index 50903b2cb..1ec43e2c9 100644 --- a/app/src/main/java/com/fongmi/android/tv/utils/Download.java +++ b/app/src/main/java/com/fongmi/android/tv/utils/Download.java @@ -32,6 +32,15 @@ public class Download { this.callback = callback; } + public File get() { + return get(true); + } + + public File get(boolean force) { + if (force) performSync(); + return file; + } + public void start() { if (url.startsWith("file")) return; if (callback == null) performSync(); diff --git a/catvod/src/main/java/com/github/catvod/net/OkHttp.java b/catvod/src/main/java/com/github/catvod/net/OkHttp.java index 44d113329..008670819 100644 --- a/catvod/src/main/java/com/github/catvod/net/OkHttp.java +++ b/catvod/src/main/java/com/github/catvod/net/OkHttp.java @@ -135,16 +135,6 @@ public class OkHttp { } } - public static byte[] bytes(String url) { - if (!url.startsWith("http")) return new byte[0]; - try (Response res = newCall(url).execute()) { - return res.body().bytes(); - } catch (Exception e) { - e.printStackTrace(); - return new byte[0]; - } - } - public static Call newCall(String url) { return client().newCall(new Request.Builder().url(url).build()); } diff --git a/catvod/src/main/java/com/github/catvod/utils/Path.java b/catvod/src/main/java/com/github/catvod/utils/Path.java index 06e9b0f99..ce6fe3e86 100644 --- a/catvod/src/main/java/com/github/catvod/utils/Path.java +++ b/catvod/src/main/java/com/github/catvod/utils/Path.java @@ -128,14 +128,17 @@ public class Path { } public static String read(File file) { - return new String(readToByte(file), StandardCharsets.UTF_8); + try { + return new String(readToByte(file), StandardCharsets.UTF_8); + } catch (Exception e) { + return ""; + } } public static String read(InputStream is) { try { return new String(readToByte(is), StandardCharsets.UTF_8); } catch (IOException e) { - e.printStackTrace(); return ""; } } @@ -149,23 +152,31 @@ public class Path { } private static byte[] readToByte(InputStream is) throws IOException { - try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) { + try (InputStream input = is; ByteArrayOutputStream bos = new ByteArrayOutputStream()) { int read; byte[] buffer = new byte[16384]; - while ((read = is.read(buffer)) != -1) bos.write(buffer, 0, read); + while ((read = input.read(buffer)) != -1) bos.write(buffer, 0, read); return bos.toByteArray(); } } + public static File write(File file, InputStream is) { + try (InputStream input = is; FileOutputStream output = new FileOutputStream(create(file))) { + int read; + byte[] buffer = new byte[16384]; + while ((read = input.read(buffer)) != -1) output.write(buffer, 0, read); + return file; + } catch (IOException e) { + return file; + } + } + public static File write(File file, byte[] data) { - try { - FileOutputStream fos = new FileOutputStream(create(file)); + try (FileOutputStream fos = new FileOutputStream(create(file))) { fos.write(data); fos.flush(); - fos.close(); return file; } catch (IOException e) { - e.printStackTrace(); return file; } } @@ -184,13 +195,10 @@ public class Path { } public static void copy(InputStream in, File out) { - try { + try (InputStream input = in; FileOutputStream output = new FileOutputStream(create(out))) { int read; byte[] buffer = new byte[16384]; - FileOutputStream fos = new FileOutputStream(create(out)); - while ((read = in.read(buffer)) != -1) fos.write(buffer, 0, read); - fos.close(); - in.close(); + while ((read = input.read(buffer)) != -1) output.write(buffer, 0, read); } catch (IOException ignored) { } } @@ -227,7 +235,6 @@ public class Path { Shell.exec("chmod 777 " + file); return file; } catch (IOException e) { - e.printStackTrace(); return file; } } diff --git a/quickjs/src/main/java/com/fongmi/quickjs/utils/Module.java b/quickjs/src/main/java/com/fongmi/quickjs/utils/Module.java index 135a8cac6..8e935544a 100644 --- a/quickjs/src/main/java/com/fongmi/quickjs/utils/Module.java +++ b/quickjs/src/main/java/com/fongmi/quickjs/utils/Module.java @@ -1,18 +1,15 @@ package com.fongmi.quickjs.utils; -import android.net.Uri; +import android.text.TextUtils; +import android.util.LruCache; import com.github.catvod.net.OkHttp; import com.github.catvod.utils.Asset; -import com.github.catvod.utils.Path; - -import java.io.File; -import java.nio.charset.StandardCharsets; -import java.util.concurrent.ConcurrentHashMap; public class Module { - private final ConcurrentHashMap cache; + private final LruCache cache; + private static final int MAX_SIZE = 50; private static class Loader { static volatile Module INSTANCE = new Module(); @@ -23,37 +20,15 @@ public class Module { } public Module() { - this.cache = new ConcurrentHashMap<>(); + this.cache = new LruCache<>(MAX_SIZE); } public String fetch(String name) { - if (cache.contains(name)) return cache.get(name); - if (name.startsWith("http")) cache.put(name, request(name)); - if (name.startsWith("assets")) cache.put(name, Asset.read(name)); - if (name.startsWith("lib/")) cache.put(name, Asset.read("js/" + name)); - return cache.get(name); - } - - private String request(String url) { - try { - Uri uri = Uri.parse(url); - byte[] data = OkHttp.bytes(url); - File file = Path.js(uri.getLastPathSegment()); - boolean cache = !"127.0.0.1".equals(uri.getHost()); - if (cache) new Thread(() -> Path.write(file, data)).start(); - return new String(data, StandardCharsets.UTF_8); - } catch (Exception e) { - return cache(url); - } - } - - private String cache(String url) { - try { - Uri uri = Uri.parse(url); - File file = Path.js(uri.getLastPathSegment()); - return Path.exists(file) ? Path.read(file) : ""; - } catch (Exception e) { - return ""; - } + String content = cache.get(name); + if (!TextUtils.isEmpty(content)) return content; + if (name.startsWith("http")) cache.put(name, content = OkHttp.string(name)); + else if (name.startsWith("assets")) cache.put(name, content = Asset.read(name)); + else if (name.startsWith("lib/")) cache.put(name, content = Asset.read("js/" + name)); + return content; } }