添加Stream进度条中总大小回调

This commit is contained in:
xufeng 2022-04-09 13:22:57 +08:00
parent 2b79119c00
commit 12ce19ff12
5 changed files with 27 additions and 27 deletions

View File

@ -17,7 +17,7 @@ public interface StreamProgress {
* *
* @param progressSize 已经进行的大小 * @param progressSize 已经进行的大小
*/ */
void progress(long progressSize); void progress(long contentLength, long progressSize);
/** /**
* 结束 * 结束

View File

@ -107,7 +107,7 @@ public class ChannelCopier extends IoCopier<ReadableByteChannel, WritableByteCha
numToRead -= read; numToRead -= read;
total += read; total += read;
if (null != progress) { if (null != progress) {
progress.progress(total); progress.progress(this.count, total);
} }
} }

View File

@ -108,7 +108,7 @@ public class ReaderWriterCopier extends IoCopier<Reader, Writer> {
numToRead -= read; numToRead -= read;
total += read; total += read;
if (null != progress) { if (null != progress) {
progress.progress(total); progress.progress(this.count, total);
} }
} }

View File

@ -107,7 +107,7 @@ public class StreamCopier extends IoCopier<InputStream, OutputStream> {
numToRead -= read; numToRead -= read;
total += read; total += read;
if (null != progress) { if (null != progress) {
progress.progress(total); progress.progress(this.count, total);
} }
} }

View File

@ -13,7 +13,7 @@ import java.util.UUID;
/** /**
* 下载单元测试 * 下载单元测试
* *
* @author looly * @author looly
*/ */
public class DownloadTest { public class DownloadTest {
@ -54,18 +54,18 @@ public class DownloadTest {
} }
@Override @Override
public void progress(long progressSize) { public void progress(long contentLength, long progressSize) {
long speed = progressSize / (System.currentTimeMillis() - time) * 1000; long speed = progressSize / (System.currentTimeMillis() - time) * 1000;
Console.log("已下载:{}, 速度:{}/s", FileUtil.readableFileSize(progressSize), FileUtil.readableFileSize(speed)); Console.log("总大小:{}, 已下载:{}, 速度:{}/s", FileUtil.readableFileSize(contentLength), FileUtil.readableFileSize(progressSize), FileUtil.readableFileSize(speed));
} }
@Override @Override
public void finish() { public void finish() {
Console.log("下载完成!"); Console.log("下载完成!");
} }
}); });
} }
@Test @Test
@Ignore @Ignore
public void downloadFileFromUrlTest1() { public void downloadFileFromUrlTest1() {
@ -74,7 +74,7 @@ public class DownloadTest {
Assert.assertTrue(file.isFile()); Assert.assertTrue(file.isFile());
Assert.assertTrue(file.length() > 0); Assert.assertTrue(file.length() > 0);
} }
@Test @Test
@Ignore @Ignore
public void downloadFileFromUrlTest2() { public void downloadFileFromUrlTest2() {
@ -85,18 +85,18 @@ public class DownloadTest {
public void start() { public void start() {
System.out.println("start"); System.out.println("start");
} }
@Override @Override
public void progress(long progressSize) { public void progress(long contentLength, long progressSize) {
System.out.println("download size:" + progressSize); System.out.println("download size:" + progressSize);
} }
@Override @Override
public void finish() { public void finish() {
System.out.println("end"); System.out.println("end");
} }
}); });
Assert.assertNotNull(file); Assert.assertNotNull(file);
Assert.assertTrue(file.exists()); Assert.assertTrue(file.exists());
Assert.assertTrue(file.isFile()); Assert.assertTrue(file.isFile());
@ -108,7 +108,7 @@ public class DownloadTest {
FileUtil.del(file); FileUtil.del(file);
} }
} }
@Test @Test
@Ignore @Ignore
public void downloadFileFromUrlTest3() { public void downloadFileFromUrlTest3() {
@ -119,18 +119,18 @@ public class DownloadTest {
public void start() { public void start() {
System.out.println("start"); System.out.println("start");
} }
@Override @Override
public void progress(long progressSize) { public void progress(long contentLength, long progressSize) {
System.out.println("download size:" + progressSize); System.out.println("contentLength:" + contentLength + "download size:" + progressSize);
} }
@Override @Override
public void finish() { public void finish() {
System.out.println("end"); System.out.println("end");
} }
}); });
Assert.assertNotNull(file); Assert.assertNotNull(file);
Assert.assertTrue(file.exists()); Assert.assertTrue(file.exists());
Assert.assertTrue(file.isFile()); Assert.assertTrue(file.isFile());
@ -140,14 +140,14 @@ public class DownloadTest {
FileUtil.del(file); FileUtil.del(file);
} }
} }
@Test @Test
@Ignore @Ignore
public void downloadFileFromUrlTest4() { public void downloadFileFromUrlTest4() {
File file = null; File file = null;
try { try {
file = HttpUtil.downloadFileFromUrl("http://groovy-lang.org/changelogs/changelog-3.0.5.html", FileUtil.file("d:/download/temp"), 1); file = HttpUtil.downloadFileFromUrl("http://groovy-lang.org/changelogs/changelog-3.0.5.html", FileUtil.file("d:/download/temp"), 1);
Assert.assertNotNull(file); Assert.assertNotNull(file);
Assert.assertTrue(file.exists()); Assert.assertTrue(file.exists());
Assert.assertTrue(file.isFile()); Assert.assertTrue(file.isFile());
@ -159,15 +159,15 @@ public class DownloadTest {
FileUtil.del(file); FileUtil.del(file);
} }
} }
@Test @Test
@Ignore @Ignore
public void downloadFileFromUrlTest5() { public void downloadFileFromUrlTest5() {
File file = null; File file = null;
try { try {
file = HttpUtil.downloadFileFromUrl("http://groovy-lang.org/changelogs/changelog-3.0.5.html", FileUtil.file("d:/download/temp", UUID.randomUUID().toString())); file = HttpUtil.downloadFileFromUrl("http://groovy-lang.org/changelogs/changelog-3.0.5.html", FileUtil.file("d:/download/temp", UUID.randomUUID().toString()));
Assert.assertNotNull(file); Assert.assertNotNull(file);
Assert.assertTrue(file.exists()); Assert.assertTrue(file.exists());
Assert.assertTrue(file.isFile()); Assert.assertTrue(file.isFile());
@ -175,11 +175,11 @@ public class DownloadTest {
} finally { } finally {
FileUtil.del(file); FileUtil.del(file);
} }
File file1 = null; File file1 = null;
try { try {
file1 = HttpUtil.downloadFileFromUrl("http://groovy-lang.org/changelogs/changelog-3.0.5.html", FileUtil.file("d:/download/temp")); file1 = HttpUtil.downloadFileFromUrl("http://groovy-lang.org/changelogs/changelog-3.0.5.html", FileUtil.file("d:/download/temp"));
Assert.assertNotNull(file1); Assert.assertNotNull(file1);
Assert.assertTrue(file1.exists()); Assert.assertTrue(file1.exists());
Assert.assertTrue(file1.isFile()); Assert.assertTrue(file1.isFile());