Search in sources :

Example 16 with IORuntimeException

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

Example 17 with IORuntimeException

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;
}
Also used : Configuration(freemarker.template.Configuration) IORuntimeException(cn.hutool.core.io.IORuntimeException) ClassTemplateLoader(freemarker.cache.ClassTemplateLoader) TemplateConfig(cn.hutool.extra.template.TemplateConfig) IOException(java.io.IOException) FileTemplateLoader(freemarker.cache.FileTemplateLoader)

Example 18 with IORuntimeException

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

Example 19 with IORuntimeException

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

Example 20 with IORuntimeException

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