util.java
来自「JGRoups源码」· Java 代码 · 共 2,058 行 · 第 1/5 页
JAVA
2,058 行
tok=new StringTokenizer(s, ","); while(tok.hasMoreTokens()) { l=new Long(tok.nextToken()); v.addElement(l); } if(v.size() == 0) return null; retval=new long[v.size()]; for(int i=0; i < v.size(); i++) retval[i]=((Long)v.elementAt(i)).longValue(); return retval; } /** e.g. "bela,jeannette,michelle" --> List{"bela", "jeannette", "michelle"} */ public static java.util.List parseCommaDelimitedStrings(String l) { return parseStringList(l, ","); } public static List parseStringList(String l, String separator) { List tmp=new LinkedList(); StringTokenizer tok=new StringTokenizer(l, separator); String t; while(tok.hasMoreTokens()) { t=tok.nextToken(); tmp.add(t.trim()); } return tmp; } public static int parseInt(Properties props,String property,int defaultValue) { int result = defaultValue; String str=props.getProperty(property); if(str != null) { result=Integer.parseInt(str); props.remove(property); } return result; } public static long parseLong(Properties props,String property,long defaultValue) { long result = defaultValue; String str=props.getProperty(property); if(str != null) { result=Integer.parseInt(str); props.remove(property); } return result; } public static boolean parseBoolean(Properties props,String property,boolean defaultValue) { boolean result = defaultValue; String str=props.getProperty(property); if(str != null) { result=str.equalsIgnoreCase("true"); props.remove(property); } return result; } public static InetAddress parseBindAddress(Properties props, String property) throws UnknownHostException { InetAddress bind_addr=null; boolean ignore_systemprops=Util.isBindAddressPropertyIgnored(); String str=Util.getProperty(new String[]{Global.BIND_ADDR, Global.BIND_ADDR_OLD}, props, "bind_addr", ignore_systemprops, null); if(str != null) { bind_addr=InetAddress.getByName(str); props.remove(property); } return bind_addr; } public static String shortName(String hostname) { int index; StringBuffer sb=new StringBuffer(); if(hostname == null) return null; index=hostname.indexOf('.'); if(index > 0 && !Character.isDigit(hostname.charAt(0))) sb.append(hostname.substring(0, index)); else sb.append(hostname); return sb.toString(); } public static String shortName(InetAddress hostname) { if(hostname == null) return null; StringBuffer sb=new StringBuffer(); if(resolve_dns) sb.append(hostname.getHostName()); else sb.append(hostname.getHostAddress()); return sb.toString(); } /** Finds first available port starting at start_port and returns server socket */ public static ServerSocket createServerSocket(int start_port) { ServerSocket ret=null; while(true) { try { ret=new ServerSocket(start_port); } catch(BindException bind_ex) { start_port++; continue; } catch(IOException io_ex) { } break; } return ret; } public static ServerSocket createServerSocket(InetAddress bind_addr, int start_port) { ServerSocket ret=null; while(true) { try { ret=new ServerSocket(start_port, 50, bind_addr); } catch(BindException bind_ex) { start_port++; continue; } catch(IOException io_ex) { } break; } return ret; } /** * Creates a DatagramSocket bound to addr. If addr is null, socket won't be bound. If address is already in use, * start_port will be incremented until a socket can be created. * @param addr The InetAddress to which the socket should be bound. If null, the socket will not be bound. * @param port The port which the socket should use. If 0, a random port will be used. If > 0, but port is already * in use, it will be incremented until an unused port is found, or until MAX_PORT is reached. */ public static DatagramSocket createDatagramSocket(InetAddress addr, int port) throws Exception { DatagramSocket sock=null; if(addr == null) { if(port == 0) { return new DatagramSocket(); } else { while(port < MAX_PORT) { try { return new DatagramSocket(port); } catch(BindException bind_ex) { // port already used port++; } catch(Exception ex) { throw ex; } } } } else { if(port == 0) port=1024; while(port < MAX_PORT) { try { return new DatagramSocket(port, addr); } catch(BindException bind_ex) { // port already used port++; } catch(Exception ex) { throw ex; } } } return sock; // will never be reached, but the stupid compiler didn't figure it out... } public static boolean checkForLinux() { String os=System.getProperty("os.name"); return os != null && os.toLowerCase().startsWith("linux"); } public static boolean checkForSolaris() { String os=System.getProperty("os.name"); return os != null && os.toLowerCase().startsWith("sun"); } public static boolean checkForWindows() { String os=System.getProperty("os.name"); return os != null && os.toLowerCase().startsWith("win"); } public static void prompt(String s) { System.out.println(s); System.out.flush(); try { while(System.in.available() > 0) System.in.read(); System.in.read(); } catch(IOException e) { e.printStackTrace(); } } public static int getJavaVersion() { String version=System.getProperty("java.version"); int retval=0; if(version != null) { if(version.startsWith("1.2")) return 12; if(version.startsWith("1.3")) return 13; if(version.startsWith("1.4")) return 14; if(version.startsWith("1.5")) return 15; if(version.startsWith("5")) return 15; if(version.startsWith("1.6")) return 16; if(version.startsWith("6")) return 16; } return retval; } public static String memStats(boolean gc) { StringBuffer sb=new StringBuffer(); Runtime rt=Runtime.getRuntime(); if(gc) rt.gc(); long free_mem, total_mem, used_mem; free_mem=rt.freeMemory(); total_mem=rt.totalMemory(); used_mem=total_mem - free_mem; sb.append("Free mem: ").append(free_mem).append("\nUsed mem: ").append(used_mem); sb.append("\nTotal mem: ").append(total_mem); return sb.toString(); }// public static InetAddress getFirstNonLoopbackAddress() throws SocketException {// Enumeration en=NetworkInterface.getNetworkInterfaces();// while(en.hasMoreElements()) {// NetworkInterface i=(NetworkInterface)en.nextElement();// for(Enumeration en2=i.getInetAddresses(); en2.hasMoreElements();) {// InetAddress addr=(InetAddress)en2.nextElement();// if(!addr.isLoopbackAddress())// return addr;// }// }// return null;// } public static InetAddress getFirstNonLoopbackAddress() throws SocketException { Enumeration en=NetworkInterface.getNetworkInterfaces(); boolean preferIpv4=Boolean.getBoolean("java.net.preferIPv4Stack"); boolean preferIPv6=Boolean.getBoolean("java.net.preferIPv6Addresses"); while(en.hasMoreElements()) { NetworkInterface i=(NetworkInterface)en.nextElement(); for(Enumeration en2=i.getInetAddresses(); en2.hasMoreElements();) { InetAddress addr=(InetAddress)en2.nextElement(); if(!addr.isLoopbackAddress()) { if(addr instanceof Inet4Address) { if(preferIPv6) continue; return addr; } if(addr instanceof Inet6Address) { if(preferIpv4) continue; return addr; } } } } return null; } public static InetAddress getFirstNonLoopbackIPv6Address() throws SocketException { Enumeration en=NetworkInterface.getNetworkInterfaces(); boolean preferIpv4=false; boolean preferIPv6=true; while(en.hasMoreElements()) { NetworkInterface i=(NetworkInterface)en.nextElement(); for(Enumeration en2=i.getInetAddresses(); en2.hasMoreElements();) { InetAddress addr=(InetAddress)en2.nextElement(); if(!addr.isLoopbackAddress()) { if(addr instanceof Inet4Address) { if(preferIPv6) continue; return addr; } if(addr instanceof Inet6Address) { if(preferIpv4) continue; return addr; } } } } return null; } public static List getAllAvailableInterfaces() throws SocketException { List retval=new ArrayList(10); NetworkInterface intf; for(Enumeration en=NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { intf=(NetworkInterface)en.nextElement(); retval.add(intf); } return retval; } /** * Returns a value associated wither with one or more system properties, or found in the props map * @param system_props * @param props List of properties read from the configuration file * @param prop_name The name of the property, will be removed from props if found * @param ignore_sysprops If true, system properties are not used and the values will only be retrieved from * props (not system_props) * @param default_value Used to return a default value if the properties or system properties didn't have the value * @return The value, or null if not found */ public static String getProperty(String[] system_props, Properties props, String prop_name, boolean ignore_sysprops, String default_value) { String retval=null; if(props != null && prop_name != null) { retval=props.getProperty(prop_name); props.remove(prop_name); } if(!ignore_sysprops) { String tmp, prop; if(system_props != null) { for(int i=0; i < system_props.length; i++) { prop=system_props[i]; if(prop != null) { try { tmp=System.getProperty(prop); if(tmp != null) return tmp; // system properties override config file definitions } catch(SecurityException ex) {} } } } } if(retval == null) return default_value; return retval; } public static boolean isBindAddressPropertyIgnored() { try { String tmp=System.getProperty(Global.IGNORE_BIND_ADDRESS_PROPERTY); if(tmp == null) { tmp=System.getProperty(Global.IGNORE_BIND_ADDRESS_PROPERTY_OLD); if(tmp == null) return false; } tmp=tmp.trim().toLowerCase(); if(tmp.equals("false") || tmp.equals("no") || tmp.equals("off")) return false; else return tmp.equals("true") || tmp.equals("yes") || tmp.equals("on"); } catch(SecurityException ex) { return false; } } public static MBeanServer getMBeanServer() { ArrayList servers=MBeanServerFactory.findMBeanServer(null); if(servers == null || servers.size() == 0) return null; // return 'jboss' server if available for(int i=0; i < servers.size(); i++) { MBeanServer srv=(MBeanServer)servers.get(i); if("jboss".equalsIgnoreCase(srv.getDefaultDomain())) return srv; } // return first available server return (MBeanServer)servers.get(0); } /* public
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?