Search in sources :

Example 41 with IORuntimeException

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

the class NetUtil method netCat.

/**
 * 简易的使用Socket发送数据
 *
 * @param host Server主机
 * @param port Server端口
 * @param isBlock 是否阻塞方式
 * @param data 需要发送的数据
 * @throws IORuntimeException IO异常
 * @since 3.3.0
 */
public static void netCat(String host, int port, boolean isBlock, ByteBuffer data) throws IORuntimeException {
    try (SocketChannel channel = SocketChannel.open(createAddress(host, port))) {
        channel.configureBlocking(isBlock);
        channel.write(data);
    } catch (IOException e) {
        throw new IORuntimeException(e);
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) IORuntimeException(cn.hutool.core.io.IORuntimeException) IOException(java.io.IOException)

Example 42 with IORuntimeException

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

the class NetUtil method netCat.

/**
 * 使用普通Socket发送数据
 *
 * @param host Server主机
 * @param port Server端口
 * @param data 数据
 * @throws IOException IO异常
 * @since 3.3.0
 */
public static void netCat(String host, int port, byte[] data) throws IORuntimeException {
    OutputStream out = null;
    try (Socket socket = new Socket(host, port)) {
        out = socket.getOutputStream();
        out.write(data);
        out.flush();
    } catch (IOException e) {
        throw new IORuntimeException(e);
    } finally {
        IoUtil.close(out);
    }
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) OutputStream(java.io.OutputStream) IOException(java.io.IOException) Socket(java.net.Socket)

Example 43 with IORuntimeException

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

the class RuntimeUtil method exec.

/**
 * 执行命令<br>
 * 命令带参数时参数可作为其中一个参数,也可以将命令和参数组合为一个字符串传入
 *
 * @param cmds 命令
 * @return {@link Process}
 */
public static Process exec(String... cmds) {
    if (ArrayUtil.isEmpty(cmds)) {
        throw new NullPointerException("Command is empty !");
    }
    // 单条命令的情况
    if (1 == cmds.length) {
        final String cmd = cmds[0];
        if (StrUtil.isBlank(cmd)) {
            throw new NullPointerException("Command is empty !");
        }
        cmds = StrUtil.splitToArray(cmd, StrUtil.C_SPACE);
    }
    Process process;
    try {
        process = new ProcessBuilder(cmds).redirectErrorStream(true).start();
    } catch (IOException e) {
        throw new IORuntimeException(e);
    }
    return process;
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) IOException(java.io.IOException)

Example 44 with IORuntimeException

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

the class JschUtil method exec.

/**
 * 打开Exec连接<br>
 * 获取ChannelExec后首先
 *
 * @param session Session会话
 * @param cmd 命令
 * @param charset 发送和读取内容的编码
 * @return {@link ChannelExec}
 * @since 4.0.3
 */
public static String exec(Session session, String cmd, Charset charset) {
    if (null == charset) {
        charset = CharsetUtil.CHARSET_UTF_8;
    }
    ChannelExec channel;
    try {
        channel = (ChannelExec) session.openChannel("exec");
    } catch (JSchException e) {
        throw new JschRuntimeException(e);
    }
    channel.setCommand(StrUtil.bytes(cmd, charset));
    channel.setInputStream(null);
    channel.setErrStream(System.err);
    InputStream in = null;
    try {
        // 执行命令 等待执行结束
        channel.connect();
        in = channel.getInputStream();
        return IoUtil.read(in, CharsetUtil.CHARSET_UTF_8);
    } catch (IOException e) {
        throw new IORuntimeException(e);
    } catch (JSchException e) {
        throw new JschRuntimeException(e);
    } finally {
        IoUtil.close(in);
        close(channel);
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) IORuntimeException(cn.hutool.core.io.IORuntimeException) InputStream(java.io.InputStream) IOException(java.io.IOException) ChannelExec(com.jcraft.jsch.ChannelExec)

Example 45 with IORuntimeException

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

the class ZipUtil method append.

/**
 * 在zip文件中添加新文件或目录<br>
 * 新文件添加在zip根目录,文件夹包括其本身和内容<br>
 * 如果待添加文件夹是系统根路径(如/或c:/),则只复制文件夹下的内容
 *
 * @param zipPath        zip文件的Path
 * @param appendFilePath 待添加文件Path(可以是文件夹)
 * @param options        拷贝选项,可选是否覆盖等
 * @throws IORuntimeException IO异常
 * @since 5.7.15
 */
public static void append(Path zipPath, Path appendFilePath, CopyOption... options) throws IORuntimeException {
    try (FileSystem zipFileSystem = FileSystemUtil.createZip(zipPath.toString())) {
        if (Files.isDirectory(appendFilePath)) {
            Path source = appendFilePath.getParent();
            if (null == source) {
                // 如果用户提供的是根路径,则不复制目录,直接复制目录下的内容
                source = appendFilePath;
            }
            Files.walkFileTree(appendFilePath, new ZipCopyVisitor(source, zipFileSystem, options));
        } else {
            Files.copy(appendFilePath, zipFileSystem.getPath(PathUtil.getName(appendFilePath)), options);
        }
    } catch (FileAlreadyExistsException ignored) {
    // 不覆盖情况下,文件已存在, 跳过
    } catch (IOException e) {
        throw new IORuntimeException(e);
    }
}
Also used : Path(java.nio.file.Path) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) ZipCopyVisitor(cn.hutool.core.compress.ZipCopyVisitor) IORuntimeException(cn.hutool.core.io.IORuntimeException) FileSystem(java.nio.file.FileSystem) 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