mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix bug
This commit is contained in:
parent
e1a2eaafa6
commit
415a091b5e
@ -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 {
|
||||
Assert.notNull(in, "InputStream 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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -103,7 +103,8 @@ public class ChannelCopier extends IoCopier<ReadableByteChannel, WritableByteCha
|
||||
numToRead -= read;
|
||||
total += read;
|
||||
if (null != progress) {
|
||||
progress.progress(this.count, total);
|
||||
// 总长度未知的情况下,-1表示未知
|
||||
progress.progress(this.count < Long.MAX_VALUE ? this.count : -1, total);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2,20 +2,55 @@ package cn.hutool.core.io;
|
||||
|
||||
import cn.hutool.core.io.resource.ResourceUtil;
|
||||
import cn.hutool.core.io.stream.EmptyOutputStream;
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.channels.FileChannel;
|
||||
|
||||
public class NioUtilTest {
|
||||
@Test
|
||||
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);
|
||||
|
||||
// 确认写出
|
||||
Assert.assertEquals(file.length(), 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
|
||||
public void readUtf8Test() throws IOException {
|
||||
final String s = NioUtil.readUtf8(FileChannel.open(FileUtil.file("text.txt").toPath()));
|
||||
|
@ -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
|
Loading…
x
Reference in New Issue
Block a user