Search in sources :

Example 21 with UtilException

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;
}
Also used : UtilException(cn.hutool.core.exceptions.UtilException) SOAPException(javax.xml.soap.SOAPException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 22 with UtilException

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);
    }
}
Also used : UtilException(cn.hutool.core.exceptions.UtilException) Method(java.lang.reflect.Method) IOException(java.io.IOException) File(java.io.File)

Example 23 with UtilException

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);
    }
}
Also used : MethodHandles(java.lang.invoke.MethodHandles) UtilException(cn.hutool.core.exceptions.UtilException) MethodHandle(java.lang.invoke.MethodHandle)

Example 24 with UtilException

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);
    }
}
Also used : UtilException(cn.hutool.core.exceptions.UtilException) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Example 25 with UtilException

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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SocketException(java.net.SocketException) Inet4Address(java.net.Inet4Address) UtilException(cn.hutool.core.exceptions.UtilException) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

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