Path: blob/master/src/java.base/share/classes/sun/net/util/SocketExceptions.java
41159 views
/*1* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.net.util;2627import java.io.IOException;28import java.lang.reflect.Constructor;29import java.net.InetSocketAddress;30import java.net.UnixDomainSocketAddress;31import java.net.SocketAddress;32import java.security.AccessController;33import java.security.PrivilegedAction;3435import sun.security.util.SecurityProperties;3637public final class SocketExceptions {38private SocketExceptions() {}3940private static final boolean enhancedExceptionText =41SecurityProperties.includedInExceptions("hostInfo");4243/**44* Utility which takes an exception and returns either the same exception45* or a new exception of the same type with the same stack trace46* and detail message enhanced with addressing information from the47* given InetSocketAddress.48*49* If the system/security property "jdk.includeInExceptions" is not50* set or does not contain the category hostInfo,51* then the original exception is returned.52*53* Only specific IOException subtypes are supported.54*/55public static IOException of(IOException e, SocketAddress addr) {56if (!enhancedExceptionText || addr == null) {57return e;58}59if (addr instanceof UnixDomainSocketAddress) {60return ofUnixDomain(e, (UnixDomainSocketAddress)addr);61} else if (addr instanceof InetSocketAddress) {62return ofInet(e, (InetSocketAddress)addr);63} else {64return e;65}66}6768private static IOException ofInet(IOException e, InetSocketAddress addr) {69int port = addr.getPort();70String host = addr.getHostString();71StringBuilder sb = new StringBuilder();72sb.append(e.getMessage());73sb.append(": ");74sb.append(host);75sb.append(':');76sb.append(Integer.toString(port));77String enhancedMsg = sb.toString();78return create(e, enhancedMsg);79}8081private static IOException ofUnixDomain(IOException e, UnixDomainSocketAddress addr) {82String path = addr.getPath().toString();83StringBuilder sb = new StringBuilder();84sb.append(e.getMessage());85sb.append(": ");86sb.append(path);87String enhancedMsg = sb.toString();88return create(e, enhancedMsg);89}9091// return a new instance of the same type with the given detail92// msg, or if the type doesn't support detail msgs, return given93// instance.9495@SuppressWarnings("removal")96private static IOException create(IOException e, String msg) {97return AccessController.doPrivileged(new PrivilegedAction<IOException>() {98public IOException run() {99try {100Class<?> clazz = e.getClass();101Constructor<?> ctor = clazz.getConstructor(String.class);102IOException e1 = (IOException)(ctor.newInstance(msg));103e1.setStackTrace(e.getStackTrace());104return e1;105} catch (Exception e0) {106// Some eg AsynchronousCloseException have no detail msg107return e;108}109}110});111}112}113114115