Search in sources :

Example 11 with IORuntimeException

use of cn.hutool.core.io.IORuntimeException in project hutool by looly.

the class ExcelSaxUtil method readFrom.

/**
 * 从Excel的XML文档中读取内容,并使用{@link ContentHandler}处理
 *
 * @param xmlDocStream Excel的XML文档流
 * @param handler      文档内容处理接口,实现此接口用于回调处理数据
 * @throws DependencyException 依赖异常
 * @throws POIException        POI异常,包装了SAXException
 * @throws IORuntimeException  IO异常,如流关闭或异常等
 * @since 5.1.4
 */
public static void readFrom(InputStream xmlDocStream, ContentHandler handler) throws DependencyException, POIException, IORuntimeException {
    XMLReader xmlReader;
    try {
        // xmlReader = XMLReaderFactory.createXMLReader();
        // noinspection deprecation
        xmlReader = SAXHelper.newXMLReader();
    } catch (SAXException | ParserConfigurationException e) {
        if (e.getMessage().contains("org.apache.xerces.parsers.SAXParser")) {
            throw new DependencyException(e, "You need to add 'xerces:xercesImpl' to your project and version >= 2.11.0");
        } else {
            throw new POIException(e);
        }
    }
    xmlReader.setContentHandler(handler);
    try {
        xmlReader.parse(new InputSource(xmlDocStream));
    } catch (IOException e) {
        throw new IORuntimeException(e);
    } catch (SAXException e) {
        throw new POIException(e);
    }
}
Also used : InputSource(org.xml.sax.InputSource) IORuntimeException(cn.hutool.core.io.IORuntimeException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) DependencyException(cn.hutool.core.exceptions.DependencyException) XMLReader(org.xml.sax.XMLReader) SAXException(org.xml.sax.SAXException) POIException(cn.hutool.poi.exceptions.POIException)

Example 12 with IORuntimeException

use of cn.hutool.core.io.IORuntimeException in project hutool by looly.

the class SheetRidReader method read.

/**
 * 读取Wordkbook的XML中sheet标签中sheetId和rid的对应关系
 *
 * @param xssfReader XSSF读取器
 * @return this
 */
public SheetRidReader read(XSSFReader xssfReader) {
    InputStream workbookData = null;
    try {
        workbookData = xssfReader.getWorkbookData();
        ExcelSaxUtil.readFrom(workbookData, this);
    } catch (InvalidFormatException e) {
        throw new POIException(e);
    } catch (IOException e) {
        throw new IORuntimeException(e);
    } finally {
        IoUtil.close(workbookData);
    }
    return this;
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) InputStream(java.io.InputStream) IOException(java.io.IOException) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) POIException(cn.hutool.poi.exceptions.POIException)

Example 13 with IORuntimeException

use of cn.hutool.core.io.IORuntimeException in project hutool by looly.

the class Word07Writer method flush.

/**
 * 将Word Document刷出到输出流
 *
 * @param out        输出流
 * @param isCloseOut 是否关闭输出流
 * @return this
 * @throws IORuntimeException IO异常
 */
public Word07Writer flush(OutputStream out, boolean isCloseOut) throws IORuntimeException {
    Assert.isFalse(this.isClosed, "WordWriter has been closed!");
    try {
        this.doc.write(out);
        out.flush();
    } catch (IOException e) {
        throw new IORuntimeException(e);
    } finally {
        if (isCloseOut) {
            IoUtil.close(out);
        }
    }
    return this;
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) IOException(java.io.IOException)

Example 14 with IORuntimeException

use of cn.hutool.core.io.IORuntimeException in project hutool by looly.

the class XmlUtil method readBySax.

/**
 * 使用Sax方式读取指定的XML<br>
 * 如果用户传入的contentHandler为{@link DefaultHandler},则其接口都会被处理
 *
 * @param source         XML源,可以是文件、流、路径等
 * @param contentHandler XML流处理器,用于按照Element处理xml
 * @since 5.4.4
 */
public static void readBySax(InputSource source, ContentHandler contentHandler) {
    // 1.获取解析工厂
    if (null == factory) {
        factory = SAXParserFactory.newInstance();
        factory.setValidating(false);
        factory.setNamespaceAware(namespaceAware);
    }
    // 2.从解析工厂获取解析器
    final SAXParser parse;
    XMLReader reader;
    try {
        parse = factory.newSAXParser();
        if (contentHandler instanceof DefaultHandler) {
            parse.parse(source, (DefaultHandler) contentHandler);
            return;
        }
        // 3.得到解读器
        reader = parse.getXMLReader();
        reader.setContentHandler(contentHandler);
        reader.parse(source);
    } catch (ParserConfigurationException | SAXException e) {
        throw new UtilException(e);
    } catch (IOException e) {
        throw new IORuntimeException(e);
    }
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) UtilException(cn.hutool.core.exceptions.UtilException) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) XMLReader(org.xml.sax.XMLReader) DefaultHandler(org.xml.sax.helpers.DefaultHandler) SAXException(org.xml.sax.SAXException)

Example 15 with IORuntimeException

use of cn.hutool.core.io.IORuntimeException in project hutool by looly.

the class JschUtil method exec.

/**
 * 执行Shell命令(使用EXEC方式)
 * <p>
 * 此方法单次发送一个命令到服务端,不读取环境变量,执行结束后自动关闭channel,不会产生阻塞。
 * </p>
 *
 * @param session   Session会话
 * @param cmd       命令
 * @param charset   发送和读取内容的编码
 * @param errStream 错误信息输出到的位置
 * @return 执行结果内容
 * @since 4.3.1
 */
public static String exec(Session session, String cmd, Charset charset, OutputStream errStream) {
    if (null == charset) {
        charset = CharsetUtil.CHARSET_UTF_8;
    }
    final ChannelExec channel = (ChannelExec) createChannel(session, ChannelType.EXEC);
    channel.setCommand(StrUtil.bytes(cmd, charset));
    channel.setInputStream(null);
    channel.setErrStream(errStream);
    InputStream in = null;
    try {
        channel.connect();
        in = channel.getInputStream();
        return IoUtil.read(in, charset);
    } catch (IOException e) {
        throw new IORuntimeException(e);
    } catch (JSchException e) {
        throw new JschRuntimeException(e);
    } finally {
        IoUtil.close(in);
        close(channel);
    }
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) InputStream(java.io.InputStream) 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