📄 util.java
字号:
host = u.getHost(); if (host == null) { int i = us.indexOf(Protocol.URI_DELIMITER); int j = us.lastIndexOf(COLON); if (i > -1 && j > i && i + Protocol.URI_DELIMITER.length() != j) { host = us.substring(i + Protocol.URI_DELIMITER.length(), j); } else { host = IPUtils.ANYADDRESS.getHostAddress(); } } } else { host = IPUtils.ANYADDRESS.getHostAddress(); } if (host.indexOf(COLON) > -1) { host = BRACKET_OPEN + host + BRACKET_CLOSE; } int port = u != null ? u.getPort() : -1; if (port == -1) { int i = us.indexOf(Protocol.URI_DELIMITER); int j = us.lastIndexOf(COLON); if (j > -1 && j > i) { try { port = Integer.valueOf(us.substring(j + COLON.length()).trim()).intValue(); } catch (NumberFormatException nfe) { if (LOG.isEnabledFor(Level.ERROR)) { LOG.error("invalid port: " + us.substring(j + COLON.length()).trim()); } } } } u = makeAddress(protocol, host, port != -1 ? port : getProtocolPort(us)); } } return u; } public static URI makeAddress(String protocol, String addr, int port) { URI u = null; if (addr != null && addr.trim().length() > 0) { try { u = new URI( protocol + addr.trim() + COLON + port); } catch (URISyntaxException use) { if (LOG.isEnabledFor(Level.ERROR)) { LOG.error("invalid tcp address", use); } } } else { try { u = new URI(protocol + Util.getLocalHost() + COLON + port); } catch (URISyntaxException use) { if (LOG.isEnabledFor(Level.ERROR)) { LOG.error("invalid tcp address", use); } } } return u; } /** * {@link java.net.URI} factory. * * @param scheme scheme specification * @param host host specification * @param port port specification * @return newly constructed {@link java.net.URI} */ public static URI toURI(String scheme, String host, int port) { URI u = null; try { u = new URI(scheme, EMPTY_STRING, host == null || host.trim().length() == 0 ? getLocalHost() : host, port, EMPTY_STRING, EMPTY_STRING, EMPTY_STRING); } catch (URISyntaxException use) { if (LOG.isEnabledFor(Level.ERROR)) { LOG.error("invalid transformation", use); } } return u; } /** * {@link java.net.URI} factory. * * @param scheme scheme specification * @return newly constructed {@link java.net.URI} */ public static URI model(String scheme) { return model(scheme, Default.INVALID_PORT); } /** * {@link java.net.URI} factory. * * @param scheme scheme specification * @param port port specification * @return newly constructed {@link java.net.URI} */ public static URI model(String scheme, int port) { URI u = null; if (Protocol.TCP.equalsIgnoreCase(scheme)) { u = toURI(Default.ANY_TCP_ADDRESS.getScheme(), null, port != Default.INVALID_PORT ? port : Default.TCP_PORT); } else if (Protocol.HTTP.equalsIgnoreCase(scheme)) { u = toURI(Default.ANY_HTTP_ADDRESS.getScheme(), null, port != Default.INVALID_PORT ? port : Default.HTTP_PORT); } else if (Protocol.UDP.equalsIgnoreCase(scheme)) { u = toURI(Default.ANY_UDP_ADDRESS.getScheme(), Default.MULTICAST_ADDRESS.getHost(), port != Default.INVALID_PORT ? port : Default.MULTICAST_PORT); } return u; } /** * Checks the specified port for availability. * * @param address specified address * @param port specified port * @return port availablity results */ public static boolean isPortAvailable(InetAddress address, int port) { ServerSocket ss = getServerSocket(address, port); boolean isAvailable = ss != null; if (ss != null) { try { ss.close(); } catch (IOException ioe) {} } ss = null; return isAvailable; } /** * {@link java.net.ServerSocket} factory. * * @param address specified address * @param port specified port * @return newly created {@link java.net.ServerSocket} */ public static ServerSocket getServerSocket(InetAddress address, int port) { ServerSocket ss = null; try { ss = new ServerSocket(port, 0, address != null && ! address.getHostAddress().equals(Env.ALL_ADDRESSES.getHostAddress()) ? address : null); } catch (BindException be) {} catch (IOException ioe) {} return ss; } /** * Accessor for user-agent proxy. * * @return user-agent proxy */ public static String getProxyFromUserAgent() { String p = null; return p; } /** * Address non-routable validator. * * @param u specified address * @return address non-routability indicator */ public static boolean isNonRoutable(URI u) { boolean isNonRoutable = false; String a = u != null ? u.getHost() : null; if (a != null) { for (Iterator r = Env.NON_ROUTABLE_ADDRESSES.iterator(); ! isNonRoutable && r.hasNext(); ) { if (a.startsWith((String)r.next())) { isNonRoutable = true; } } } return isNonRoutable; } /** * Address multicast validator. * * @param u specified address * @return address multicast indicator */ public static boolean isMulticast(URI u) { String h = u != null ? u.getHost() : null; InetAddress ia = null; if (h != null) { try { ia = InetAddress.getByAddress(h, inetAddressToBytes(h)); } catch (UnknownHostException uhe) { if (LOG.isEnabledFor(Level.ERROR)) { LOG.error("invalid address", uhe); } } } return ia != null ? ia.isMulticastAddress() : false; } /** * Address to byte array converter. * * @param ipAddr specified address * @return byte array address equivalent */ public static byte[] inetAddressToBytes(String ipAddr) { byte[] bytes = new byte[4]; int bit = 0; for(int i = 0; i < ipAddr.length(); i++) { char c = ipAddr.charAt(i); switch(c) { case '.': bit++; break; default: bytes[bit] = (byte)(bytes[bit]*10 + Character.digit(c, 10)); break; } } return bytes; } /** * Retrieves the contents of the provided {@link java.net.URI} bootstrap * resource in the form of a {@link java.util.List} of {@link java.util.URI}. * * @param u bootstrap resource address * @return list of addresses * @throws IOException can't establish a connection */ public static List fetchBootstrapAddresses(URI u) throws IOException { List r = null; URL url = null; if (u != null) { try { url = u.toURL(); } catch (MalformedURLException mue) { if (LOG.isEnabledFor(Level.ERROR)) { LOG.error("invalid location", mue); } } } if (url != null) { Message results = null; try { results = new Dispatcher(url, MAX_WAIT).dispatch(); } catch (IOException ioe) {} String s = (results != null ? results.getBody() : null); if (s != null) { r = new ArrayList(); StreamTokenizer st = new StreamTokenizer(new StringReader(s)); st.wordChars(' ', '~'); st.eolIsSignificant(true); try { while (st.nextToken() != StreamTokenizer.TT_EOF) { switch (st.ttype) { case StreamTokenizer.TT_WORD: try { r.add(new URI(st.sval.trim())); } catch (URISyntaxException use) { if (LOG.isEnabledFor(Level.ERROR)) { LOG.error("invalid uri", use); } } break; default: } } } catch (IOException ioe) { if (LOG.isEnabledFor(Level.ERROR)) { LOG.error("invalid uri tokenizer", ioe); } } } } return r != null ? r : Collections.EMPTY_LIST; } private static int getNextAvailablePort(Address a, int defaultPort) { URI u = a != null ? a.getAddress() : null; String h = u != null ? u.getHost() : null; int p = u != null ? u.getPort() : Default.INVALID_PORT; int port = p != Default.INVALID_PORT ? p : defaultPort; int pr = a != null ? a.getPortRange() : Default.PORT_RANGE; InetAddress ia = null; if (h != null) { try { ia = InetAddress.getByAddress(h, inetAddressToBytes(h)); } catch (UnknownHostException uhe) { if (LOG.isEnabledFor(Level.ERROR)) { LOG.error("invalid address", uhe); } } if (ia != null && p != Default.INVALID_PORT) { for (int i = p; i <= p + pr; i++) { if (isPortAvailable(ia, i)) { port = i; break; } } } } return port; } private static String getProtocolURI(String s) { String p = null; s = s != null ? s.trim().toLowerCase() : null; if (s != null && s.length() > 0) { if (Protocol.TCP_URI.toLowerCase().indexOf(s) > -1) { p = Protocol.TCP_URI; } else if (Protocol.HTTP_URI.toLowerCase().indexOf(s) > -1) { p = Protocol.HTTP_URI; } else if (Protocol.UDP_URI.toLowerCase().indexOf(s) > -1) { p = Protocol.UDP_URI; } } return p; } private static int getProtocolPort(String s) { int p = -1; s = s != null ? s.trim().toLowerCase() : null; if (s != null && s.length() > 0) { if (Protocol.TCP_URI.toLowerCase().indexOf(s) > -1) { p = Default.TCP_PORT; } else if (Protocol.HTTP_URI.toLowerCase().indexOf(s) > -1) { p = Default.HTTP_PORT; } } return p; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -