Search in sources :

Example 71 with IORuntimeException

use of cn.hutool.core.io.IORuntimeException 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)

Example 72 with IORuntimeException

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

the class SymmetricCrypto method encrypt.

@Override
public void encrypt(InputStream data, OutputStream out, boolean isClose) throws IORuntimeException {
    lock.lock();
    CipherOutputStream cipherOutputStream = null;
    try {
        final Cipher cipher = initMode(Cipher.ENCRYPT_MODE);
        cipherOutputStream = new CipherOutputStream(out, cipher);
        long length = IoUtil.copy(data, cipherOutputStream);
        if (this.isZeroPadding) {
            final int blockSize = cipher.getBlockSize();
            if (blockSize > 0) {
                // 按照块拆分后的数据中多余的数据
                final int remainLength = (int) (length % blockSize);
                if (remainLength > 0) {
                    // 补充0
                    cipherOutputStream.write(new byte[blockSize - remainLength]);
                    cipherOutputStream.flush();
                }
            }
        }
    } catch (IORuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new CryptoException(e);
    } finally {
        lock.unlock();
        // issue#I4EMST@Gitee
        // CipherOutputStream必须关闭,才能完全写出
        IoUtil.close(cipherOutputStream);
        if (isClose) {
            IoUtil.close(data);
        }
    }
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) IORuntimeException(cn.hutool.core.io.IORuntimeException) Cipher(javax.crypto.Cipher) CryptoException(cn.hutool.crypto.CryptoException) IORuntimeException(cn.hutool.core.io.IORuntimeException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) IOException(java.io.IOException) CryptoException(cn.hutool.crypto.CryptoException) InvalidKeyException(java.security.InvalidKeyException)

Example 73 with IORuntimeException

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

the class Deflate method inflater.

/**
 * 将压缩流解压到target中
 *
 * @return this
 */
public Deflate inflater() {
    target = (target instanceof InflaterOutputStream) ? (InflaterOutputStream) target : new InflaterOutputStream(target, new Inflater(nowrap));
    IoUtil.copy(source, target);
    try {
        ((InflaterOutputStream) target).finish();
    } catch (IOException e) {
        throw new IORuntimeException(e);
    }
    return this;
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) InflaterOutputStream(java.util.zip.InflaterOutputStream) Inflater(java.util.zip.Inflater) IOException(java.io.IOException)

Example 74 with IORuntimeException

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

the class ZipWriter method add.

/**
 * 对文件或文件目录进行压缩
 *
 * @param withSrcDir 是否包含被打包目录,只针对压缩目录有效。若为false,则只压缩目录下的文件或目录,为true则将本目录也压缩
 * @param filter     文件过滤器,通过实现此接口,自定义要过滤的文件(过滤掉哪些文件或文件夹不加入压缩),{@code null}表示不过滤
 * @param files      要压缩的源文件或目录。如果压缩一个文件,则为该文件的全路径;如果压缩一个目录,则为该目录的顶层目录路径
 * @return this
 * @throws IORuntimeException IO异常
 * @since 5.1.1
 */
public ZipWriter add(boolean withSrcDir, FileFilter filter, File... files) throws IORuntimeException {
    for (File file : files) {
        // 如果只是压缩一个文件,则需要截取该文件的父目录
        String srcRootDir;
        try {
            srcRootDir = file.getCanonicalPath();
            if ((false == file.isDirectory()) || withSrcDir) {
                // 若是文件,则将父目录完整路径都截取掉;若设置包含目录,则将上级目录全部截取掉,保留本目录名
                srcRootDir = file.getCanonicalFile().getParentFile().getCanonicalPath();
            }
        } catch (IOException e) {
            throw new IORuntimeException(e);
        }
        _add(file, srcRootDir, filter);
    }
    return this;
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) IOException(java.io.IOException) File(java.io.File)

Example 75 with IORuntimeException

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

the class ZipWriter method putEntry.

/**
 * 添加文件流到压缩包,添加后关闭输入文件流<br>
 * 如果输入流为{@code null},则只创建空目录
 *
 * @param path 压缩的路径, {@code null}和""表示根目录下
 * @param in   需要压缩的输入流,使用完后自动关闭,{@code null}表示加入空目录
 * @throws IORuntimeException IO异常
 */
private ZipWriter putEntry(String path, InputStream in) throws IORuntimeException {
    try {
        out.putNextEntry(new ZipEntry(path));
        if (null != in) {
            IoUtil.copy(in, out);
        }
        out.closeEntry();
    } catch (IOException e) {
        throw new IORuntimeException(e);
    } finally {
        IoUtil.close(in);
    }
    IoUtil.flush(this.out);
    return this;
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException)

Aggregations

IORuntimeException (cn.hutool.core.io.IORuntimeException)81 IOException (java.io.IOException)75 File (java.io.File)9 StreamProgress (cn.hutool.core.io.StreamProgress)4 FileOutputStream (java.io.FileOutputStream)4 InputStream (java.io.InputStream)4 OutputStream (java.io.OutputStream)4 UtilException (cn.hutool.core.exceptions.UtilException)3 POIException (cn.hutool.poi.exceptions.POIException)3 Rectangle (java.awt.Rectangle)3 BufferedImage (java.awt.image.BufferedImage)3 BufferedReader (java.io.BufferedReader)3 SocketChannel (java.nio.channels.SocketChannel)3 Path (java.nio.file.Path)3 Test (org.junit.Test)3 StreamGobbler (ch.ethz.ssh2.StreamGobbler)2 CryptoException (cn.hutool.crypto.CryptoException)2 RenderedImage (java.awt.image.RenderedImage)2 BufferedWriter (java.io.BufferedWriter)2 FileInputStream (java.io.FileInputStream)2