Search in sources :

Example 6 with UtilException

use of cn.hutool.core.exceptions.UtilException in project hutool by looly.

the class ObjectUtil method unserialize.

/**
 * 反序列化<br>
 * 对象必须实现Serializable接口
 *
 * @param <T> 对象类型
 * @param bytes 反序列化的字节码
 * @return 反序列化后的对象
 */
@SuppressWarnings("unchecked")
public static <T> T unserialize(byte[] bytes) {
    ObjectInputStream ois = null;
    try {
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        ois = new ObjectInputStream(bais);
        return (T) ois.readObject();
    } catch (Exception e) {
        throw new UtilException(e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) UtilException(cn.hutool.core.exceptions.UtilException) UtilException(cn.hutool.core.exceptions.UtilException) ObjectInputStream(java.io.ObjectInputStream)

Example 7 with UtilException

use of cn.hutool.core.exceptions.UtilException in project hutool by looly.

the class XmlUtil method createXml.

// -------------------------------------------------------------------------------------- Create
/**
 * 创建XML文档<br>
 * 创建的XML默认是utf8编码,修改编码的过程是在toStr和toFile方法里,既XML在转为文本的时候才定义编码
 *
 * @return XML文档
 * @since 4.0.8
 */
public static Document createXml() {
    final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = null;
    try {
        builder = dbf.newDocumentBuilder();
    } catch (Exception e) {
        throw new UtilException(e, "Create xml document error!");
    }
    final Document doc = builder.newDocument();
    return doc;
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) UtilException(cn.hutool.core.exceptions.UtilException) Document(org.w3c.dom.Document) XPathExpressionException(javax.xml.xpath.XPathExpressionException) UtilException(cn.hutool.core.exceptions.UtilException) IOException(java.io.IOException)

Example 8 with UtilException

use of cn.hutool.core.exceptions.UtilException in project hutool by looly.

the class ZipUtil method zip.

/**
 * 对文件或文件目录进行压缩
 *
 * @param zipFile 生成的Zip文件,包括文件名。注意:zipPath不能是srcPath路径下的子文件夹
 * @param charset 编码
 * @param withSrcDir 是否包含被打包目录
 * @param srcFiles 要压缩的源文件或目录。如果压缩一个文件,则为该文件的全路径;如果压缩一个目录,则为该目录的顶层目录路径
 * @return 压缩文件
 * @throws UtilException IO异常
 */
public static File zip(File zipFile, Charset charset, boolean withSrcDir, File... srcFiles) throws UtilException {
    validateFiles(zipFile, srcFiles);
    try (ZipOutputStream out = getZipOutputStream(zipFile, charset)) {
        String srcRootDir;
        for (File srcFile : srcFiles) {
            // 如果只是压缩一个文件,则需要截取该文件的父目录
            srcRootDir = srcFile.getCanonicalPath();
            if (srcFile.isFile() || withSrcDir) {
                srcRootDir = srcFile.getParent();
            }
            // 调用递归压缩方法进行目录或文件压缩
            zip(srcFile, srcRootDir, out);
            out.flush();
        }
    } catch (IOException e) {
        throw new UtilException(e);
    }
    return zipFile;
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) UtilException(cn.hutool.core.exceptions.UtilException) IOException(java.io.IOException) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Example 9 with UtilException

use of cn.hutool.core.exceptions.UtilException in project hutool by looly.

the class ZipUtil method gzip.

/**
 * Gzip压缩文件
 *
 * @param file 被压缩的文件
 * @return 压缩后的字节流
 * @throws UtilException IO异常
 */
public static byte[] gzip(File file) throws UtilException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream((int) file.length());
    GZIPOutputStream gos = null;
    BufferedInputStream in;
    try {
        gos = new GZIPOutputStream(bos);
        in = FileUtil.getInputStream(file);
        IoUtil.copy(in, gos);
        return bos.toByteArray();
    } catch (IOException e) {
        throw new UtilException(e);
    } finally {
        IoUtil.close(gos);
    }
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) BufferedInputStream(java.io.BufferedInputStream) UtilException(cn.hutool.core.exceptions.UtilException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FastByteArrayOutputStream(cn.hutool.core.io.FastByteArrayOutputStream) IOException(java.io.IOException)

Example 10 with UtilException

use of cn.hutool.core.exceptions.UtilException in project hutool by looly.

the class ZipUtil method addFile.

/**
 * 添加文件流到压缩包,不关闭输入流
 *
 * @param in 需要压缩的输入流
 * @param path 压缩的路径
 * @param out 压缩文件存储对象
 * @throws UtilException IO异常
 */
private static void addFile(InputStream in, String path, ZipOutputStream out) throws UtilException {
    if (null == in) {
        return;
    }
    try {
        out.putNextEntry(new ZipEntry(path));
        IoUtil.copy(in, out);
    } catch (IOException e) {
        throw new UtilException(e);
    } finally {
        closeEntry(out);
    }
}
Also used : UtilException(cn.hutool.core.exceptions.UtilException) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException)

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