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