Search in sources :

Example 51 with IORuntimeException

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;
}
Also used : StreamGobbler(ch.ethz.ssh2.StreamGobbler) IORuntimeException(cn.hutool.core.io.IORuntimeException) IOException(java.io.IOException)

Example 52 with IORuntimeException

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);
    }
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException)

Example 53 with IORuntimeException

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;
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException)

Example 54 with IORuntimeException

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;
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) IOException(java.io.IOException)

Example 55 with IORuntimeException

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);
    }
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) FTPFile(org.apache.commons.net.ftp.FTPFile) 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