Search in sources :

Example 36 with IORuntimeException

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

the class Deflate method deflater.

/**
 * 将普通数据流压缩
 *
 * @param level 压缩级别,0~9
 * @return this
 */
public Deflate deflater(int level) {
    target = (target instanceof DeflaterOutputStream) ? (DeflaterOutputStream) target : new DeflaterOutputStream(target, new Deflater(level, nowrap));
    IoUtil.copy(source, target);
    try {
        ((DeflaterOutputStream) target).finish();
    } catch (IOException e) {
        throw new IORuntimeException(e);
    }
    return this;
}
Also used : Deflater(java.util.zip.Deflater) IORuntimeException(cn.hutool.core.io.IORuntimeException) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) IOException(java.io.IOException)

Example 37 with IORuntimeException

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

the class WatchMonitor method init.

// ------------------------------------------------------ Constructor method end
/**
 * 初始化<br>
 * 初始化包括:
 * <pre>
 * 1、解析传入的路径,判断其为目录还是文件
 * 2、创建{@link WatchService} 对象
 * </pre>
 *
 * @throws WatchException 监听异常,IO异常时抛出此异常
 */
@Override
public void init() throws WatchException {
    // 获取目录或文件路径
    if (false == Files.exists(this.path, LinkOption.NOFOLLOW_LINKS)) {
        // 不存在的路径
        final Path lastPathEle = FileUtil.getLastPathEle(this.path);
        if (null != lastPathEle) {
            final String lastPathEleStr = lastPathEle.toString();
            // 带有点表示有扩展名,按照未创建的文件对待。Linux下.d的为目录,排除之
            if (StrUtil.contains(lastPathEleStr, StrUtil.C_DOT) && false == StrUtil.endWithIgnoreCase(lastPathEleStr, ".d")) {
                this.filePath = this.path;
                this.path = this.filePath.getParent();
            }
        }
        // 创建不存在的目录或父目录
        try {
            Files.createDirectories(this.path);
        } catch (IOException e) {
            throw new IORuntimeException(e);
        }
    } else if (Files.isRegularFile(this.path, LinkOption.NOFOLLOW_LINKS)) {
        // 文件路径
        this.filePath = this.path;
        this.path = this.filePath.getParent();
    }
    super.init();
}
Also used : Path(java.nio.file.Path) IORuntimeException(cn.hutool.core.io.IORuntimeException) IOException(java.io.IOException)

Example 38 with IORuntimeException

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

the class SymmetricCrypto method decrypt.

@Override
public void decrypt(InputStream data, OutputStream out, boolean isClose) throws IORuntimeException {
    lock.lock();
    CipherInputStream cipherInputStream = null;
    try {
        final Cipher cipher = initMode(Cipher.DECRYPT_MODE);
        cipherInputStream = new CipherInputStream(data, cipher);
        if (this.isZeroPadding) {
            final int blockSize = cipher.getBlockSize();
            if (blockSize > 0) {
                copyForZeroPadding(cipherInputStream, out, blockSize);
                return;
            }
        }
        IoUtil.copy(cipherInputStream, out);
    } catch (IOException e) {
        throw new IORuntimeException(e);
    } catch (IORuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new CryptoException(e);
    } finally {
        lock.unlock();
        // issue#I4EMST@Gitee
        // CipherOutputStream必须关闭,才能完全写出
        IoUtil.close(cipherInputStream);
        if (isClose) {
            IoUtil.close(data);
        }
    }
}
Also used : CipherInputStream(javax.crypto.CipherInputStream) IORuntimeException(cn.hutool.core.io.IORuntimeException) Cipher(javax.crypto.Cipher) IOException(java.io.IOException) 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 39 with IORuntimeException

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

the class FileReader method writeToStream.

/**
 * 将文件写入流中
 *
 * @param out 流
 * @return File
 * @throws IORuntimeException IO异常
 */
public File writeToStream(OutputStream out) throws IORuntimeException {
    FileInputStream in = null;
    try {
        in = new FileInputStream(file);
        IoUtil.copy(in, out);
    } catch (IOException e) {
        throw new IORuntimeException(e);
    } finally {
        IoUtil.close(in);
    }
    return this.file;
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 40 with IORuntimeException

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

the class ImageUtil method sliceByRowsAndCols.

/**
 * 图像切割(指定切片的行数和列数)
 *
 * @param srcImage 源图像
 * @param destDir 切片目标文件夹
 * @param rows 目标切片行数。默认2,必须是范围 [1, 20] 之内
 * @param cols 目标切片列数。默认2,必须是范围 [1, 20] 之内
 */
public static void sliceByRowsAndCols(Image srcImage, File destDir, int rows, int cols) {
    if (false == destDir.exists()) {
        FileUtil.mkdir(destDir);
    } else if (false == destDir.isDirectory()) {
        throw new IllegalArgumentException("Destination Dir must be a Directory !");
    }
    try {
        if (rows <= 0 || rows > 20) {
            // 切片行数
            rows = 2;
        }
        if (cols <= 0 || cols > 20) {
            // 切片列数
            cols = 2;
        }
        // 读取源图像
        final BufferedImage bi = toBufferedImage(srcImage);
        // 源图宽度
        int srcWidth = bi.getWidth();
        // 源图高度
        int srcHeight = bi.getHeight();
        // 每张切片的宽度
        int destWidth = NumberUtil.partValue(srcWidth, cols);
        // 每张切片的高度
        int destHeight = NumberUtil.partValue(srcHeight, rows);
        // 循环建立切片
        BufferedImage tag;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                tag = cut(bi, new Rectangle(j * destWidth, i * destHeight, destWidth, destHeight));
                // 输出为文件
                ImageIO.write(tag, IMAGE_TYPE_JPEG, new File(destDir, "_r" + i + "_c" + j + ".jpg"));
            }
        }
    } catch (IOException e) {
        throw new IORuntimeException(e);
    }
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) Rectangle(java.awt.Rectangle) IOException(java.io.IOException) File(java.io.File) BufferedImage(java.awt.image.BufferedImage)

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