use of cn.hutool.core.exceptions.UtilException in project hutool by looly.
the class SoapUtil method toString.
/**
* {@link SOAPMessage} 转为字符串
*
* @param message SOAP消息对象
* @param pretty 是否格式化
* @param charset 编码
* @return SOAP XML字符串
* @since 4.5.7
*/
public static String toString(SOAPMessage message, boolean pretty, Charset charset) {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
message.writeTo(out);
} catch (SOAPException | IOException e) {
throw new SoapRuntimeException(e);
}
String messageToString;
try {
messageToString = out.toString(charset.toString());
} catch (UnsupportedEncodingException e) {
throw new UtilException(e);
}
return pretty ? XmlUtil.format(messageToString) : messageToString;
}
use of cn.hutool.core.exceptions.UtilException in project hutool by looly.
the class JarClassLoader method loadJar.
/**
* 加载Jar文件到指定loader中
*
* @param loader {@link URLClassLoader}
* @param jarFile 被加载的jar
* @throws UtilException IO异常包装和执行异常
*/
public static void loadJar(URLClassLoader loader, File jarFile) throws UtilException {
try {
final Method method = ClassUtil.getDeclaredMethod(URLClassLoader.class, "addURL", URL.class);
if (null != method) {
method.setAccessible(true);
final List<File> jars = loopJar(jarFile);
for (File jar : jars) {
ReflectUtil.invoke(loader, method, jar.toURI().toURL());
}
}
} catch (IOException e) {
throw new UtilException(e);
}
}
use of cn.hutool.core.exceptions.UtilException in project hutool by looly.
the class MethodHandleUtil method invoke.
/**
* 执行接口或对象中的方法<br>
*
* <pre class="code">
* interface Duck {
* default String quack() {
* return "Quack";
* }
* }
*
* Duck duck = (Duck) Proxy.newProxyInstance(
* ClassLoaderUtil.getClassLoader(),
* new Class[] { Duck.class },
* MethodHandleUtil::invoke);
* </pre>
*
* @param <T> 返回结果类型
* @param isSpecial 是否为特殊方法(private、static等)
* @param obj 接口的子对象或代理对象
* @param method 方法
* @param args 参数
* @return 结果
*/
@SuppressWarnings("unchecked")
public static <T> T invoke(boolean isSpecial, Object obj, Method method, Object... args) {
Assert.notNull(method, "Method must be not null!");
final Class<?> declaringClass = method.getDeclaringClass();
final MethodHandles.Lookup lookup = lookup(declaringClass);
try {
MethodHandle handle = isSpecial ? lookup.unreflectSpecial(method, declaringClass) : lookup.unreflect(method);
if (null != obj) {
handle = handle.bindTo(obj);
}
return (T) handle.invokeWithArguments(args);
} catch (Throwable e) {
throw new UtilException(e);
}
}
use of cn.hutool.core.exceptions.UtilException in project hutool by looly.
the class IoUtil method readObj.
/**
* 从流中读取内容,读到输出流中
*
* @param <T> 读取对象的类型
* @param in 输入流
* @return 输出流
* @throws IORuntimeException IO异常
* @throws UtilException ClassNotFoundException包装
*/
public static <T> T readObj(InputStream in) throws IORuntimeException, UtilException {
if (in == null) {
throw new IllegalArgumentException("The InputStream must not be null");
}
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(in);
// may fail with CCE if serialised form is incorrect
@SuppressWarnings("unchecked") final T obj = (T) ois.readObject();
return obj;
} catch (IOException e) {
throw new IORuntimeException(e);
} catch (ClassNotFoundException e) {
throw new UtilException(e);
}
}
use of cn.hutool.core.exceptions.UtilException in project hutool by looly.
the class NetUtil method localIpv4s.
/**
* 获得本机的IP地址列表<br>
* 返回的IP列表有序,按照系统设备顺序
*
* @return IP地址列表 {@link LinkedHashSet}
*/
public static LinkedHashSet<String> localIpv4s() {
Enumeration<NetworkInterface> networkInterfaces = null;
try {
networkInterfaces = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
throw new UtilException(e);
}
if (networkInterfaces == null) {
throw new UtilException("Get network interface error!");
}
final LinkedHashSet<String> ipSet = new LinkedHashSet<>();
while (networkInterfaces.hasMoreElements()) {
final NetworkInterface networkInterface = networkInterfaces.nextElement();
final Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
while (inetAddresses.hasMoreElements()) {
final InetAddress inetAddress = inetAddresses.nextElement();
if (inetAddress != null && inetAddress instanceof Inet4Address) {
ipSet.add(inetAddress.getHostAddress());
}
}
}
return ipSet;
}
Aggregations