From 7ae452cf13bbb56dcdcb32911d5fa7a27c7209dd Mon Sep 17 00:00:00 2001 From: tarsin Date: Wed, 18 Jan 2023 12:29:19 +0800 Subject: [PATCH] Fixup! Use FileChannel transferFrom --- .../hippo/ehviewer/spider/SpiderQueen.java | 35 +++++++++---------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/com/hippo/ehviewer/spider/SpiderQueen.java b/app/src/main/java/com/hippo/ehviewer/spider/SpiderQueen.java index cdf7f28d8..0148ff5c1 100644 --- a/app/src/main/java/com/hippo/ehviewer/spider/SpiderQueen.java +++ b/app/src/main/java/com/hippo/ehviewer/spider/SpiderQueen.java @@ -1244,7 +1244,6 @@ public final class SpiderQueen implements Runnable { } // Download image - InputStream is = null; try { if (DEBUG_LOG) { Log.d(TAG, "Start download image " + index); @@ -1281,11 +1280,10 @@ public final class SpiderQueen implements Runnable { } long contentLength = responseBody.contentLength(); - is = responseBody.byteStream(); long receivedSize = 0; OutputStream os = osPipe.open(); if (os instanceof FileOutputStream fileOutputStream) { - try (var channel = fileOutputStream.getChannel(); var source = responseBody.source(); response) { + try (var channel = fileOutputStream.getChannel(); var source = responseBody.source(); responseBody; response) { while (!mStoped) { // Is 40k a good size ? long bytesRead = channel.transferFrom(source, receivedSize, 40960); @@ -1301,22 +1299,23 @@ public final class SpiderQueen implements Runnable { } } else { final byte[] data = new byte[1024 * 4]; - while (!mStoped) { - int bytesRead = is.read(data); - if (bytesRead == -1) { - response.close(); - break; + try (var is = responseBody.byteStream(); responseBody; response) { + while (!mStoped) { + int bytesRead = is.read(data); + if (bytesRead == -1) { + break; + } + os.write(data, 0, bytesRead); + receivedSize += bytesRead; + // Update page percent + if (contentLength > 0) { + mPagePercentMap.put(index, (float) receivedSize / contentLength); + } + // Notify listener + notifyPageDownload(index, contentLength, receivedSize, bytesRead); } - os.write(data, 0, bytesRead); - receivedSize += bytesRead; - // Update page percent - if (contentLength > 0) { - mPagePercentMap.put(index, (float) receivedSize / contentLength); - } - // Notify listener - notifyPageDownload(index, contentLength, receivedSize, bytesRead); + os.flush(); } - os.flush(); } // check download size @@ -1401,8 +1400,6 @@ public final class SpiderQueen implements Runnable { error = GetText.getString(R.string.error_socket); forceHtml = true; } finally { - IOUtils.closeQuietly(is); - if (DEBUG_LOG) { Log.d(TAG, "End download image " + index); }