Search in sources :

Example 11 with UtilException

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;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) UtilException(cn.hutool.core.exceptions.UtilException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FastByteArrayOutputStream(cn.hutool.core.io.FastByteArrayOutputStream) IOException(java.io.IOException)

Example 12 with UtilException

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

Example 13 with UtilException

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

Example 14 with UtilException

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

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!");
    }
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) UtilException(cn.hutool.core.exceptions.UtilException) XPathExpressionException(javax.xml.xpath.XPathExpressionException) UtilException(cn.hutool.core.exceptions.UtilException) SAXException(org.xml.sax.SAXException) IORuntimeException(cn.hutool.core.io.IORuntimeException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

UtilException (cn.hutool.core.exceptions.UtilException)50 IOException (java.io.IOException)19 FastByteArrayOutputStream (cn.hutool.core.io.FastByteArrayOutputStream)5 IORuntimeException (cn.hutool.core.io.IORuntimeException)5 URL (java.net.URL)5 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 File (java.io.File)3 ObjectInputStream (java.io.ObjectInputStream)3 PrintWriter (java.io.PrintWriter)3 AccessibleObject (java.lang.reflect.AccessibleObject)3 Method (java.lang.reflect.Method)3 MalformedURLException (java.net.MalformedURLException)3 SocketException (java.net.SocketException)3 HashSet (java.util.HashSet)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 XPathExpressionException (javax.xml.xpath.XPathExpressionException)3 ConcurrentHashSet (cn.hutool.core.collection.ConcurrentHashSet)2 BufferedInputStream (java.io.BufferedInputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2