Search in sources :

Example 26 with UtilException

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

the class NumberUtil method generateRandomNumber.

// ------------------------------------------------------------------------------------------- generateXXX
/**
 * 生成不重复随机数 根据给定的最小数字和最大数字,以及随机数的个数,产生指定的不重复的数组
 *
 * @param begin 最小数字(包含该数)
 * @param end 最大数字(不包含该数)
 * @param size 指定产生随机数的个数
 * @return 随机int数组
 */
public static int[] generateRandomNumber(int begin, int end, int size) {
    if (begin > end) {
        int temp = begin;
        begin = end;
        end = temp;
    }
    // 加入逻辑判断,确保begin<end并且size不能大于该表示范围
    if ((end - begin) < size) {
        throw new UtilException("Size is larger than range between begin and end!");
    }
    // 种子你可以随意生成,但不能重复
    int[] seed = new int[end - begin];
    for (int i = begin; i < end; i++) {
        seed[i - begin] = i;
    }
    int[] ranArr = new int[size];
    Random ran = new Random();
    // 数量你可以自己定义。
    for (int i = 0; i < size; i++) {
        // 得到一个位置
        int j = ran.nextInt(seed.length - i);
        // 得到那个位置的数值
        ranArr[i] = seed[j];
        // 将最后一个未用的数字放到这里
        seed[j] = seed[seed.length - 1 - i];
    }
    return ranArr;
}
Also used : Random(java.util.Random) UtilException(cn.hutool.core.exceptions.UtilException)

Example 27 with UtilException

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

the class URLUtil method complateUrl.

/**
 * 补全相对路径
 *
 * @param baseUrl 基准URL
 * @param relativePath 相对URL
 * @return 相对路径
 * @exception UtilException MalformedURLException
 */
public static String complateUrl(String baseUrl, String relativePath) {
    baseUrl = formatUrl(baseUrl);
    if (StrUtil.isBlank(baseUrl)) {
        return null;
    }
    try {
        final URL absoluteUrl = new URL(baseUrl);
        final URL parseUrl = new URL(absoluteUrl, relativePath);
        return parseUrl.toString();
    } catch (MalformedURLException e) {
        throw new UtilException(e);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) UtilException(cn.hutool.core.exceptions.UtilException) URL(java.net.URL)

Example 28 with UtilException

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

the class XmlUtil method transform.

/**
 * 将XML文档写出
 *
 * @param source 源
 * @param result 目标
 * @param charset 编码
 * @param isPretty 是否格式化输出
 * @since 4.0.9
 */
public static void transform(Source source, Result result, String charset, boolean isPretty) {
    final TransformerFactory factory = TransformerFactory.newInstance();
    try {
        final Transformer xformer = factory.newTransformer();
        xformer.setOutputProperty(OutputKeys.INDENT, isPretty ? "yes" : "no");
        if (StrUtil.isNotBlank(charset)) {
            xformer.setOutputProperty(OutputKeys.ENCODING, charset);
        }
        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) IOException(java.io.IOException)

Example 29 with UtilException

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

the class ZipUtil method unzip.

/**
 * 解压
 *
 * @param zipFile zip文件
 * @param outFile 解压到的目录
 * @param charset 编码
 * @return 解压的目录
 * @throws UtilException IO异常
 * @since 3.2.2
 */
@SuppressWarnings("unchecked")
public static File unzip(File zipFile, File outFile, Charset charset) throws UtilException {
    charset = (null == charset) ? DEFAULT_CHARSET : charset;
    ZipFile zipFileObj = null;
    try {
        zipFileObj = new ZipFile(zipFile, charset);
        final Enumeration<ZipEntry> em = (Enumeration<ZipEntry>) zipFileObj.entries();
        ZipEntry zipEntry = null;
        File outItemFile = null;
        while (em.hasMoreElements()) {
            zipEntry = em.nextElement();
            outItemFile = new File(outFile, zipEntry.getName());
            if (zipEntry.isDirectory()) {
                outItemFile.mkdirs();
            } else {
                FileUtil.touch(outItemFile);
                copy(zipFileObj, zipEntry, outItemFile);
            }
        }
    } catch (IOException e) {
        throw new UtilException(e);
    } finally {
        IoUtil.close(zipFileObj);
    }
    return outFile;
}
Also used : Enumeration(java.util.Enumeration) ZipFile(java.util.zip.ZipFile) UtilException(cn.hutool.core.exceptions.UtilException) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Example 30 with UtilException

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

the class ZipUtil method gzip.

/**
 * Gzip压缩处理
 *
 * @param val 被压缩的字节流
 * @return 压缩后的字节流
 * @throws UtilException IO异常
 */
public static byte[] gzip(byte[] val) throws UtilException {
    FastByteArrayOutputStream bos = new FastByteArrayOutputStream(val.length);
    GZIPOutputStream gos = null;
    try {
        gos = new GZIPOutputStream(bos);
        gos.write(val, 0, val.length);
        gos.finish();
        gos.flush();
        val = bos.toByteArray();
    } catch (IOException e) {
        throw new UtilException(e);
    } finally {
        IoUtil.close(gos);
    }
    return val;
}
Also used : FastByteArrayOutputStream(cn.hutool.core.io.FastByteArrayOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) UtilException(cn.hutool.core.exceptions.UtilException) 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