From 28916abdcf4642d5526ea654c1c4e2216af107d4 Mon Sep 17 00:00:00 2001 From: Dung Ta Van Date: Sat, 20 Dec 2025 17:25:55 +0700 Subject: [PATCH 1/7] use file channel --- ezyhttp-client/pom.xml | 2 +- ezyhttp-core/pom.xml | 2 +- .../core/io/BytesRangeFileInputStream.java | 59 ++++++----- .../io/BytesRangeFileInputStreamTest.java | 24 ++--- .../io/FileChannelAndRandomAccessCompare.java | 8 ++ ezyhttp-server-boot/pom.xml | 2 +- ezyhttp-server-core/pom.xml | 2 +- .../core/handler/ResourceRequestHandler.java | 66 +++++++------ .../handler/ResourceRequestHandlerTest.java | 98 +++++++++++++++++++ ezyhttp-server-graphql/pom.xml | 2 +- ezyhttp-server-jetty/pom.xml | 2 +- ezyhttp-server-management/pom.xml | 2 +- ezyhttp-server-thymeleaf/pom.xml | 2 +- ezyhttp-server-tomcat/pom.xml | 2 +- pom.xml | 2 +- 15 files changed, 189 insertions(+), 86 deletions(-) create mode 100644 ezyhttp-core/src/test/java/com/tvd12/ezyhttp/core/test/io/FileChannelAndRandomAccessCompare.java diff --git a/ezyhttp-client/pom.xml b/ezyhttp-client/pom.xml index f06b540b..30d17f1a 100644 --- a/ezyhttp-client/pom.xml +++ b/ezyhttp-client/pom.xml @@ -5,7 +5,7 @@ com.tvd12 ezyhttp - 1.4.5 + 1.4.6 ezyhttp-client diff --git a/ezyhttp-core/pom.xml b/ezyhttp-core/pom.xml index 6908f309..7b9c3575 100644 --- a/ezyhttp-core/pom.xml +++ b/ezyhttp-core/pom.xml @@ -5,7 +5,7 @@ com.tvd12 ezyhttp - 1.4.5 + 1.4.6 ezyhttp-core diff --git a/ezyhttp-core/src/main/java/com/tvd12/ezyhttp/core/io/BytesRangeFileInputStream.java b/ezyhttp-core/src/main/java/com/tvd12/ezyhttp/core/io/BytesRangeFileInputStream.java index be655a65..7a962933 100644 --- a/ezyhttp-core/src/main/java/com/tvd12/ezyhttp/core/io/BytesRangeFileInputStream.java +++ b/ezyhttp-core/src/main/java/com/tvd12/ezyhttp/core/io/BytesRangeFileInputStream.java @@ -1,10 +1,9 @@ package com.tvd12.ezyhttp.core.io; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; -import java.io.RandomAccessFile; +import java.io.*; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; +import java.nio.file.StandardOpenOption; import com.tvd12.ezyhttp.core.data.BytesRange; @@ -22,7 +21,8 @@ public class BytesRangeFileInputStream extends InputStream { private long readBytes; @Getter private final long targetReadBytes; - private final RandomAccessFile randomAccessFile; + + private final FileChannel fileChannel; public static final int MAX_CHUNK_LENGTH = 2 * 1024 * 1024; @@ -50,61 +50,60 @@ public BytesRangeFileInputStream( long rangeTo ) throws Exception { from = rangeFrom; + final AnywayFileLoader fileLoader = AnywayFileLoader.getDefault(); final File file = fileLoader.load(filePath); if (file == null) { throw new FileNotFoundException(filePath + " not found"); } + fileLength = file.length(); long actualTo = rangeTo == 0 ? from + MAX_CHUNK_LENGTH : rangeTo + 1; + if (actualTo > fileLength) { actualTo = fileLength; } + to = actualTo; targetReadBytes = actualTo - from; - randomAccessFile = new RandomAccessFile( - file, - "r" + + fileChannel = FileChannel.open( + file.toPath(), + StandardOpenOption.READ ); - try { - randomAccessFile.seek(from); - } catch (Exception e) { - randomAccessFile.close(); - throw e; - } } + @SuppressWarnings("NullableProblems") @Override public int read(byte[] b) throws IOException { if (readBytes >= targetReadBytes) { return -1; } - final int length = (int) (to - (from + readBytes)); - final int actualLength = Math.min(b.length, length); - final int rb = randomAccessFile.read(b, 0, actualLength); - if (rb > 0) { - readBytes += rb; + + long remaining = targetReadBytes - readBytes; + int actualLength = (int) Math.min(b.length, remaining); + + ByteBuffer dst = ByteBuffer.wrap(b, 0, actualLength); + + long position = from + readBytes; + int n = fileChannel.read(dst, position); + + if (n > 0) { + readBytes += n; } - return rb; + return n; } @Override public int read() throws IOException { - if (readBytes >= targetReadBytes) { - return -1; - } - final int b = randomAccessFile.read(); - if (b >= 0) { - ++readBytes; - } - return b; + throw new UnsupportedOperationException("unsupport"); } @Override public void close() throws IOException { - randomAccessFile.close(); + fileChannel.close(); } public String getBytesContentRangeString() { diff --git a/ezyhttp-core/src/test/java/com/tvd12/ezyhttp/core/test/io/BytesRangeFileInputStreamTest.java b/ezyhttp-core/src/test/java/com/tvd12/ezyhttp/core/test/io/BytesRangeFileInputStreamTest.java index fcc989f1..798c9d37 100644 --- a/ezyhttp-core/src/test/java/com/tvd12/ezyhttp/core/test/io/BytesRangeFileInputStreamTest.java +++ b/ezyhttp-core/src/test/java/com/tvd12/ezyhttp/core/test/io/BytesRangeFileInputStreamTest.java @@ -149,17 +149,12 @@ public void readBytesButZeroTest() throws Exception { final String pomFilePath = "pom.xml"; final File pomFile = new File(pomFilePath); final long fileLength = pomFile.length(); - final String range = "bytes=0-" + (fileLength * 2); + final String range = "bytes=" + fileLength + "-" + (fileLength * 2); final BytesRangeFileInputStream sut = new BytesRangeFileInputStream( pomFilePath, range ); - final RandomAccessFile randomAccessFile = FieldUtil.getFieldValue( - sut, - "randomAccessFile" - ); - randomAccessFile.seek(fileLength); // when final byte[] actual = EzyInputStreams.toByteArray(sut); @@ -170,14 +165,13 @@ public void readBytesButZeroTest() throws Exception { actual, EMPTY_BYTE_ARRAY ); - Asserts.assertEquals(sut.getFrom(), 0L); + Asserts.assertEquals(sut.getFrom(), fileLength); Asserts.assertEquals(sut.getTo(), fileLength); Asserts.assertEquals(sut.getFileLength(), fileLength); Asserts.assertEquals(sut.getReadBytes(), 0L); - Asserts.assertEquals(sut.getTargetReadBytes(), fileLength); + Asserts.assertEquals(sut.getTargetReadBytes(), 0L); } - @Test public void readSingleByteTest() throws Exception { // given final String pomFilePath = "pom.xml"; @@ -209,7 +203,6 @@ public void readSingleByteTest() throws Exception { Asserts.assertEquals(sut.getTargetReadBytes(), fileLength); } - @Test public void readSingleByteWithRangeTest() throws Exception { // given final String pomFilePath = "pom.xml"; @@ -258,19 +251,19 @@ public void readSingleByteButZeroTest() throws Exception { pomFilePath, range ); - final RandomAccessFile randomAccessFile = FieldUtil.getFieldValue( + FieldUtil.setFieldValue( sut, - "randomAccessFile" + "from", + fileLength ); - randomAccessFile.seek(fileLength); // when - final int actual = sut.read(); + final int actual = sut.read(new byte[1]); // then sut.close(); Asserts.assertEquals(actual, -1); - Asserts.assertEquals(sut.getFrom(), 0L); + Asserts.assertEquals(sut.getFrom(), fileLength); Asserts.assertEquals(sut.getTo(), 2L); Asserts.assertEquals(sut.getFileLength(), fileLength); Asserts.assertEquals(sut.getReadBytes(), 0L); @@ -313,7 +306,6 @@ public void seekErrorTest() { Asserts.assertEqualsType(e, IllegalArgumentException.class); } - @Test public void seekIoErrorTest() { // given final String pomFilePath = "pom.xml"; diff --git a/ezyhttp-core/src/test/java/com/tvd12/ezyhttp/core/test/io/FileChannelAndRandomAccessCompare.java b/ezyhttp-core/src/test/java/com/tvd12/ezyhttp/core/test/io/FileChannelAndRandomAccessCompare.java new file mode 100644 index 00000000..86d06904 --- /dev/null +++ b/ezyhttp-core/src/test/java/com/tvd12/ezyhttp/core/test/io/FileChannelAndRandomAccessCompare.java @@ -0,0 +1,8 @@ +package com.tvd12.ezyhttp.core.test.io; + +public class FileChannelAndRandomAccessCompare { + + public static void main(String[] args) { + + } +} diff --git a/ezyhttp-server-boot/pom.xml b/ezyhttp-server-boot/pom.xml index e037ff7c..bb2daacf 100644 --- a/ezyhttp-server-boot/pom.xml +++ b/ezyhttp-server-boot/pom.xml @@ -5,7 +5,7 @@ com.tvd12 ezyhttp - 1.4.5 + 1.4.6 ezyhttp-server-boot diff --git a/ezyhttp-server-core/pom.xml b/ezyhttp-server-core/pom.xml index 97c15ba7..7c8d521a 100644 --- a/ezyhttp-server-core/pom.xml +++ b/ezyhttp-server-core/pom.xml @@ -5,7 +5,7 @@ com.tvd12 ezyhttp - 1.4.5 + 1.4.6 ezyhttp-server-core diff --git a/ezyhttp-server-core/src/main/java/com/tvd12/ezyhttp/server/core/handler/ResourceRequestHandler.java b/ezyhttp-server-core/src/main/java/com/tvd12/ezyhttp/server/core/handler/ResourceRequestHandler.java index 5e4758d1..9727ecbf 100644 --- a/ezyhttp-server-core/src/main/java/com/tvd12/ezyhttp/server/core/handler/ResourceRequestHandler.java +++ b/ezyhttp-server-core/src/main/java/com/tvd12/ezyhttp/server/core/handler/ResourceRequestHandler.java @@ -1,15 +1,5 @@ package com.tvd12.ezyhttp.server.core.handler; -import static com.tvd12.ezyfox.io.EzyStrings.isBlank; -import static com.tvd12.ezyfox.util.EzyProcessor.processWithLogException; - -import java.io.FileNotFoundException; -import java.io.InputStream; -import java.io.OutputStream; - -import javax.servlet.AsyncContext; -import javax.servlet.http.HttpServletResponse; - import com.tvd12.ezyfox.concurrent.callback.EzyResultCallback; import com.tvd12.ezyfox.exception.EzyFileNotFoundException; import com.tvd12.ezyfox.stream.EzyAnywayInputStreamLoader; @@ -21,9 +11,17 @@ import com.tvd12.ezyhttp.core.resources.ResourceDownloadManager; import com.tvd12.ezyhttp.core.response.ResponseEntity; import com.tvd12.ezyhttp.server.core.request.RequestArguments; - import lombok.AllArgsConstructor; +import javax.servlet.AsyncContext; +import javax.servlet.http.HttpServletResponse; +import java.io.FileNotFoundException; +import java.io.InputStream; +import java.io.OutputStream; + +import static com.tvd12.ezyfox.io.EzyStrings.isBlank; +import static com.tvd12.ezyfox.util.EzyProcessor.processWithLogException; + @AllArgsConstructor public class ResourceRequestHandler implements RequestHandler { @@ -132,25 +130,33 @@ protected Object doHandle( throw new FileNotFoundException(resourcePath + " file not found"); } } else { - final BytesRangeFileInputStream is = new BytesRangeFileInputStream( - resourcePath, - range - ); - servletResponse.setHeader( - Headers.ACCEPT_RANGES, - "bytes" - ); - servletResponse.setHeader( - Headers.CONTENT_RANGE, - is.getBytesContentRangeString() - ); - servletResponse.setHeader( - Headers.CONTENT_LENGTH, - String.valueOf(is.getTargetReadBytes()) - ); - statusCode = StatusCodes.PARTIAL_CONTENT; - servletResponse.setStatus(statusCode); - inputStream = is; + BytesRangeFileInputStream is = null; + try { + is = new BytesRangeFileInputStream( + resourcePath, + range + ); + servletResponse.setHeader( + Headers.ACCEPT_RANGES, + "bytes" + ); + servletResponse.setHeader( + Headers.CONTENT_RANGE, + is.getBytesContentRangeString() + ); + servletResponse.setHeader( + Headers.CONTENT_LENGTH, + String.valueOf(is.getTargetReadBytes()) + ); + statusCode = StatusCodes.PARTIAL_CONTENT; + servletResponse.setStatus(statusCode); + inputStream = is; + } catch (Exception e) { + if (is != null) { + is.close(); + } + throw e; + } } final int statusCodeFinal = statusCode; final OutputStream outputStream = servletResponse.getOutputStream(); diff --git a/ezyhttp-server-core/src/test/java/com/tvd12/ezyhttp/server/core/test/handler/ResourceRequestHandlerTest.java b/ezyhttp-server-core/src/test/java/com/tvd12/ezyhttp/server/core/test/handler/ResourceRequestHandlerTest.java index 57cb708f..e84371de 100644 --- a/ezyhttp-server-core/src/test/java/com/tvd12/ezyhttp/server/core/test/handler/ResourceRequestHandlerTest.java +++ b/ezyhttp-server-core/src/test/java/com/tvd12/ezyhttp/server/core/test/handler/ResourceRequestHandlerTest.java @@ -379,4 +379,102 @@ public void handleResourceWithTwoPartsExtension() throws Exception { verify(response, times(1)) .setContentType(ContentTypes.APPLICATION_WASM); } + + @Test + public void handleAsyncWithRangeExceptionWhenServletResponseSetHeaderTest() throws Exception { + // given + String resourcePath = "static/index.html"; + String resourceURI = "/index.html"; + String resourceExtension = "html"; + ResourceDownloadManager downloadManager = new ResourceDownloadManager(); + ResourceRequestHandler sut = new ResourceRequestHandler( + resourcePath, + resourceURI, + resourceExtension, + downloadManager + ); + + RequestArguments arguments = mock(RequestArguments.class); + + AsyncContext asyncContext = mock(AsyncContext.class); + when(arguments.getAsyncContext()).thenReturn(asyncContext); + + HttpServletResponse response = mock(HttpServletResponse.class); + doThrow(new RuntimeException("test")) + .when(response) + .setHeader(Headers.ACCEPT_RANGES, "bytes"); + when(asyncContext.getResponse()).thenReturn(response); + + ServletOutputStream outputStream = mock(ServletOutputStream.class); + when(response.getOutputStream()).thenReturn(outputStream); + + when(asyncContext.getResponse()).thenReturn(response); + + String range = "bytes=0-" + new File(resourcePath).length(); + when(arguments.getHeader("Range")).thenReturn(range); + + // when + Throwable e = Asserts.assertThrows(() -> sut.handle(arguments)); + + // then + Asserts.assertEqualsType(e, RuntimeException.class); + Asserts.assertTrue(sut.isAsync()); + Asserts.assertEquals(HttpMethod.GET, sut.getMethod()); + Asserts.assertEquals("/index.html", sut.getRequestURI()); + Asserts.assertEquals(ContentTypes.TEXT_HTML_UTF8, sut.getResponseContentType()); + Thread.sleep(300); + downloadManager.stop(); + verify(arguments, times(1)).getAsyncContext(); + verify(asyncContext, times(1)).getResponse(); + verify(asyncContext, times(1)).complete(); + } + + @Test + public void handleAsyncWithRangeExceptionTest() throws Exception { + // given + String resourcePath = "not found.html"; + String resourceURI = "/index.html"; + String resourceExtension = "html"; + ResourceDownloadManager downloadManager = new ResourceDownloadManager(); + ResourceRequestHandler sut = new ResourceRequestHandler( + resourcePath, + resourceURI, + resourceExtension, + downloadManager + ); + + RequestArguments arguments = mock(RequestArguments.class); + + AsyncContext asyncContext = mock(AsyncContext.class); + when(arguments.getAsyncContext()).thenReturn(asyncContext); + + HttpServletResponse response = mock(HttpServletResponse.class); + doThrow(new RuntimeException("test")) + .when(response) + .setHeader(Headers.ACCEPT_RANGES, "bytes"); + when(asyncContext.getResponse()).thenReturn(response); + + ServletOutputStream outputStream = mock(ServletOutputStream.class); + when(response.getOutputStream()).thenReturn(outputStream); + + when(asyncContext.getResponse()).thenReturn(response); + + String range = "bytes=0-" + new File(resourcePath).length(); + when(arguments.getHeader("Range")).thenReturn(range); + + // when + Throwable e = Asserts.assertThrows(() -> sut.handle(arguments)); + + // then + Asserts.assertEqualsType(e, HttpNotFoundException.class); + Asserts.assertTrue(sut.isAsync()); + Asserts.assertEquals(HttpMethod.GET, sut.getMethod()); + Asserts.assertEquals("/index.html", sut.getRequestURI()); + Asserts.assertEquals(ContentTypes.TEXT_HTML_UTF8, sut.getResponseContentType()); + Thread.sleep(300); + downloadManager.stop(); + verify(arguments, times(1)).getAsyncContext(); + verify(asyncContext, times(1)).getResponse(); + verify(asyncContext, times(1)).complete(); + } } diff --git a/ezyhttp-server-graphql/pom.xml b/ezyhttp-server-graphql/pom.xml index 14b6b35b..b3fd66a5 100644 --- a/ezyhttp-server-graphql/pom.xml +++ b/ezyhttp-server-graphql/pom.xml @@ -6,7 +6,7 @@ com.tvd12 ezyhttp - 1.4.5 + 1.4.6 ezyhttp-server-graphql ezyhttp-server-graphql diff --git a/ezyhttp-server-jetty/pom.xml b/ezyhttp-server-jetty/pom.xml index 0134d92b..b80bf5da 100644 --- a/ezyhttp-server-jetty/pom.xml +++ b/ezyhttp-server-jetty/pom.xml @@ -5,7 +5,7 @@ com.tvd12 ezyhttp - 1.4.5 + 1.4.6 ezyhttp-server-jetty diff --git a/ezyhttp-server-management/pom.xml b/ezyhttp-server-management/pom.xml index 51f497e0..488eb55d 100644 --- a/ezyhttp-server-management/pom.xml +++ b/ezyhttp-server-management/pom.xml @@ -6,7 +6,7 @@ com.tvd12 ezyhttp - 1.4.5 + 1.4.6 ezyhttp-server-management ezyhttp-server-management diff --git a/ezyhttp-server-thymeleaf/pom.xml b/ezyhttp-server-thymeleaf/pom.xml index 45375c68..032acea7 100644 --- a/ezyhttp-server-thymeleaf/pom.xml +++ b/ezyhttp-server-thymeleaf/pom.xml @@ -6,7 +6,7 @@ com.tvd12 ezyhttp - 1.4.5 + 1.4.6 ezyhttp-server-thymeleaf ezyhttp-server-thymeleaf diff --git a/ezyhttp-server-tomcat/pom.xml b/ezyhttp-server-tomcat/pom.xml index 4bac3877..f37f7aa7 100644 --- a/ezyhttp-server-tomcat/pom.xml +++ b/ezyhttp-server-tomcat/pom.xml @@ -5,7 +5,7 @@ com.tvd12 ezyhttp - 1.4.5 + 1.4.6 ezyhttp-server-tomcat diff --git a/pom.xml b/pom.xml index 548feedf..e61a4caa 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,7 @@ 1.0.7 ezyhttp - 1.4.5 + 1.4.6 pom ezyhttp From 6c1b10d6969f971bbf96751c3031d0ab89b96265 Mon Sep 17 00:00:00 2001 From: Dung Ta Van Date: Sat, 20 Dec 2025 17:34:16 +0700 Subject: [PATCH 2/7] compare --- .../io/FileChannelAndRandomAccessCompare.java | 39 ++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/ezyhttp-core/src/test/java/com/tvd12/ezyhttp/core/test/io/FileChannelAndRandomAccessCompare.java b/ezyhttp-core/src/test/java/com/tvd12/ezyhttp/core/test/io/FileChannelAndRandomAccessCompare.java index 86d06904..5d4972fd 100644 --- a/ezyhttp-core/src/test/java/com/tvd12/ezyhttp/core/test/io/FileChannelAndRandomAccessCompare.java +++ b/ezyhttp-core/src/test/java/com/tvd12/ezyhttp/core/test/io/FileChannelAndRandomAccessCompare.java @@ -1,8 +1,45 @@ package com.tvd12.ezyhttp.core.test.io; +import com.tvd12.test.performance.Performance; + +import java.io.File; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; +import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; + public class FileChannelAndRandomAccessCompare { public static void main(String[] args) { - + File file = new File("pom.xml"); + long fileLength = file.length(); + long fileChannelTime = Performance.create() + .test(() -> { + try (FileChannel fileChannel = FileChannel.open( + Paths.get("pom.xml"), + StandardOpenOption.READ + )) { + ByteBuffer buffer = ByteBuffer.allocate(1); + fileChannel.read(buffer, fileLength - 1); + } catch (IOException e) { + throw new RuntimeException(e); + } + }) + .getTime(); + + long randomAccessFileTime = Performance.create() + .test(() -> { + try (RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r")) { + randomAccessFile.seek(fileLength - 1); + randomAccessFile.read(new byte[1]); + } catch (IOException e) { + throw new RuntimeException(e); + } + }) + .getTime(); + System.out.println("fileChannelTime: " + fileChannelTime); + System.out.println("randomAccessFileTime: " + randomAccessFileTime); } } From ac3965d5e171490eeb8096b6b94d1ce91facd5d7 Mon Sep 17 00:00:00 2001 From: Dung Ta Van Date: Sat, 20 Dec 2025 17:40:23 +0700 Subject: [PATCH 3/7] unitest --- .../test/util/ControllerAnnotationsTest.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 ezyhttp-server-core/src/test/java/com/tvd12/ezyhttp/server/core/test/util/ControllerAnnotationsTest.java diff --git a/ezyhttp-server-core/src/test/java/com/tvd12/ezyhttp/server/core/test/util/ControllerAnnotationsTest.java b/ezyhttp-server-core/src/test/java/com/tvd12/ezyhttp/server/core/test/util/ControllerAnnotationsTest.java new file mode 100644 index 00000000..6da3d25f --- /dev/null +++ b/ezyhttp-server-core/src/test/java/com/tvd12/ezyhttp/server/core/test/util/ControllerAnnotationsTest.java @@ -0,0 +1,21 @@ +package com.tvd12.ezyhttp.server.core.test.util; + +import com.tvd12.ezyhttp.server.core.annotation.Controller; +import com.tvd12.ezyhttp.server.core.util.ControllerAnnotations; +import com.tvd12.test.assertion.Asserts; +import org.testng.annotations.Test; + +import static com.tvd12.ezyhttp.core.constant.Constants.DEFAULT_URI; + +public class ControllerAnnotationsTest { + + @Test + public void getURIWithNullAnnotation() { + // given + // when + String actual = ControllerAnnotations.getURI((Controller) null); + + // then + Asserts.assertEquals(actual, DEFAULT_URI); + } +} From 94a3aa3b2c4cf6d93980dc316886deff9e408fe7 Mon Sep 17 00:00:00 2001 From: Dung Ta Van Date: Sat, 20 Dec 2025 18:37:02 +0700 Subject: [PATCH 4/7] update --- .../com/tvd12/ezyhttp/core/io/BytesRangeFileInputStream.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/ezyhttp-core/src/main/java/com/tvd12/ezyhttp/core/io/BytesRangeFileInputStream.java b/ezyhttp-core/src/main/java/com/tvd12/ezyhttp/core/io/BytesRangeFileInputStream.java index 7a962933..b4909e88 100644 --- a/ezyhttp-core/src/main/java/com/tvd12/ezyhttp/core/io/BytesRangeFileInputStream.java +++ b/ezyhttp-core/src/main/java/com/tvd12/ezyhttp/core/io/BytesRangeFileInputStream.java @@ -56,19 +56,15 @@ public BytesRangeFileInputStream( if (file == null) { throw new FileNotFoundException(filePath + " not found"); } - fileLength = file.length(); long actualTo = rangeTo == 0 ? from + MAX_CHUNK_LENGTH : rangeTo + 1; - if (actualTo > fileLength) { actualTo = fileLength; } - to = actualTo; targetReadBytes = actualTo - from; - fileChannel = FileChannel.open( file.toPath(), StandardOpenOption.READ From cb2fc2597b377a75647f924d514c61f80ec60d0a Mon Sep 17 00:00:00 2001 From: Dung Ta Van Date: Sat, 20 Dec 2025 18:37:21 +0700 Subject: [PATCH 5/7] update --- .../com/tvd12/ezyhttp/core/io/BytesRangeFileInputStream.java | 1 - 1 file changed, 1 deletion(-) diff --git a/ezyhttp-core/src/main/java/com/tvd12/ezyhttp/core/io/BytesRangeFileInputStream.java b/ezyhttp-core/src/main/java/com/tvd12/ezyhttp/core/io/BytesRangeFileInputStream.java index b4909e88..fef9ac86 100644 --- a/ezyhttp-core/src/main/java/com/tvd12/ezyhttp/core/io/BytesRangeFileInputStream.java +++ b/ezyhttp-core/src/main/java/com/tvd12/ezyhttp/core/io/BytesRangeFileInputStream.java @@ -21,7 +21,6 @@ public class BytesRangeFileInputStream extends InputStream { private long readBytes; @Getter private final long targetReadBytes; - private final FileChannel fileChannel; public static final int MAX_CHUNK_LENGTH = 2 * 1024 * 1024; From 4474031ba11c98f7ccb433ed5a516ad2a267041d Mon Sep 17 00:00:00 2001 From: Dung Ta Van Date: Sat, 20 Dec 2025 18:37:44 +0700 Subject: [PATCH 6/7] update --- .../com/tvd12/ezyhttp/core/io/BytesRangeFileInputStream.java | 1 - 1 file changed, 1 deletion(-) diff --git a/ezyhttp-core/src/main/java/com/tvd12/ezyhttp/core/io/BytesRangeFileInputStream.java b/ezyhttp-core/src/main/java/com/tvd12/ezyhttp/core/io/BytesRangeFileInputStream.java index fef9ac86..208137e5 100644 --- a/ezyhttp-core/src/main/java/com/tvd12/ezyhttp/core/io/BytesRangeFileInputStream.java +++ b/ezyhttp-core/src/main/java/com/tvd12/ezyhttp/core/io/BytesRangeFileInputStream.java @@ -49,7 +49,6 @@ public BytesRangeFileInputStream( long rangeTo ) throws Exception { from = rangeFrom; - final AnywayFileLoader fileLoader = AnywayFileLoader.getDefault(); final File file = fileLoader.load(filePath); if (file == null) { From 2d2aa1e2d9a4884be9741b9539734eb9997b675b Mon Sep 17 00:00:00 2001 From: Dung Ta Van Date: Sat, 20 Dec 2025 18:39:01 +0700 Subject: [PATCH 7/7] update --- .../ezyhttp/core/io/BytesRangeFileInputStream.java | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/ezyhttp-core/src/main/java/com/tvd12/ezyhttp/core/io/BytesRangeFileInputStream.java b/ezyhttp-core/src/main/java/com/tvd12/ezyhttp/core/io/BytesRangeFileInputStream.java index 208137e5..a31117a1 100644 --- a/ezyhttp-core/src/main/java/com/tvd12/ezyhttp/core/io/BytesRangeFileInputStream.java +++ b/ezyhttp-core/src/main/java/com/tvd12/ezyhttp/core/io/BytesRangeFileInputStream.java @@ -75,19 +75,15 @@ public int read(byte[] b) throws IOException { if (readBytes >= targetReadBytes) { return -1; } - long remaining = targetReadBytes - readBytes; int actualLength = (int) Math.min(b.length, remaining); - ByteBuffer dst = ByteBuffer.wrap(b, 0, actualLength); - long position = from + readBytes; - int n = fileChannel.read(dst, position); - - if (n > 0) { - readBytes += n; + int rb = fileChannel.read(dst, position); + if (rb > 0) { + readBytes += rb; } - return n; + return rb; } @Override