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;
}
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;
}
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;
}
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();
}
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);
}
}
Aggregations