diff --git a/okdownload/src/main/java/com/liulishuo/okdownload/core/breakpoint/BreakpointInfo.java b/okdownload/src/main/java/com/liulishuo/okdownload/core/breakpoint/BreakpointInfo.java index dd52c625..90f0615c 100644 --- a/okdownload/src/main/java/com/liulishuo/okdownload/core/breakpoint/BreakpointInfo.java +++ b/okdownload/src/main/java/com/liulishuo/okdownload/core/breakpoint/BreakpointInfo.java @@ -129,21 +129,14 @@ public long getTotalOffset() { final int count = list.size(); for (int i = 0; i < count; i++) { final BlockInfo info = list.get(i); - long cOffset = info.getCurrentOffset(); - if (i == 0) { - if (cOffset > info.getContentLength()) { - offset = info.getContentLength(); - } else { - offset = cOffset; - } - } else { - offset += cOffset; - } + offset += info.getCurrentOffset(); } return offset; } public long getTotalLength() { + if (isChunked()) return getTotalOffset(); + long length = 0; ArrayList list = (ArrayList) ((ArrayList) blockInfoList).clone(); for (BlockInfo info : list) { diff --git a/okdownload/src/test/java/com/liulishuo/okdownload/core/breakpoint/BreakpointInfoTest.java b/okdownload/src/test/java/com/liulishuo/okdownload/core/breakpoint/BreakpointInfoTest.java index 5023b5dd..6fe4889c 100644 --- a/okdownload/src/test/java/com/liulishuo/okdownload/core/breakpoint/BreakpointInfoTest.java +++ b/okdownload/src/test/java/com/liulishuo/okdownload/core/breakpoint/BreakpointInfoTest.java @@ -23,7 +23,9 @@ import java.io.File; import static org.assertj.core.api.Java6Assertions.assertThat; +import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; public class BreakpointInfoTest { @@ -54,12 +56,20 @@ public void copyNotClone() { @Test public void getTotalOffset() { BreakpointInfo info = new BreakpointInfo(0, "", new File(""), null); - info.addBlock(new BlockInfo(0, 10, 12)); + info.addBlock(new BlockInfo(0, 10, 10)); info.addBlock(new BlockInfo(10, 18, 18)); info.addBlock(new BlockInfo(28, 66, 66)); assertThat(info.getTotalOffset()).isEqualTo(94); } + @Test + public void getTotalLength_chunked() { + BreakpointInfo info = spy(new BreakpointInfo(0, "", new File(""), null)); + when(info.isChunked()).thenReturn(true); + doReturn(1L).when(info).getTotalOffset(); + assertThat(info.getTotalLength()).isEqualTo(1L); + } + @Test public void getTotalLength() { BreakpointInfo info = new BreakpointInfo(0, "", new File(""), null);