Search in sources :

Example 21 with IORuntimeException

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

Example 22 with IORuntimeException

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

Example 23 with IORuntimeException

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

Example 24 with IORuntimeException

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

Example 25 with IORuntimeException

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