use of cn.hutool.core.io.IORuntimeException in project hutool by looly.
the class GanymedUtil method execByShell.
/**
* 执行Shell命令
* <p>
* 此方法单次发送一个命令到服务端,自动读取环境变量,执行结束后自动关闭Session,可能产生阻塞。
* </p>
*
* @param session Session会话
* @param cmd 命令
* @param charset 发送和读取内容的编码
* @param errStream 错误信息输出到的位置
* @return 执行返回结果
*/
public static String execByShell(Session session, String cmd, Charset charset, OutputStream errStream) {
final String result;
try {
session.requestDumbPTY();
IoUtil.write(session.getStdin(), charset, true, cmd);
result = IoUtil.read(new StreamGobbler(session.getStdout()), charset);
if (null != errStream) {
// 错误输出
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 FreemarkerEngine method createCfg.
/**
* 创建配置项
*
* @param config 模板配置
* @return {@link Configuration }
*/
private static Configuration createCfg(TemplateConfig config) {
if (null == config) {
config = new TemplateConfig();
}
final Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);
cfg.setLocalizedLookup(false);
cfg.setDefaultEncoding(config.getCharset().toString());
switch(config.getResourceMode()) {
case CLASSPATH:
cfg.setTemplateLoader(new ClassTemplateLoader(ClassUtil.getClassLoader(), config.getPath()));
break;
case FILE:
try {
cfg.setTemplateLoader(new FileTemplateLoader(FileUtil.file(config.getPath())));
} catch (IOException e) {
throw new IORuntimeException(e);
}
break;
case WEB_ROOT:
try {
cfg.setTemplateLoader(new FileTemplateLoader(FileUtil.file(FileUtil.getWebRoot(), config.getPath())));
} catch (IOException e) {
throw new IORuntimeException(e);
}
break;
case STRING:
cfg.setTemplateLoader(new SimpleStringTemplateLoader());
break;
default:
break;
}
return cfg;
}
use of cn.hutool.core.io.IORuntimeException in project hutool by looly.
the class Props method store.
/**
* 持久化当前设置,会覆盖掉之前的设置
*
* @param absolutePath 设置文件的绝对路径
* @throws IORuntimeException IO异常,可能为文件未找到
*/
public void store(String absolutePath) throws IORuntimeException {
Writer writer = null;
try {
writer = FileUtil.getWriter(absolutePath, charset, false);
super.store(writer, null);
} catch (IOException e) {
throw new IORuntimeException(e, "Store properties to [{}] error!", absolutePath);
} finally {
IoUtil.close(writer);
}
}
use of cn.hutool.core.io.IORuntimeException in project hutool by looly.
the class NioServerTest method main.
public static void main(String[] args) {
NioServer server = new NioServer(8080);
server.setChannelHandler((sc) -> {
ByteBuffer readBuffer = ByteBuffer.allocate(1024);
try {
// 从channel读数据到缓冲区
int readBytes = sc.read(readBuffer);
if (readBytes > 0) {
// Flips this buffer. The limit is set to the current position and then
// the position is set to zero,就是表示要从起始位置开始读取数据
readBuffer.flip();
// eturns the number of elements between the current position and the limit.
// 要读取的字节长度
byte[] bytes = new byte[readBuffer.remaining()];
// 将缓冲区的数据读到bytes数组
readBuffer.get(bytes);
String body = StrUtil.utf8Str(bytes);
Console.log("[{}]: {}", sc.getRemoteAddress(), body);
doWrite(sc, body);
} else if (readBytes < 0) {
IoUtil.close(sc);
}
} catch (IOException e) {
throw new IORuntimeException(e);
}
});
server.listen();
}
use of cn.hutool.core.io.IORuntimeException in project hutool by looly.
the class AcceptHandler method completed.
@Override
public void completed(ServerSocketChannel serverSocketChannel, NioServer nioServer) {
SocketChannel socketChannel;
try {
// 获取连接到此服务器的客户端通道
socketChannel = serverSocketChannel.accept();
StaticLog.debug("Client [{}] accepted.", socketChannel.getRemoteAddress());
} catch (IOException e) {
throw new IORuntimeException(e);
}
// SocketChannel通道的可读事件注册到Selector中
NioUtil.registerChannel(nioServer.getSelector(), socketChannel, Operation.READ);
}
Aggregations