use of cn.hutool.core.io.IORuntimeException in project hutool by looly.
the class NioClient method init.
/**
* 初始化
*
* @param address 地址和端口
* @return this
*/
public NioClient init(InetSocketAddress address) {
try {
// 创建一个SocketChannel对象,配置成非阻塞模式
this.channel = SocketChannel.open();
channel.configureBlocking(false);
channel.connect(address);
// 创建一个选择器,并把SocketChannel交给selector对象
this.selector = Selector.open();
channel.register(this.selector, SelectionKey.OP_READ);
// noinspection StatementWithEmptyBody
while (false == channel.finishConnect()) {
}
} catch (IOException e) {
close();
throw new IORuntimeException(e);
}
return this;
}
use of cn.hutool.core.io.IORuntimeException in project hutool by looly.
the class NioUtil method registerChannel.
/**
* 注册通道的指定操作到指定Selector上
*
* @param selector Selector
* @param channel 通道
* @param ops 注册的通道监听(操作)类型
*/
public static void registerChannel(Selector selector, SelectableChannel channel, Operation ops) {
if (channel == null) {
return;
}
try {
channel.configureBlocking(false);
// 注册通道
// noinspection MagicConstant
channel.register(selector, ops.getValue());
} catch (IOException e) {
throw new IORuntimeException(e);
}
}
use of cn.hutool.core.io.IORuntimeException in project hutool by looly.
the class AioServer method init.
/**
* 初始化
*
* @param address 地址和端口
* @return this
*/
public AioServer init(InetSocketAddress address) {
try {
this.group = //
AsynchronousChannelGroup.withFixedThreadPool(// 默认线程池大小
config.getThreadPoolSize(), //
ThreadFactoryBuilder.create().setNamePrefix("Hutool-socket-").build());
this.channel = AsynchronousServerSocketChannel.open(group).bind(address);
} catch (IOException e) {
throw new IORuntimeException(e);
}
return this;
}
use of cn.hutool.core.io.IORuntimeException in project hutool by looly.
the class FileCopier method internalCopyDirContent.
// ----------------------------------------------------------------------------------------- Private method start
/**
* 拷贝目录内容,只用于内部,不做任何安全检查<br>
* 拷贝内容的意思为源目录下的所有文件和目录拷贝到另一个目录下,而不拷贝源目录本身
*
* @param src 源目录
* @param dest 目标目录
* @throws IORuntimeException IO异常
*/
private void internalCopyDirContent(File src, File dest) throws IORuntimeException {
if (null != copyFilter && false == copyFilter.accept(src)) {
// 被过滤的目录跳过
return;
}
if (false == dest.exists()) {
// 目标为不存在路径,创建为目录
// noinspection ResultOfMethodCallIgnored
dest.mkdirs();
} else if (false == dest.isDirectory()) {
throw new IORuntimeException(StrUtil.format("Src [{}] is a directory but dest [{}] is a file!", src.getPath(), dest.getPath()));
}
final String[] files = src.list();
if (ArrayUtil.isNotEmpty(files)) {
File srcFile;
File destFile;
for (String file : files) {
srcFile = new File(src, file);
destFile = this.isOnlyCopyFile ? dest : new File(dest, file);
// 递归复制
if (srcFile.isDirectory()) {
internalCopyDirContent(srcFile, destFile);
} else {
internalCopyFile(srcFile, destFile);
}
}
}
}
use of cn.hutool.core.io.IORuntimeException in project hutool by looly.
the class FileWriter method write.
/**
* 写入数据到文件
*
* @param data 数据
* @param off 数据开始位置
* @param len 数据长度
* @param isAppend 是否追加模式
* @return 目标文件
* @throws IORuntimeException IO异常
*/
public File write(byte[] data, int off, int len, boolean isAppend) throws IORuntimeException {
try (FileOutputStream out = new FileOutputStream(FileUtil.touch(file), isAppend)) {
out.write(data, off, len);
out.flush();
} catch (IOException e) {
throw new IORuntimeException(e);
}
return file;
}
Aggregations