From e0a7f41c1cbf35e51fb0d8735e5fb58c12c5d478 Mon Sep 17 00:00:00 2001 From: vvb2060 Date: Mon, 20 Apr 2026 03:33:46 +0800 Subject: [PATCH] fix DataSourceChannel --- .../magisk/core/utils/DataSourceChannel.java | 30 +++++++++---------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/app/core/src/main/java/com/topjohnwu/magisk/core/utils/DataSourceChannel.java b/app/core/src/main/java/com/topjohnwu/magisk/core/utils/DataSourceChannel.java index a7899c839..3f3e99382 100644 --- a/app/core/src/main/java/com/topjohnwu/magisk/core/utils/DataSourceChannel.java +++ b/app/core/src/main/java/com/topjohnwu/magisk/core/utils/DataSourceChannel.java @@ -23,8 +23,9 @@ public class DataSourceChannel implements SeekableByteChannel { private final OkHttpClient client; private final String url; private final FileChannel fileChannel; + private final boolean ownership; private final long startOffset; - private final long size; + private long size; private long position = 0; private boolean open = true; @@ -33,23 +34,21 @@ public class DataSourceChannel implements SeekableByteChannel { private long cacheStart = -1; private DataSourceChannel(OkHttpClient client, String url, FileChannel fileChannel, - long startOffset, long size) { + boolean ownership, long startOffset, long size) { this.client = client; this.url = url; this.fileChannel = fileChannel; + this.ownership = ownership; this.startOffset = startOffset; this.size = size; } public DataSourceChannel(FileChannel fileChannel) throws IOException { - this(null, null, fileChannel, 0, fileChannel.size()); + this(null, null, fileChannel, true, 0, fileChannel.size()); } public DataSourceChannel(OkHttpClient client, String url) throws IOException { - this(client, url, null, 0, fetchTotalSize(client, url)); - } - - private static long fetchTotalSize(OkHttpClient client, String url) throws IOException { + this(client, url, null, false, 0, 0); var request = new Request.Builder() .url(url) .header("Range", "bytes=" + "-" + RANDOM_READ_CACHE_SIZE) @@ -63,27 +62,26 @@ public class DataSourceChannel implements SeekableByteChannel { throw new IOException("Could not determine file size."); } var contentLength = contentRange.substring(contentRange.lastIndexOf('/') + 1); - var size = Long.parseLong(contentLength); + size = Long.parseLong(contentLength); cache = response.body().bytes(); cacheStart = size - cache.length; - return size; } } public DataSourceChannel slice(long offset, long sliceSize) { - if (offset == 0 && sliceSize == size) { - return this; - } - if (offset < 0 || sliceSize <= 0 || offset + sliceSize >= size) { + if (offset < 0 || sliceSize <= 0 || offset + sliceSize > size) { throw new IllegalArgumentException("Invalid slice parameters"); } - return new DataSourceChannel(client, url, fileChannel, startOffset + offset, sliceSize); + return new DataSourceChannel(client, url, fileChannel, false, + startOffset + offset, sliceSize); } @Override public int read(ByteBuffer dst) throws IOException { var bytesRead = read(dst, position); - position += bytesRead; + if (bytesRead > 0) { + position += bytesRead; + } return bytesRead; } @@ -253,7 +251,7 @@ public class DataSourceChannel implements SeekableByteChannel { public void close() throws IOException { open = false; cache = null; - if (fileChannel != null) { + if (ownership) { fileChannel.close(); } }