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;
}
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();
}
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);
}
}
}
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;
}
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);
}
}
Aggregations