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