use of cn.hutool.core.exceptions.UtilException in project hutool by looly.
the class ZipUtil method unGzip.
/**
* Gzip解压处理
*
* @param buf buf
* @return bytes
* @throws UtilException IO异常
*/
public static byte[] unGzip(byte[] buf) throws UtilException {
GZIPInputStream gzi = null;
ByteArrayOutputStream bos = null;
try {
gzi = new GZIPInputStream(new ByteArrayInputStream(buf));
bos = new ByteArrayOutputStream(buf.length);
IoUtil.copy(gzi, bos);
buf = bos.toByteArray();
} catch (IOException e) {
throw new UtilException(e);
} finally {
IoUtil.close(gzi);
}
return buf;
}
use of cn.hutool.core.exceptions.UtilException in project hutool by looly.
the class VelocityUtil method toFile.
/**
* 生成文件
*
* @param template 模板
* @param context 模板上下文
* @param destPath 目标路径(绝对)
*/
public static void toFile(Template template, VelocityContext context, String destPath) {
PrintWriter writer = null;
try {
writer = FileUtil.getPrintWriter(destPath, Velocity.getProperty(Velocity.OUTPUT_ENCODING).toString(), false);
merge(template, context, writer);
} catch (IORuntimeException e) {
throw new UtilException(e, "Write Velocity content to [{}] error!", destPath);
} finally {
IoUtil.close(writer);
}
}
use of cn.hutool.core.exceptions.UtilException in project hutool by looly.
the class XmlUtil method readXML.
// -------------------------------------------------------------------------------------- Read
/**
* 读取解析XML文件
*
* @param file XML文件
* @return XML文档对象
*/
public static Document readXML(File file) {
Assert.notNull(file, "Xml file is null !");
if (false == file.exists()) {
throw new UtilException("File [{}] not a exist!", file.getAbsolutePath());
}
if (false == file.isFile()) {
throw new UtilException("[{}] not a file!", file.getAbsolutePath());
}
try {
file = file.getCanonicalFile();
} catch (IOException e) {
// ignore
}
BufferedInputStream in = null;
try {
in = FileUtil.getInputStream(file);
return readXML(in);
} finally {
IoUtil.close(in);
}
}
use of cn.hutool.core.exceptions.UtilException 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.exceptions.UtilException in project hutool by looly.
the class XmlUtil method transform.
/**
* 将XML文档写出<br>
* 格式化输出逻辑参考:https://stackoverflow.com/questions/139076/how-to-pretty-print-xml-from-java
*
* @param source 源
* @param result 目标
* @param charset 编码
* @param indent 格式化输出中缩进量,小于1表示不格式化输出
* @param omitXmlDeclaration 是否输出 xml Declaration
* @since 5.1.2
*/
public static void transform(Source source, Result result, String charset, int indent, boolean omitXmlDeclaration) {
final TransformerFactory factory = TransformerFactory.newInstance();
try {
final Transformer xformer = factory.newTransformer();
if (indent > 0) {
xformer.setOutputProperty(OutputKeys.INDENT, "yes");
// fix issue#1232@Github
xformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "yes");
xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indent));
}
if (StrUtil.isNotBlank(charset)) {
xformer.setOutputProperty(OutputKeys.ENCODING, charset);
}
if (omitXmlDeclaration) {
xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
}
xformer.transform(source, result);
} catch (Exception e) {
throw new UtilException(e, "Trans xml document to string error!");
}
}
Aggregations