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);
}
}
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;
}
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;
}
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);
}
}
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);
}
}
Aggregations