code
stringlengths
23
201k
docstring
stringlengths
17
96.2k
func_name
stringlengths
0
235
language
stringclasses
1 value
repo
stringlengths
8
72
path
stringlengths
11
317
url
stringlengths
57
377
license
stringclasses
7 values
public static IOException close(final ZipFile zip) { try { if (zip != null) { zip.close(); } } catch (final IOException ioe) { return ioe; } return null; }
@return a byte[] containing the information contained in the specified InputStream. @throws java.io.IOException
close
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/IOUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/IOUtils.java
Apache-2.0
public static boolean isSubFile(File parent, File child) throws IOException { return child.getCanonicalPath().startsWith(parent.getCanonicalPath() + File.separator); }
@return a byte[] containing the information contained in the specified InputStream. @throws java.io.IOException
isSubFile
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/IOUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/IOUtils.java
Apache-2.0
public static boolean isSubFile(String parent, String child) throws IOException { return isSubFile(new File(parent), new File(child)); }
@return a byte[] containing the information contained in the specified InputStream. @throws java.io.IOException
isSubFile
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/IOUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/IOUtils.java
Apache-2.0
public static void unzip(String zipFile, String extractFolder) throws IOException { File file = new File(zipFile); ZipFile zip = null; try { int BUFFER = 1024 * 8; zip = new ZipFile(file); File newPath = new File(extractFolder); newPath.mkdirs(); Enumeration<? extends ZipEntry> zipFileEntries = zip.entries(); // Process each entry while (zipFileEntries.hasMoreElements()) { // grab a zip file entry ZipEntry entry = (ZipEntry) zipFileEntries.nextElement(); String currentEntry = entry.getName(); File destFile = new File(newPath, currentEntry); if (!isSubFile(newPath, destFile)) { throw new IOException("Bad zip entry: " + currentEntry); } // destFile = new File(newPath, destFile.getName()); File destinationParent = destFile.getParentFile(); // create the parent directory structure if needed destinationParent.mkdirs(); if (!entry.isDirectory()) { BufferedInputStream is = null; BufferedOutputStream dest = null; try { is = new BufferedInputStream(zip.getInputStream(entry)); int currentByte; // establish buffer for writing file byte data[] = new byte[BUFFER]; // write the current file to disk FileOutputStream fos = new FileOutputStream(destFile); dest = new BufferedOutputStream(fos, BUFFER); // read and write until last byte is encountered while ((currentByte = is.read(data, 0, BUFFER)) != -1) { dest.write(data, 0, currentByte); } dest.flush(); } finally { close(dest); close(is); } } } } finally { close(zip); } }
@return a byte[] containing the information contained in the specified InputStream. @throws java.io.IOException
unzip
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/IOUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/IOUtils.java
Apache-2.0
public Object run() throws Exception { try { return MethodHandles.class.getMethod("privateLookupIn", Class.class, MethodHandles.Lookup.class); } catch (NoSuchMethodException ex) { return null; } }
from spring @version $Id: ReflectUtils.java,v 1.30 2009/01/11 19:47:49 herbyderby Exp $
run
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
Apache-2.0
public Object run() throws Exception { try { return MethodHandles.Lookup.class.getMethod("defineClass", byte[].class); } catch (NoSuchMethodException ex) { return null; } }
from spring @version $Id: ReflectUtils.java,v 1.30 2009/01/11 19:47:49 herbyderby Exp $
run
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
Apache-2.0
public Object run() throws Exception { return ClassLoader.class.getDeclaredMethod("defineClass", String.class, byte[].class, Integer.TYPE, Integer.TYPE, ProtectionDomain.class); }
from spring @version $Id: ReflectUtils.java,v 1.30 2009/01/11 19:47:49 herbyderby Exp $
run
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
Apache-2.0
public Object run() throws Exception { Method[] methods = Object.class.getDeclaredMethods(); for (Method method : methods) { if ("finalize".equals(method.getName()) || (method.getModifiers() & (Modifier.FINAL | Modifier.STATIC)) > 0) { continue; } OBJECT_METHODS.add(method); } return null; }
from spring @version $Id: ReflectUtils.java,v 1.30 2009/01/11 19:47:49 herbyderby Exp $
run
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
Apache-2.0
public static ProtectionDomain getProtectionDomain(final Class source) { if (source == null) { return null; } return (ProtectionDomain) AccessController.doPrivileged(new PrivilegedAction() { public Object run() { return source.getProtectionDomain(); } }); }
from spring @version $Id: ReflectUtils.java,v 1.30 2009/01/11 19:47:49 herbyderby Exp $
getProtectionDomain
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
Apache-2.0
public Object run() { return source.getProtectionDomain(); }
from spring @version $Id: ReflectUtils.java,v 1.30 2009/01/11 19:47:49 herbyderby Exp $
run
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
Apache-2.0
public static Constructor findConstructor(String desc) { return findConstructor(desc, defaultLoader); }
from spring @version $Id: ReflectUtils.java,v 1.30 2009/01/11 19:47:49 herbyderby Exp $
findConstructor
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
Apache-2.0
public static Constructor findConstructor(String desc, ClassLoader loader) { try { int lparen = desc.indexOf('('); String className = desc.substring(0, lparen).trim(); return getClass(className, loader).getConstructor(parseTypes(desc, loader)); } catch (ClassNotFoundException ex) { throw new ReflectException(ex); } catch (NoSuchMethodException ex) { throw new ReflectException(ex); } }
from spring @version $Id: ReflectUtils.java,v 1.30 2009/01/11 19:47:49 herbyderby Exp $
findConstructor
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
Apache-2.0
public static Method findMethod(String desc) { return findMethod(desc, defaultLoader); }
from spring @version $Id: ReflectUtils.java,v 1.30 2009/01/11 19:47:49 herbyderby Exp $
findMethod
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
Apache-2.0
public static Method findMethod(String desc, ClassLoader loader) { try { int lparen = desc.indexOf('('); int dot = desc.lastIndexOf('.', lparen); String className = desc.substring(0, dot).trim(); String methodName = desc.substring(dot + 1, lparen).trim(); return getClass(className, loader).getDeclaredMethod(methodName, parseTypes(desc, loader)); } catch (ClassNotFoundException ex) { throw new ReflectException(ex); } catch (NoSuchMethodException ex) { throw new ReflectException(ex); } }
from spring @version $Id: ReflectUtils.java,v 1.30 2009/01/11 19:47:49 herbyderby Exp $
findMethod
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
Apache-2.0
private static Class[] parseTypes(String desc, ClassLoader loader) throws ClassNotFoundException { int lparen = desc.indexOf('('); int rparen = desc.indexOf(')', lparen); List params = new ArrayList(); int start = lparen + 1; for (;;) { int comma = desc.indexOf(',', start); if (comma < 0) { break; } params.add(desc.substring(start, comma).trim()); start = comma + 1; } if (start < rparen) { params.add(desc.substring(start, rparen).trim()); } Class[] types = new Class[params.size()]; for (int i = 0; i < types.length; i++) { types[i] = getClass((String) params.get(i), loader); } return types; }
from spring @version $Id: ReflectUtils.java,v 1.30 2009/01/11 19:47:49 herbyderby Exp $
parseTypes
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
Apache-2.0
private static Class getClass(String className, ClassLoader loader) throws ClassNotFoundException { return getClass(className, loader, CGLIB_PACKAGES); }
from spring @version $Id: ReflectUtils.java,v 1.30 2009/01/11 19:47:49 herbyderby Exp $
getClass
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
Apache-2.0
private static Class getClass(String className, ClassLoader loader, String[] packages) throws ClassNotFoundException { String save = className; int dimensions = 0; int index = 0; while ((index = className.indexOf("[]", index) + 1) > 0) { dimensions++; } StringBuilder brackets = new StringBuilder(className.length() - dimensions); for (int i = 0; i < dimensions; i++) { brackets.append('['); } className = className.substring(0, className.length() - 2 * dimensions); String prefix = (dimensions > 0) ? brackets + "L" : ""; String suffix = (dimensions > 0) ? ";" : ""; try { return Class.forName(prefix + className + suffix, false, loader); } catch (ClassNotFoundException ignore) { } for (int i = 0; i < packages.length; i++) { try { return Class.forName(prefix + packages[i] + '.' + className + suffix, false, loader); } catch (ClassNotFoundException ignore) { } } if (dimensions == 0) { Class c = (Class) primitives.get(className); if (c != null) { return c; } } else { String transform = (String) transforms.get(className); if (transform != null) { try { return Class.forName(brackets + transform, false, loader); } catch (ClassNotFoundException ignore) { } } } throw new ClassNotFoundException(save); }
from spring @version $Id: ReflectUtils.java,v 1.30 2009/01/11 19:47:49 herbyderby Exp $
getClass
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
Apache-2.0
public static Object newInstance(Class type) { return newInstance(type, EMPTY_CLASS_ARRAY, null); }
from spring @version $Id: ReflectUtils.java,v 1.30 2009/01/11 19:47:49 herbyderby Exp $
newInstance
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
Apache-2.0
public static Object newInstance(Class type, Class[] parameterTypes, Object[] args) { return newInstance(getConstructor(type, parameterTypes), args); }
from spring @version $Id: ReflectUtils.java,v 1.30 2009/01/11 19:47:49 herbyderby Exp $
newInstance
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
Apache-2.0
public static Object newInstance(final Constructor cstruct, final Object[] args) { boolean flag = cstruct.isAccessible(); try { if (!flag) { cstruct.setAccessible(true); } Object result = cstruct.newInstance(args); return result; } catch (InstantiationException e) { throw new ReflectException(e); } catch (IllegalAccessException e) { throw new ReflectException(e); } catch (InvocationTargetException e) { throw new ReflectException(e.getTargetException()); } finally { if (!flag) { cstruct.setAccessible(flag); } } }
from spring @version $Id: ReflectUtils.java,v 1.30 2009/01/11 19:47:49 herbyderby Exp $
newInstance
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
Apache-2.0
public static Constructor getConstructor(Class type, Class[] parameterTypes) { try { Constructor constructor = type.getDeclaredConstructor(parameterTypes); constructor.setAccessible(true); return constructor; } catch (NoSuchMethodException e) { throw new ReflectException(e); } }
from spring @version $Id: ReflectUtils.java,v 1.30 2009/01/11 19:47:49 herbyderby Exp $
getConstructor
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
Apache-2.0
public static String[] getNames(Class[] classes) { if (classes == null) return null; String[] names = new String[classes.length]; for (int i = 0; i < names.length; i++) { names[i] = classes[i].getName(); } return names; }
from spring @version $Id: ReflectUtils.java,v 1.30 2009/01/11 19:47:49 herbyderby Exp $
getNames
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
Apache-2.0
public static Class[] getClasses(Object[] objects) { Class[] classes = new Class[objects.length]; for (int i = 0; i < objects.length; i++) { classes[i] = objects[i].getClass(); } return classes; }
from spring @version $Id: ReflectUtils.java,v 1.30 2009/01/11 19:47:49 herbyderby Exp $
getClasses
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
Apache-2.0
public static Method findNewInstance(Class iface) { Method m = findInterfaceMethod(iface); if (!m.getName().equals("newInstance")) { throw new IllegalArgumentException(iface + " missing newInstance method"); } return m; }
from spring @version $Id: ReflectUtils.java,v 1.30 2009/01/11 19:47:49 herbyderby Exp $
findNewInstance
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
Apache-2.0
public static Method[] getPropertyMethods(PropertyDescriptor[] properties, boolean read, boolean write) { Set methods = new HashSet(); for (int i = 0; i < properties.length; i++) { PropertyDescriptor pd = properties[i]; if (read) { methods.add(pd.getReadMethod()); } if (write) { methods.add(pd.getWriteMethod()); } } methods.remove(null); return (Method[]) methods.toArray(new Method[methods.size()]); }
from spring @version $Id: ReflectUtils.java,v 1.30 2009/01/11 19:47:49 herbyderby Exp $
getPropertyMethods
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
Apache-2.0
public static PropertyDescriptor[] getBeanProperties(Class type) { return getPropertiesHelper(type, true, true); }
from spring @version $Id: ReflectUtils.java,v 1.30 2009/01/11 19:47:49 herbyderby Exp $
getBeanProperties
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
Apache-2.0
public static PropertyDescriptor[] getBeanGetters(Class type) { return getPropertiesHelper(type, true, false); }
from spring @version $Id: ReflectUtils.java,v 1.30 2009/01/11 19:47:49 herbyderby Exp $
getBeanGetters
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
Apache-2.0
public static PropertyDescriptor[] getBeanSetters(Class type) { return getPropertiesHelper(type, false, true); }
from spring @version $Id: ReflectUtils.java,v 1.30 2009/01/11 19:47:49 herbyderby Exp $
getBeanSetters
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
Apache-2.0
private static PropertyDescriptor[] getPropertiesHelper(Class type, boolean read, boolean write) { try { BeanInfo info = Introspector.getBeanInfo(type, Object.class); PropertyDescriptor[] all = info.getPropertyDescriptors(); if (read && write) { return all; } List properties = new ArrayList(all.length); for (int i = 0; i < all.length; i++) { PropertyDescriptor pd = all[i]; if ((read && pd.getReadMethod() != null) || (write && pd.getWriteMethod() != null)) { properties.add(pd); } } return (PropertyDescriptor[]) properties.toArray(new PropertyDescriptor[properties.size()]); } catch (IntrospectionException e) { throw new ReflectException(e); } }
from spring @version $Id: ReflectUtils.java,v 1.30 2009/01/11 19:47:49 herbyderby Exp $
getPropertiesHelper
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
Apache-2.0
public static Method findDeclaredMethod(final Class type, final String methodName, final Class[] parameterTypes) throws NoSuchMethodException { Class cl = type; while (cl != null) { try { return cl.getDeclaredMethod(methodName, parameterTypes); } catch (NoSuchMethodException e) { cl = cl.getSuperclass(); } } throw new NoSuchMethodException(methodName); }
from spring @version $Id: ReflectUtils.java,v 1.30 2009/01/11 19:47:49 herbyderby Exp $
findDeclaredMethod
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
Apache-2.0
public static List addAllMethods(final Class type, final List list) { if (type == Object.class) { list.addAll(OBJECT_METHODS); } else list.addAll(java.util.Arrays.asList(type.getDeclaredMethods())); Class superclass = type.getSuperclass(); if (superclass != null) { addAllMethods(superclass, list); } Class[] interfaces = type.getInterfaces(); for (int i = 0; i < interfaces.length; i++) { addAllMethods(interfaces[i], list); } return list; }
from spring @version $Id: ReflectUtils.java,v 1.30 2009/01/11 19:47:49 herbyderby Exp $
addAllMethods
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
Apache-2.0
public static List addAllInterfaces(Class type, List list) { Class superclass = type.getSuperclass(); if (superclass != null) { list.addAll(Arrays.asList(type.getInterfaces())); addAllInterfaces(superclass, list); } return list; }
from spring @version $Id: ReflectUtils.java,v 1.30 2009/01/11 19:47:49 herbyderby Exp $
addAllInterfaces
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
Apache-2.0
public static Method findInterfaceMethod(Class iface) { if (!iface.isInterface()) { throw new IllegalArgumentException(iface + " is not an interface"); } Method[] methods = iface.getDeclaredMethods(); if (methods.length != 1) { throw new IllegalArgumentException("expecting exactly 1 method in " + iface); } return methods[0]; }
from spring @version $Id: ReflectUtils.java,v 1.30 2009/01/11 19:47:49 herbyderby Exp $
findInterfaceMethod
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
Apache-2.0
public static Class defineClass(String className, byte[] b, ClassLoader loader) throws Exception { return defineClass(className, b, loader, null, null); }
from spring @version $Id: ReflectUtils.java,v 1.30 2009/01/11 19:47:49 herbyderby Exp $
defineClass
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
Apache-2.0
public static Class defineClass(String className, byte[] b, ClassLoader loader, ProtectionDomain protectionDomain) throws Exception { return defineClass(className, b, loader, protectionDomain, null); }
from spring @version $Id: ReflectUtils.java,v 1.30 2009/01/11 19:47:49 herbyderby Exp $
defineClass
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
Apache-2.0
public static int findPackageProtected(Class[] classes) { for (int i = 0; i < classes.length; i++) { if (!Modifier.isPublic(classes[i].getModifiers())) { return i; } } return 0; }
from spring @version $Id: ReflectUtils.java,v 1.30 2009/01/11 19:47:49 herbyderby Exp $
findPackageProtected
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/ReflectUtils.java
Apache-2.0
public static long findTcpListenProcess(int port) { // Add a timeout of 5 seconds to prevent blocking final int TIMEOUT_SECONDS = 5; ExecutorService executor = Executors.newSingleThreadExecutor(); try { Future<Long> future = executor.submit(new Callable<Long>() { @Override public Long call() throws Exception { return doFindTcpListenProcess(port); } }); try { return future.get(TIMEOUT_SECONDS, TimeUnit.SECONDS); } catch (TimeoutException e) { future.cancel(true); return -1; } catch (Exception e) { return -1; } } finally { executor.shutdownNow(); } }
The default maximum value for port ranges used when finding an available socket port.
findTcpListenProcess
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/SocketUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/SocketUtils.java
Apache-2.0
@Override public Long call() throws Exception { return doFindTcpListenProcess(port); }
The default maximum value for port ranges used when finding an available socket port.
call
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/SocketUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/SocketUtils.java
Apache-2.0
private static long doFindTcpListenProcess(int port) { try { if (OSUtils.isWindows()) { return findTcpListenProcessOnWindows(port); } if (OSUtils.isLinux() || OSUtils.isMac()) { return findTcpListenProcessOnUnix(port); } } catch (Throwable e) { // ignore } return -1; }
The default maximum value for port ranges used when finding an available socket port.
doFindTcpListenProcess
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/SocketUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/SocketUtils.java
Apache-2.0
private static long findTcpListenProcessOnWindows(int port) { String[] command = { "netstat", "-ano", "-p", "TCP" }; List<String> lines = ExecutingCommand.runNative(command); for (String line : lines) { if (line.contains("LISTENING")) { // TCP 0.0.0.0:49168 0.0.0.0:0 LISTENING 476 String[] strings = line.trim().split("\\s+"); if (strings.length == 5) { if (strings[1].endsWith(":" + port)) { return Long.parseLong(strings[4]); } } } } return -1; }
The default maximum value for port ranges used when finding an available socket port.
findTcpListenProcessOnWindows
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/SocketUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/SocketUtils.java
Apache-2.0
private static long findTcpListenProcessOnUnix(int port) { String pid = ExecutingCommand.getFirstAnswer("lsof -t -s TCP:LISTEN -i TCP:" + port); if (pid != null && !pid.trim().isEmpty()) { try { return Long.parseLong(pid.trim()); } catch (NumberFormatException e) { // ignore } } return -1; }
The default maximum value for port ranges used when finding an available socket port.
findTcpListenProcessOnUnix
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/SocketUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/SocketUtils.java
Apache-2.0
public static boolean isTcpPortAvailable(int port) { try { ServerSocket serverSocket = ServerSocketFactory.getDefault().createServerSocket(port, 1, InetAddress.getByName("localhost")); serverSocket.close(); return true; } catch (Exception ex) { return false; } }
The default maximum value for port ranges used when finding an available socket port.
isTcpPortAvailable
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/SocketUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/SocketUtils.java
Apache-2.0
public static int findAvailableTcpPort() { return findAvailableTcpPort(PORT_RANGE_MIN); }
Find an available TCP port randomly selected from the range [{@value #PORT_RANGE_MIN}, {@value #PORT_RANGE_MAX}]. @return an available TCP port number @throws IllegalStateException if no available port could be found
findAvailableTcpPort
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/SocketUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/SocketUtils.java
Apache-2.0
public static int findAvailableTcpPort(int minPort) { return findAvailableTcpPort(minPort, PORT_RANGE_MAX); }
Find an available TCP port randomly selected from the range [{@code minPort}, {@value #PORT_RANGE_MAX}]. @param minPort the minimum port number @return an available TCP port number @throws IllegalStateException if no available port could be found
findAvailableTcpPort
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/SocketUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/SocketUtils.java
Apache-2.0
public static int findAvailableTcpPort(int minPort, int maxPort) { return findAvailablePort(minPort, maxPort); }
Find an available TCP port randomly selected from the range [{@code minPort}, {@code maxPort}]. @param minPort the minimum port number @param maxPort the maximum port number @return an available TCP port number @throws IllegalStateException if no available port could be found
findAvailableTcpPort
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/SocketUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/SocketUtils.java
Apache-2.0
private static int findAvailablePort(int minPort, int maxPort) { int portRange = maxPort - minPort; int candidatePort; int searchCounter = 0; do { if (searchCounter > portRange) { throw new IllegalStateException( String.format("Could not find an available tcp port in the range [%d, %d] after %d attempts", minPort, maxPort, searchCounter)); } candidatePort = findRandomPort(minPort, maxPort); searchCounter++; } while (!isTcpPortAvailable(candidatePort)); return candidatePort; }
Find an available port for this {@code SocketType}, randomly selected from the range [{@code minPort}, {@code maxPort}]. @param minPort the minimum port number @param maxPort the maximum port number @return an available port number for this socket type @throws IllegalStateException if no available port could be found
findAvailablePort
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/SocketUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/SocketUtils.java
Apache-2.0
private static int findRandomPort(int minPort, int maxPort) { int portRange = maxPort - minPort; return minPort + random.nextInt(portRange + 1); }
Find a pseudo-random port number within the range [{@code minPort}, {@code maxPort}]. @param minPort the minimum port number @param maxPort the maximum port number @return a random port number within the specified range
findRandomPort
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/SocketUtils.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/SocketUtils.java
Apache-2.0
private static int hash(int h) { // Spread bits to regularize both segment and index locations, // using variant of single-word Wang/Jenkins hash. h += h << 15 ^ 0xffffcd7d; h ^= h >>> 10; h += h << 3; h ^= h >>> 6; h += (h << 2) + (h << 14); return h ^ h >>> 16; }
Applies a supplemental hash function to a given hashCode, which defends against poor quality hash functions. This is critical because ConcurrentReferenceHashMap uses power-of-two length hash tables, that otherwise encounter collisions for hashCodes that do not differ in lower or upper bits.
hash
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
Segment<K, V> segmentFor(int hash) { return segments[hash >>> segmentShift & segmentMask]; }
Returns the segment that should be used for key with given hash. @param hash the hash code for the key @return the segment
segmentFor
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
private static int hashOf(Object key) { return hash(key.hashCode()); }
Returns the segment that should be used for key with given hash. @param hash the hash code for the key @return the segment
hashOf
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
public int keyHash() { return hash; }
A weak-key reference which stores the key hash needed for reclamation.
keyHash
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
public Object keyRef() { return this; }
A weak-key reference which stores the key hash needed for reclamation.
keyRef
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
@SuppressWarnings("unchecked") K key() { return ((Reference<K>) keyRef).get(); }
ConcurrentReferenceHashMap list entry. Note that this is never exported out as a user-visible Map.Entry. Because the value field is volatile, not final, it is legal wrt the Java Memory Model for an unsynchronized reader to see null instead of initial value when read via a data race. Although a reordering leading to this is not likely to ever actually occur, the Segment.readValueUnderLock method is used as a backup in case a null (pre-initialized) value is ever seen in an unsynchronized access method.
key
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
V value() { return dereferenceValue(valueRef); }
ConcurrentReferenceHashMap list entry. Note that this is never exported out as a user-visible Map.Entry. Because the value field is volatile, not final, it is legal wrt the Java Memory Model for an unsynchronized reader to see null instead of initial value when read via a data race. Although a reordering leading to this is not likely to ever actually occur, the Segment.readValueUnderLock method is used as a backup in case a null (pre-initialized) value is ever seen in an unsynchronized access method.
value
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
@SuppressWarnings("unchecked") V dereferenceValue(Object value) { if (value instanceof WeakKeyReference) { return ((Reference<V>) value).get(); } return (V) value; }
ConcurrentReferenceHashMap list entry. Note that this is never exported out as a user-visible Map.Entry. Because the value field is volatile, not final, it is legal wrt the Java Memory Model for an unsynchronized reader to see null instead of initial value when read via a data race. Although a reordering leading to this is not likely to ever actually occur, the Segment.readValueUnderLock method is used as a backup in case a null (pre-initialized) value is ever seen in an unsynchronized access method.
dereferenceValue
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
void setValue(V value) { valueRef = value; }
ConcurrentReferenceHashMap list entry. Note that this is never exported out as a user-visible Map.Entry. Because the value field is volatile, not final, it is legal wrt the Java Memory Model for an unsynchronized reader to see null instead of initial value when read via a data race. Although a reordering leading to this is not likely to ever actually occur, the Segment.readValueUnderLock method is used as a backup in case a null (pre-initialized) value is ever seen in an unsynchronized access method.
setValue
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
@SuppressWarnings("unchecked") static <K, V> HashEntry<K, V>[] newArray(int i) { return new HashEntry[i]; }
ConcurrentReferenceHashMap list entry. Note that this is never exported out as a user-visible Map.Entry. Because the value field is volatile, not final, it is legal wrt the Java Memory Model for an unsynchronized reader to see null instead of initial value when read via a data race. Although a reordering leading to this is not likely to ever actually occur, the Segment.readValueUnderLock method is used as a backup in case a null (pre-initialized) value is ever seen in an unsynchronized access method.
newArray
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
@SuppressWarnings("unchecked") static <K, V> Segment<K, V>[] newArray(int i) { return new Segment[i]; }
The collected weak-key reference queue for this segment. This should be (re)initialized whenever table is assigned,
newArray
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
private static boolean keyEq(Object src, Object dest) { return src.equals(dest); }
The collected weak-key reference queue for this segment. This should be (re)initialized whenever table is assigned,
keyEq
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
void setTable(HashEntry<K, V>[] newTable) { threshold = (int) (newTable.length * loadFactor); table = newTable; refQueue = new ReferenceQueue<Object>(); }
Sets table to new HashEntry array. Call only while holding lock or in constructor.
setTable
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
HashEntry<K, V> getFirst(int hash) { HashEntry<K, V>[] tab = table; return tab[hash & tab.length - 1]; }
Returns properly casted first entry of bin for given hash.
getFirst
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
HashEntry<K, V> newHashEntry( K key, int hash, HashEntry<K, V> next, V value) { return new HashEntry<K, V>( key, hash, next, value, refQueue); }
Returns properly casted first entry of bin for given hash.
newHashEntry
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
V readValueUnderLock(HashEntry<K, V> e) { lock(); try { removeStale(); return e.value(); } finally { unlock(); } }
Reads value field of an entry under lock. Called if value field ever appears to be null. This is possible only if a compiler happens to reorder a HashEntry initialization with its table assignment, which is legal under memory model but is not known to ever occur.
readValueUnderLock
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
V get(Object key, int hash) { if (count != 0) { // read-volatile HashEntry<K, V> e = getFirst(hash); while (e != null) { if (e.hash == hash && keyEq(key, e.key())) { Object opaque = e.valueRef; if (opaque != null) { return e.dereferenceValue(opaque); } return readValueUnderLock(e); // recheck } e = e.next; } } return null; }
Reads value field of an entry under lock. Called if value field ever appears to be null. This is possible only if a compiler happens to reorder a HashEntry initialization with its table assignment, which is legal under memory model but is not known to ever occur.
get
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
boolean containsKey(Object key, int hash) { if (count != 0) { // read-volatile HashEntry<K, V> e = getFirst(hash); while (e != null) { if (e.hash == hash && keyEq(key, e.key())) { return true; } e = e.next; } } return false; }
Reads value field of an entry under lock. Called if value field ever appears to be null. This is possible only if a compiler happens to reorder a HashEntry initialization with its table assignment, which is legal under memory model but is not known to ever occur.
containsKey
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
boolean containsValue(Object value) { if (count != 0) { // read-volatile for (HashEntry<K, V> e: table) { for (; e != null; e = e.next) { Object opaque = e.valueRef; V v; if (opaque == null) { v = readValueUnderLock(e); // recheck } else { v = e.dereferenceValue(opaque); } if (value.equals(v)) { return true; } } } } return false; }
Reads value field of an entry under lock. Called if value field ever appears to be null. This is possible only if a compiler happens to reorder a HashEntry initialization with its table assignment, which is legal under memory model but is not known to ever occur.
containsValue
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
boolean replace(K key, int hash, V oldValue, V newValue) { lock(); try { removeStale(); HashEntry<K, V> e = getFirst(hash); while (e != null && (e.hash != hash || !keyEq(key, e.key()))) { e = e.next; } boolean replaced = false; if (e != null && oldValue.equals(e.value())) { replaced = true; e.setValue(newValue); } return replaced; } finally { unlock(); } }
Reads value field of an entry under lock. Called if value field ever appears to be null. This is possible only if a compiler happens to reorder a HashEntry initialization with its table assignment, which is legal under memory model but is not known to ever occur.
replace
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
V replace(K key, int hash, V newValue) { lock(); try { removeStale(); HashEntry<K, V> e = getFirst(hash); while (e != null && (e.hash != hash || !keyEq(key, e.key()))) { e = e.next; } V oldValue = null; if (e != null) { oldValue = e.value(); e.setValue(newValue); } return oldValue; } finally { unlock(); } }
Reads value field of an entry under lock. Called if value field ever appears to be null. This is possible only if a compiler happens to reorder a HashEntry initialization with its table assignment, which is legal under memory model but is not known to ever occur.
replace
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
V put(K key, int hash, V value, boolean onlyIfAbsent) { lock(); try { removeStale(); int c = count; if (c ++ > threshold) { // ensure capacity int reduced = rehash(); if (reduced > 0) { count = (c -= reduced) - 1; // write-volatile } } HashEntry<K, V>[] tab = table; int index = hash & tab.length - 1; HashEntry<K, V> first = tab[index]; HashEntry<K, V> e = first; while (e != null && (e.hash != hash || !keyEq(key, e.key()))) { e = e.next; } V oldValue; if (e != null) { oldValue = e.value(); if (!onlyIfAbsent) { e.setValue(value); } } else { oldValue = null; ++ modCount; tab[index] = newHashEntry(key, hash, first, value); count = c; // write-volatile } return oldValue; } finally { unlock(); } }
Reads value field of an entry under lock. Called if value field ever appears to be null. This is possible only if a compiler happens to reorder a HashEntry initialization with its table assignment, which is legal under memory model but is not known to ever occur.
put
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
int rehash() { HashEntry<K, V>[] oldTable = table; int oldCapacity = oldTable.length; if (oldCapacity >= MAXIMUM_CAPACITY) { return 0; } /* * Reclassify nodes in each list to new Map. Because we are using * power-of-two expansion, the elements from each bin must either * stay at same index, or move with a power of two offset. We * eliminate unnecessary node creation by catching cases where old * nodes can be reused because their next fields won't change. * Statistically, at the default threshold, only about one-sixth of * them need cloning when a table doubles. The nodes they replace * will be garbage collectable as soon as they are no longer * referenced by any reader thread that may be in the midst of * traversing table right now. */ HashEntry<K, V>[] newTable = HashEntry.newArray(oldCapacity << 1); threshold = (int) (newTable.length * loadFactor); int sizeMask = newTable.length - 1; int reduce = 0; for (HashEntry<K, V> e: oldTable) { // We need to guarantee that any existing reads of old Map can // proceed. So we cannot yet null out each bin. if (e != null) { HashEntry<K, V> next = e.next; int idx = e.hash & sizeMask; // Single node on list if (next == null) { newTable[idx] = e; } else { // Reuse trailing consecutive sequence at same slot HashEntry<K, V> lastRun = e; int lastIdx = idx; for (HashEntry<K, V> last = next; last != null; last = last.next) { int k = last.hash & sizeMask; if (k != lastIdx) { lastIdx = k; lastRun = last; } } newTable[lastIdx] = lastRun; // Clone all remaining nodes for (HashEntry<K, V> p = e; p != lastRun; p = p.next) { // Skip GC'd weak references K key = p.key(); if (key == null) { reduce++; continue; } int k = p.hash & sizeMask; HashEntry<K, V> n = newTable[k]; newTable[k] = newHashEntry(key, p.hash, n, p.value()); } } } } table = newTable; return reduce; }
Reads value field of an entry under lock. Called if value field ever appears to be null. This is possible only if a compiler happens to reorder a HashEntry initialization with its table assignment, which is legal under memory model but is not known to ever occur.
rehash
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
V remove(Object key, int hash, Object value, boolean refRemove) { lock(); try { if (!refRemove) { removeStale(); } int c = count - 1; HashEntry<K, V>[] tab = table; int index = hash & tab.length - 1; HashEntry<K, V> first = tab[index]; HashEntry<K, V> e = first; // a reference remove operation compares the Reference instance while (e != null && key != e.keyRef && (refRemove || hash != e.hash || !keyEq(key, e.key()))) { e = e.next; } V oldValue = null; if (e != null) { V v = e.value(); if (value == null || value.equals(v)) { oldValue = v; // All entries following removed node can stay in list, // but all preceding ones need to be cloned. ++ modCount; HashEntry<K, V> newFirst = e.next; for (HashEntry<K, V> p = first; p != e; p = p.next) { K pKey = p.key(); if (pKey == null) { // Skip GC'd keys c --; continue; } newFirst = newHashEntry( pKey, p.hash, newFirst, p.value()); } tab[index] = newFirst; count = c; // write-volatile } } return oldValue; } finally { unlock(); } }
Remove; match on key only if value null, else match both.
remove
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
@SuppressWarnings("rawtypes") void removeStale() { WeakKeyReference ref; while ((ref = (WeakKeyReference) refQueue.poll()) != null) { remove(ref.keyRef(), ref.keyHash(), null, true); } }
Remove; match on key only if value null, else match both.
removeStale
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
void clear() { if (count != 0) { lock(); try { Arrays.fill(table, null); ++ modCount; // replace the reference queue to avoid unnecessary stale // cleanups refQueue = new ReferenceQueue<Object>(); count = 0; // write-volatile } finally { unlock(); } } }
Remove; match on key only if value null, else match both.
clear
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
@Override public boolean isEmpty() { final Segment<K, V>[] segments = this.segments; /* * We keep track of per-segment modCounts to avoid ABA problems in which * an element in one segment was added and in another removed during * traversal, in which case the table was never actually empty at any * point. Note the similar use of modCounts in the size() and * containsValue() methods, which are the only other methods also * susceptible to ABA problems. */ int[] mc = new int[segments.length]; int mcsum = 0; for (int i = 0; i < segments.length; ++ i) { if (segments[i].count != 0) { return false; } else { mcsum += mc[i] = segments[i].modCount; } } // If mcsum happens to be zero, then we know we got a snapshot before // any modifications at all were made. This is probably common enough // to bother tracking. if (mcsum != 0) { for (int i = 0; i < segments.length; ++ i) { if (segments[i].count != 0 || mc[i] != segments[i].modCount) { return false; } } } return true; }
Returns <tt>true</tt> if this map contains no key-value mappings. @return <tt>true</tt> if this map contains no key-value mappings
isEmpty
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
@Override public int size() { final Segment<K, V>[] segments = this.segments; long sum = 0; long check = 0; int[] mc = new int[segments.length]; // Try a few times to get accurate count. On failure due to continuous // async changes in table, resort to locking. for (int k = 0; k < RETRIES_BEFORE_LOCK; ++ k) { check = 0; sum = 0; int mcsum = 0; for (int i = 0; i < segments.length; ++ i) { sum += segments[i].count; mcsum += mc[i] = segments[i].modCount; } if (mcsum != 0) { for (int i = 0; i < segments.length; ++ i) { check += segments[i].count; if (mc[i] != segments[i].modCount) { check = -1; // force retry break; } } } if (check == sum) { break; } } if (check != sum) { // Resort to locking all segments sum = 0; for (Segment<K, V> segment: segments) { segment.lock(); } for (Segment<K, V> segment: segments) { sum += segment.count; } for (Segment<K, V> segment: segments) { segment.unlock(); } } if (sum > Integer.MAX_VALUE) { return Integer.MAX_VALUE; } else { return (int) sum; } }
Returns the number of key-value mappings in this map. If the map contains more than <tt>Integer.MAX_VALUE</tt> elements, returns <tt>Integer.MAX_VALUE</tt>. @return the number of key-value mappings in this map
size
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
@Override public V get(Object key) { int hash = hashOf(key); return segmentFor(hash).get(key, hash); }
Returns the value to which the specified key is mapped, or {@code null} if this map contains no mapping for the key. <p>More formally, if this map contains a mapping from a key {@code k} to a value {@code v} such that {@code key.equals(k)}, then this method returns {@code v}; otherwise it returns {@code null}. (There can be at most one such mapping.) @throws NullPointerException if the specified key is null
get
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
@Override public boolean containsKey(Object key) { int hash = hashOf(key); return segmentFor(hash).containsKey(key, hash); }
Tests if the specified object is a key in this table. @param key possible key @return <tt>true</tt> if and only if the specified object is a key in this table, as determined by the <tt>equals</tt> method; <tt>false</tt> otherwise. @throws NullPointerException if the specified key is null
containsKey
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
@Override public boolean containsValue(Object value) { if (value == null) { throw new NullPointerException(); } // See explanation of modCount use above final Segment<K, V>[] segments = this.segments; int[] mc = new int[segments.length]; // Try a few times without locking for (int k = 0; k < RETRIES_BEFORE_LOCK; ++ k) { int mcsum = 0; for (int i = 0; i < segments.length; ++ i) { mcsum += mc[i] = segments[i].modCount; if (segments[i].containsValue(value)) { return true; } } boolean cleanSweep = true; if (mcsum != 0) { for (int i = 0; i < segments.length; ++ i) { if (mc[i] != segments[i].modCount) { cleanSweep = false; break; } } } if (cleanSweep) { return false; } } // Resort to locking all segments for (Segment<K, V> segment: segments) { segment.lock(); } boolean found = false; try { for (Segment<K, V> segment: segments) { if (segment.containsValue(value)) { found = true; break; } } } finally { for (Segment<K, V> segment: segments) { segment.unlock(); } } return found; }
Returns <tt>true</tt> if this map maps one or more keys to the specified value. Note: This method requires a full internal traversal of the hash table, and so is much slower than method <tt>containsKey</tt>. @param value value whose presence in this map is to be tested @return <tt>true</tt> if this map maps one or more keys to the specified value @throws NullPointerException if the specified value is null
containsValue
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
public boolean contains(Object value) { return containsValue(value); }
Legacy method testing if some key maps into the specified value in this table. This method is identical in functionality to {@link #containsValue}, and exists solely to ensure full compatibility with class {@link Hashtable}, which supported this method prior to introduction of the Java Collections framework. @param value a value to search for @return <tt>true</tt> if and only if some key maps to the <tt>value</tt> argument in this table as determined by the <tt>equals</tt> method; <tt>false</tt> otherwise @throws NullPointerException if the specified value is null
contains
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
@Override public V put(K key, V value) { if (value == null) { throw new NullPointerException(); } int hash = hashOf(key); return segmentFor(hash).put(key, hash, value, false); }
Maps the specified key to the specified value in this table. Neither the key nor the value can be null. <p>The value can be retrieved by calling the <tt>get</tt> method with a key that is equal to the original key. @param key key with which the specified value is to be associated @param value value to be associated with the specified key @return the previous value associated with <tt>key</tt>, or <tt>null</tt> if there was no mapping for <tt>key</tt> @throws NullPointerException if the specified key or value is null
put
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
public V putIfAbsent(K key, V value) { if (value == null) { throw new NullPointerException(); } int hash = hashOf(key); return segmentFor(hash).put(key, hash, value, true); }
@return the previous value associated with the specified key, or <tt>null</tt> if there was no mapping for the key @throws NullPointerException if the specified key or value is null
putIfAbsent
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
@Override public void putAll(Map<? extends K, ? extends V> m) { for (Map.Entry<? extends K, ? extends V> e: m.entrySet()) { put(e.getKey(), e.getValue()); } }
Copies all of the mappings from the specified map to this one. These mappings replace any mappings that this map had for any of the keys currently in the specified map. @param m mappings to be stored in this map
putAll
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
@Override public V remove(Object key) { int hash = hashOf(key); return segmentFor(hash).remove(key, hash, null, false); }
Removes the key (and its corresponding value) from this map. This method does nothing if the key is not in the map. @param key the key that needs to be removed @return the previous value associated with <tt>key</tt>, or <tt>null</tt> if there was no mapping for <tt>key</tt> @throws NullPointerException if the specified key is null
remove
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
public boolean remove(Object key, Object value) { int hash = hashOf(key); if (value == null) { return false; } return segmentFor(hash).remove(key, hash, value, false) != null; }
@throws NullPointerException if the specified key is null
remove
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
public boolean replace(K key, V oldValue, V newValue) { if (oldValue == null || newValue == null) { throw new NullPointerException(); } int hash = hashOf(key); return segmentFor(hash).replace(key, hash, oldValue, newValue); }
@throws NullPointerException if any of the arguments are null
replace
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
public V replace(K key, V value) { if (value == null) { throw new NullPointerException(); } int hash = hashOf(key); return segmentFor(hash).replace(key, hash, value); }
@return the previous value associated with the specified key, or <tt>null</tt> if there was no mapping for the key @throws NullPointerException if the specified key or value is null
replace
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
@Override public void clear() { for (Segment<K, V> segment: segments) { segment.clear(); } }
Removes all of the mappings from this map.
clear
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
public void purgeStaleEntries() { for (Segment<K, V> segment: segments) { segment.removeStale(); } }
Removes any stale entries whose keys have been finalized. Use of this method is normally not necessary since stale entries are automatically removed lazily, when blocking operations are required. However, there are some cases where this operation should be performed eagerly, such as cleaning up old references to a ClassLoader in a multi-classloader environment. Note: this method will acquire locks, one at a time, across all segments of this table, so if it is to be used, it should be used sparingly.
purgeStaleEntries
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
@Override public Set<K> keySet() { Set<K> ks = keySet; return ks != null? ks : (keySet = new KeySet()); }
Returns a {@link Set} view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. The set supports element removal, which removes the corresponding mapping from this map, via the <tt>Iterator.remove</tt>, <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt> operations. It does not support the <tt>add</tt> or <tt>addAll</tt> operations. <p>The view's <tt>iterator</tt> is a "weakly consistent" iterator that will never throw {@link ConcurrentModificationException}, and guarantees to traverse elements as they existed upon construction of the iterator, and may (but is not guaranteed to) reflect any modifications subsequent to construction.
keySet
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
@Override public Collection<V> values() { Collection<V> vs = values; return vs != null? vs : (values = new Values()); }
Returns a {@link Collection} view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. The collection supports element removal, which removes the corresponding mapping from this map, via the <tt>Iterator.remove</tt>, <tt>Collection.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt> operations. It does not support the <tt>add</tt> or <tt>addAll</tt> operations. <p>The view's <tt>iterator</tt> is a "weakly consistent" iterator that will never throw {@link ConcurrentModificationException}, and guarantees to traverse elements as they existed upon construction of the iterator, and may (but is not guaranteed to) reflect any modifications subsequent to construction.
values
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
@Override public Set<Map.Entry<K, V>> entrySet() { Set<Map.Entry<K, V>> es = entrySet; return es != null? es : (entrySet = new EntrySet()); }
Returns a {@link Set} view of the mappings contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. The set supports element removal, which removes the corresponding mapping from the map, via the <tt>Iterator.remove</tt>, <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt> operations. It does not support the <tt>add</tt> or <tt>addAll</tt> operations. <p>The view's <tt>iterator</tt> is a "weakly consistent" iterator that will never throw {@link ConcurrentModificationException}, and guarantees to traverse elements as they existed upon construction of the iterator, and may (but is not guaranteed to) reflect any modifications subsequent to construction.
entrySet
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
public Enumeration<K> keys() { return new KeyIterator(); }
Returns an enumeration of the keys in this table. @return an enumeration of the keys in this table @see #keySet()
keys
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
public Enumeration<V> elements() { return new ValueIterator(); }
Returns an enumeration of the values in this table. @return an enumeration of the values in this table @see #values()
elements
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
public void rewind() { nextSegmentIndex = segments.length - 1; nextTableIndex = -1; currentTable = null; nextEntry = null; lastReturned = null; currentKey = null; advance(); }
Returns an enumeration of the values in this table. @return an enumeration of the values in this table @see #values()
rewind
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
public boolean hasMoreElements() { return hasNext(); }
Returns an enumeration of the values in this table. @return an enumeration of the values in this table @see #values()
hasMoreElements
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
final void advance() { if (nextEntry != null && (nextEntry = nextEntry.next) != null) { return; } while (nextTableIndex >= 0) { if ((nextEntry = currentTable[nextTableIndex --]) != null) { return; } } while (nextSegmentIndex >= 0) { Segment<K, V> seg = segments[nextSegmentIndex --]; if (seg.count != 0) { currentTable = seg.table; for (int j = currentTable.length - 1; j >= 0; -- j) { if ((nextEntry = currentTable[j]) != null) { nextTableIndex = j - 1; return; } } } } }
Returns an enumeration of the values in this table. @return an enumeration of the values in this table @see #values()
advance
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
public boolean hasNext() { while (nextEntry != null) { if (nextEntry.key() != null) { return true; } advance(); } return false; }
Returns an enumeration of the values in this table. @return an enumeration of the values in this table @see #values()
hasNext
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
HashEntry<K, V> nextEntry() { do { if (nextEntry == null) { throw new NoSuchElementException(); } lastReturned = nextEntry; currentKey = lastReturned.key(); advance(); } while (currentKey == null); // Skip GC'd keys return lastReturned; }
Returns an enumeration of the values in this table. @return an enumeration of the values in this table @see #values()
nextEntry
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
public void remove() { if (lastReturned == null) { throw new IllegalStateException(); } ConcurrentWeakKeyHashMap.this.remove(currentKey); lastReturned = null; }
Returns an enumeration of the values in this table. @return an enumeration of the values in this table @see #values()
remove
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0
public K next() { return nextEntry().key(); }
Returns an enumeration of the values in this table. @return an enumeration of the values in this table @see #values()
next
java
alibaba/arthas
common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
https://github.com/alibaba/arthas/blob/master/common/src/main/java/com/taobao/arthas/common/concurrent/ConcurrentWeakKeyHashMap.java
Apache-2.0