This commit is contained in:
Looly 2022-10-27 18:39:08 +08:00
parent e1a2eaafa6
commit 415a091b5e
4 changed files with 40 additions and 5 deletions

View File

@ -74,7 +74,9 @@ public class NioUtil {
public static long copyByNIO(final InputStream in, final OutputStream out, final int bufferSize, final long count, final StreamProgress streamProgress) throws IORuntimeException { public static long copyByNIO(final InputStream in, final OutputStream out, final int bufferSize, final long count, final StreamProgress streamProgress) throws IORuntimeException {
Assert.notNull(in, "InputStream channel is null!"); Assert.notNull(in, "InputStream channel is null!");
Assert.notNull(out, "OutputStream channel is null!"); Assert.notNull(out, "OutputStream channel is null!");
return copy(Channels.newChannel(in), Channels.newChannel(out), bufferSize, count, streamProgress); final long copySize = copy(Channels.newChannel(in), Channels.newChannel(out), bufferSize, count, streamProgress);
IoUtil.flush(out);
return copySize;
} }
/** /**

View File

@ -103,7 +103,8 @@ public class ChannelCopier extends IoCopier<ReadableByteChannel, WritableByteCha
numToRead -= read; numToRead -= read;
total += read; total += read;
if (null != progress) { if (null != progress) {
progress.progress(this.count, total); // 总长度未知的情况下-1表示未知
progress.progress(this.count < Long.MAX_VALUE ? this.count : -1, total);
} }
} }

View File

@ -2,20 +2,55 @@ package cn.hutool.core.io;
import cn.hutool.core.io.resource.ResourceUtil; import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.io.stream.EmptyOutputStream; import cn.hutool.core.io.stream.EmptyOutputStream;
import cn.hutool.core.lang.Console;
import cn.hutool.core.text.StrUtil; import cn.hutool.core.text.StrUtil;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
public class NioUtilTest { public class NioUtilTest {
@Test @Test
public void copyByNIOTest() { public void copyByNIOTest() {
final File file = FileUtil.file("hutool.jpg");
final long size = NioUtil.copyByNIO(ResourceUtil.getStream("hutool.jpg"), EmptyOutputStream.INSTANCE, NioUtil.DEFAULT_MIDDLE_BUFFER_SIZE, null); final long size = NioUtil.copyByNIO(ResourceUtil.getStream("hutool.jpg"), EmptyOutputStream.INSTANCE, NioUtil.DEFAULT_MIDDLE_BUFFER_SIZE, null);
// 确认写出
Assert.assertEquals(file.length(), size);
Assert.assertEquals(22807, size); Assert.assertEquals(22807, size);
} }
@Test
@Ignore
public void copyByNIOTest2() throws IOException {
final File file = FileUtil.file("d:/test/logo.jpg");
final BufferedInputStream in = FileUtil.getInputStream(file);
final BufferedOutputStream out = FileUtil.getOutputStream("d:/test/2logo.jpg");
final long copySize = IoUtil.copyByNIO(in, out, NioUtil.DEFAULT_BUFFER_SIZE, new StreamProgress() {
@Override
public void start() {
Console.log("start");
}
@Override
public void progress(final long total, final long progressSize) {
Console.log("{} {}", total, progressSize);
}
@Override
public void finish() {
Console.log("finish");
}
});
Assert.assertEquals(file.length(), copySize);
}
@Test @Test
public void readUtf8Test() throws IOException { public void readUtf8Test() throws IOException {
final String s = NioUtil.readUtf8(FileChannel.open(FileUtil.file("text.txt").toPath())); final String s = NioUtil.readUtf8(FileChannel.open(FileUtil.file("text.txt").toPath()));

View File

@ -1,3 +0,0 @@
cn.hutool.http.client.engine.httpclient5.HttpClient5Engine
cn.hutool.http.client.engine.httpclient4.HttpClient4Engine
cn.hutool.http.client.engine.okhttp.OkHttpEngine