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