use of cn.hutool.core.io.IORuntimeException in project hutool by looly.
the class GanymedUtil method exec.
/**
* 执行Shell命令(使用EXEC方式)
* <p>
* 此方法单次发送一个命令到服务端,不读取环境变量,执行结束后自动关闭Session,不会产生阻塞。
* </p>
*
* @param session Session会话
* @param cmd 命令
* @param charset 发送和读取内容的编码
* @param errStream 错误信息输出到的位置
* @return 执行返回结果
*/
public static String exec(Session session, String cmd, Charset charset, OutputStream errStream) {
final String result;
try {
session.execCommand(cmd, charset.name());
result = IoUtil.read(new StreamGobbler(session.getStdout()), charset);
// 错误输出
IoUtil.copy(new StreamGobbler(session.getStderr()), errStream);
} catch (IOException e) {
throw new IORuntimeException(e);
} finally {
close(session);
}
return result;
}
use of cn.hutool.core.io.IORuntimeException in project hutool by looly.
the class JschUtil method execByShell.
/**
* 执行Shell命令
* <p>
* 此方法单次发送一个命令到服务端,自动读取环境变量,执行结束后自动关闭channel,不会产生阻塞。
* </p>
*
* @param session Session会话
* @param cmd 命令
* @param charset 发送和读取内容的编码
* @return {@link ChannelExec}
* @since 5.2.5
*/
public static String execByShell(Session session, String cmd, Charset charset) {
final ChannelShell shell = openShell(session);
// 开始连接
shell.setPty(true);
OutputStream out = null;
InputStream in = null;
try {
out = shell.getOutputStream();
in = shell.getInputStream();
out.write(StrUtil.bytes(cmd, charset));
out.flush();
return IoUtil.read(in, charset);
} catch (IOException e) {
throw new IORuntimeException(e);
} finally {
IoUtil.close(out);
IoUtil.close(in);
close(shell);
}
}
use of cn.hutool.core.io.IORuntimeException in project hutool by looly.
the class CsvParser method nextRow.
/**
* 读取下一行数据
*
* @return CsvRow
* @throws IORuntimeException IO读取异常
*/
public CsvRow nextRow() throws IORuntimeException {
List<String> currentFields;
int fieldCount;
while (false == finished) {
currentFields = readLine();
fieldCount = currentFields.size();
if (fieldCount < 1) {
// 空List表示读取结束
break;
}
// 读取范围校验
if (lineNo < config.beginLineNo) {
// 未达到读取起始行,继续
continue;
}
if (lineNo > config.endLineNo) {
// 超出结束行,读取结束
break;
}
// 跳过空行
if (config.skipEmptyRows && fieldCount == 1 && currentFields.get(0).isEmpty()) {
// [""]表示空行
continue;
}
// 检查每行的字段数是否一致
if (config.errorOnDifferentFieldCount) {
if (firstLineFieldCount < 0) {
firstLineFieldCount = fieldCount;
} else if (fieldCount != firstLineFieldCount) {
throw new IORuntimeException(String.format("Line %d has %d fields, but first line has %d fields", lineNo, fieldCount, firstLineFieldCount));
}
}
// 记录最大字段数
if (fieldCount > maxFieldCount) {
maxFieldCount = fieldCount;
}
// 初始化标题
if (config.containsHeader && null == header) {
initHeader(currentFields);
// 作为标题行后,此行跳过,下一行做为第一行
continue;
}
return new CsvRow(lineNo, null == header ? null : header.headerMap, currentFields);
}
return null;
}
use of cn.hutool.core.io.IORuntimeException in project hutool by looly.
the class CsvWriter method writeComment.
/**
* 写出一行注释,注释符号可自定义<br>
* 如果注释符不存在,则抛出异常
*
* @param comment 注释内容
* @return this
* @see CsvConfig#commentCharacter
* @since 5.5.7
*/
public CsvWriter writeComment(String comment) {
Assert.notNull(this.config.commentCharacter, "Comment is disable!");
try {
if (isFirstLine) {
// 首行不补换行符
isFirstLine = false;
} else {
writer.write(config.lineDelimiter);
}
writer.write(this.config.commentCharacter);
writer.write(comment);
newline = true;
} catch (IOException e) {
throw new IORuntimeException(e);
}
return this;
}
use of cn.hutool.core.io.IORuntimeException in project hutool by looly.
the class Ftp method delDir.
@Override
public boolean delDir(String dirPath) throws IORuntimeException {
FTPFile[] dirs;
try {
dirs = client.listFiles(dirPath);
} catch (IOException e) {
throw new IORuntimeException(e);
}
String name;
String childPath;
for (FTPFile ftpFile : dirs) {
name = ftpFile.getName();
childPath = StrUtil.format("{}/{}", dirPath, name);
if (ftpFile.isDirectory()) {
// 上级和本级目录除外
if (false == ".".equals(name) && false == "..".equals(name)) {
delDir(childPath);
}
} else {
delFile(childPath);
}
}
// 删除空目录
try {
return this.client.removeDirectory(dirPath);
} catch (IOException e) {
throw new IORuntimeException(e);
}
}
Aggregations