Search in sources :

Example 1 with UtilException

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

the class ClassUtil method getClassPaths.

/**
 * 获得ClassPath
 *
 * @param packageName 包名称
 * @return ClassPath路径字符串集合
 */
public static Set<String> getClassPaths(String packageName) {
    String packagePath = packageName.replace(StrUtil.DOT, StrUtil.SLASH);
    Enumeration<URL> resources;
    try {
        resources = getClassLoader().getResources(packagePath);
    } catch (IOException e) {
        throw new UtilException(e, "Loading classPath [{}] error!", packagePath);
    }
    Set<String> paths = new HashSet<String>();
    while (resources.hasMoreElements()) {
        paths.add(resources.nextElement().getPath());
    }
    return paths;
}
Also used : UtilException(cn.hutool.core.exceptions.UtilException) IOException(java.io.IOException) URL(java.net.URL) HashSet(java.util.HashSet)

Example 2 with UtilException

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

the class NetUtil method getMacAddress.

/**
 * 获得指定地址信息中的MAC地址
 *
 * @param inetAddress {@link InetAddress}
 * @param separator 分隔符,推荐使用“-”或者“:”
 * @return MAC地址,用-分隔
 */
public static String getMacAddress(InetAddress inetAddress, String separator) {
    if (null == inetAddress) {
        return null;
    }
    byte[] mac;
    try {
        mac = NetworkInterface.getByInetAddress(inetAddress).getHardwareAddress();
    } catch (SocketException e) {
        throw new UtilException(e);
    }
    if (null != mac) {
        final StringBuilder sb = new StringBuilder();
        String s;
        for (int i = 0; i < mac.length; i++) {
            if (i != 0) {
                sb.append(separator);
            }
            // 字节转换为整数
            s = Integer.toHexString(mac[i] & 0xFF);
            sb.append(s.length() == 1 ? 0 + s : s);
        }
        return sb.toString();
    }
    return null;
}
Also used : SocketException(java.net.SocketException) UtilException(cn.hutool.core.exceptions.UtilException)

Example 3 with UtilException

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

the class NumberUtil method generateBySet.

/**
 * 生成不重复随机数 根据给定的最小数字和最大数字,以及随机数的个数,产生指定的不重复的数组
 *
 * @param begin 最小数字(包含该数)
 * @param end 最大数字(不包含该数)
 * @param size 指定产生随机数的个数
 * @return 随机int数组
 */
public static Integer[] generateBySet(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!");
    }
    Random ran = new Random();
    Set<Integer> set = new HashSet<Integer>();
    while (set.size() < size) {
        set.add(begin + ran.nextInt(end - begin));
    }
    Integer[] ranArr = set.toArray(new Integer[size]);
    return ranArr;
}
Also used : BigInteger(java.math.BigInteger) Random(java.util.Random) UtilException(cn.hutool.core.exceptions.UtilException) HashSet(java.util.HashSet)

Example 4 with UtilException

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

the class ObjectUtil method serialize.

/**
 * 序列化<br>
 * 对象必须实现Serializable接口
 *
 * @param <T> 对象类型
 * @param obj 要被序列化的对象
 * @return 序列化后的字节码
 */
public static <T> byte[] serialize(T obj) {
    if (null == obj || false == (obj instanceof Serializable)) {
        return null;
    }
    FastByteArrayOutputStream byteOut = new FastByteArrayOutputStream();
    ObjectOutputStream oos = null;
    try {
        oos = new ObjectOutputStream(byteOut);
        oos.writeObject(obj);
        oos.flush();
    } catch (Exception e) {
        throw new UtilException(e);
    } finally {
        IoUtil.close(oos);
    }
    return byteOut.toByteArray();
}
Also used : Serializable(java.io.Serializable) FastByteArrayOutputStream(cn.hutool.core.io.FastByteArrayOutputStream) UtilException(cn.hutool.core.exceptions.UtilException) ObjectOutputStream(java.io.ObjectOutputStream) UtilException(cn.hutool.core.exceptions.UtilException)

Example 5 with UtilException

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

the class ObjectUtil method cloneByStream.

/**
 * 序列化后拷贝流的方式克隆<br>
 * 对象必须实现Serializable接口
 *
 * @param <T> 对象类型
 * @param obj 被克隆对象
 * @return 克隆后的对象
 * @throws UtilException IO异常和ClassNotFoundException封装
 */
@SuppressWarnings("unchecked")
public static <T> T cloneByStream(T obj) {
    if (null == obj || false == (obj instanceof Serializable)) {
        return null;
    }
    final FastByteArrayOutputStream byteOut = new FastByteArrayOutputStream();
    ObjectOutputStream out = null;
    try {
        out = new ObjectOutputStream(byteOut);
        out.writeObject(obj);
        out.flush();
        final ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(byteOut.toByteArray()));
        return (T) in.readObject();
    } catch (Exception e) {
        throw new UtilException(e);
    } finally {
        IoUtil.close(out);
    }
}
Also used : Serializable(java.io.Serializable) FastByteArrayOutputStream(cn.hutool.core.io.FastByteArrayOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) UtilException(cn.hutool.core.exceptions.UtilException) ObjectOutputStream(java.io.ObjectOutputStream) UtilException(cn.hutool.core.exceptions.UtilException) ObjectInputStream(java.io.ObjectInputStream)

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