Search in sources :

Example 1 with StreamProgress

use of cn.hutool.core.io.StreamProgress in project hutool by looly.

the class ReaderWriterCopier method copy.

@Override
public long copy(Reader source, Writer target) {
    Assert.notNull(source, "InputStream is null !");
    Assert.notNull(target, "OutputStream is null !");
    final StreamProgress progress = this.progress;
    if (null != progress) {
        progress.start();
    }
    final long size;
    try {
        size = doCopy(source, target, new char[bufferSize(this.count)], progress);
        target.flush();
    } catch (IOException e) {
        throw new IORuntimeException(e);
    }
    if (null != progress) {
        progress.finish();
    }
    return size;
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) IOException(java.io.IOException) StreamProgress(cn.hutool.core.io.StreamProgress)

Example 2 with StreamProgress

use of cn.hutool.core.io.StreamProgress in project hutool by looly.

the class DownloadTest method downloadFileFromUrlTest2.

@Test
@Ignore
public void downloadFileFromUrlTest2() {
    File file = null;
    try {
        file = HttpUtil.downloadFileFromUrl("https://repo1.maven.org/maven2/cn/hutool/hutool-all/5.4.0/hutool-all-5.4.0-sources.jar", FileUtil.file("d:/download/temp"), 1, new StreamProgress() {

            @Override
            public void start() {
                System.out.println("start");
            }

            @Override
            public void progress(long progressSize) {
                System.out.println("download size:" + progressSize);
            }

            @Override
            public void finish() {
                System.out.println("end");
            }
        });
        Assert.assertNotNull(file);
        Assert.assertTrue(file.exists());
        Assert.assertTrue(file.isFile());
        Assert.assertTrue(file.length() > 0);
        Assert.assertTrue(file.getName().length() > 0);
    } catch (Exception e) {
        Assert.assertTrue(e instanceof IORuntimeException);
    } finally {
        FileUtil.del(file);
    }
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) File(java.io.File) StreamProgress(cn.hutool.core.io.StreamProgress) IORuntimeException(cn.hutool.core.io.IORuntimeException) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with StreamProgress

use of cn.hutool.core.io.StreamProgress in project hutool by looly.

the class DownloadTest method downloadFileFromUrlTest3.

@Test
@Ignore
public void downloadFileFromUrlTest3() {
    File file = null;
    try {
        file = HttpUtil.downloadFileFromUrl("https://repo1.maven.org/maven2/cn/hutool/hutool-all/5.4.0/hutool-all-5.4.0-sources.jar", FileUtil.file("d:/download/temp"), new StreamProgress() {

            @Override
            public void start() {
                System.out.println("start");
            }

            @Override
            public void progress(long progressSize) {
                System.out.println("download size:" + progressSize);
            }

            @Override
            public void finish() {
                System.out.println("end");
            }
        });
        Assert.assertNotNull(file);
        Assert.assertTrue(file.exists());
        Assert.assertTrue(file.isFile());
        Assert.assertTrue(file.length() > 0);
        Assert.assertTrue(file.getName().length() > 0);
    } finally {
        FileUtil.del(file);
    }
}
Also used : File(java.io.File) StreamProgress(cn.hutool.core.io.StreamProgress) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 4 with StreamProgress

use of cn.hutool.core.io.StreamProgress in project hutool by looly.

the class ChannelCopier method copy.

@Override
public long copy(ReadableByteChannel source, WritableByteChannel target) {
    Assert.notNull(source, "InputStream is null !");
    Assert.notNull(target, "OutputStream is null !");
    final StreamProgress progress = this.progress;
    if (null != progress) {
        progress.start();
    }
    final long size;
    try {
        size = doCopy(source, target, ByteBuffer.allocate(bufferSize(this.count)), progress);
    } catch (IOException e) {
        throw new IORuntimeException(e);
    }
    if (null != progress) {
        progress.finish();
    }
    return size;
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) IOException(java.io.IOException) StreamProgress(cn.hutool.core.io.StreamProgress)

Example 5 with StreamProgress

use of cn.hutool.core.io.StreamProgress in project hutool by looly.

the class StreamCopier method copy.

@Override
public long copy(InputStream source, OutputStream target) {
    Assert.notNull(source, "InputStream is null !");
    Assert.notNull(target, "OutputStream is null !");
    final StreamProgress progress = this.progress;
    if (null != progress) {
        progress.start();
    }
    final long size;
    try {
        size = doCopy(source, target, new byte[bufferSize(this.count)], progress);
        target.flush();
    } catch (IOException e) {
        throw new IORuntimeException(e);
    }
    if (null != progress) {
        progress.finish();
    }
    return size;
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) IOException(java.io.IOException) StreamProgress(cn.hutool.core.io.StreamProgress)

Aggregations

StreamProgress (cn.hutool.core.io.StreamProgress)5 IORuntimeException (cn.hutool.core.io.IORuntimeException)4 IOException (java.io.IOException)3 File (java.io.File)2 Ignore (org.junit.Ignore)2 Test (org.junit.Test)2