Class Util


  • public class Util
    extends java.lang.Object
    Collection of various utility routines that can not be assigned to other classes.
    Author:
    Bela Ban
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Util.AddressScope  
    • Constructor Summary

      Constructors 
      Constructor Description
      Util()  
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static boolean allChannelsHaveSameView​(JChannel... channels)  
      static <T> boolean allEqual​(java.util.Collection<T> elements)
      Returns true if all elements in the collection are the same
      protected static void append​(java.lang.StringBuilder sb, java.lang.String str)  
      static void assertAllChannelsHaveSameView​(JChannel... channels)  
      static void assertEquals​(java.lang.Object val1, java.lang.Object val2)  
      static void assertEquals​(java.lang.String message, java.lang.Object val1, java.lang.Object val2)  
      static void assertFalse​(boolean condition)  
      static void assertFalse​(java.lang.String message, boolean condition)  
      static void assertNotNull​(java.lang.Object val)  
      static void assertNotNull​(java.lang.String message, java.lang.Object val)  
      static void assertNull​(java.lang.String message, java.lang.Object val)  
      static void assertTrue​(boolean condition)  
      static void assertTrue​(java.lang.String message, boolean condition)  
      static java.lang.String attributeNameToMethodName​(java.lang.String attr_name)  
      static int available​(java.io.InputStream in)  
      static void binaryToAscii​(java.io.InputStream in, java.io.OutputStream out)  
      static void bind​(java.net.ServerSocket srv_sock, java.net.InetAddress bind_addr, int start_port, int end_port)  
      static void bind​(java.net.ServerSocket srv_sock, java.net.InetAddress bind_addr, int start_port, int end_port, int backlog)  
      static void bind​(java.net.Socket sock, java.net.InetAddress bind_addr, int start_port, int end_port)  
      static void bind​(java.nio.channels.ServerSocketChannel ch, java.net.InetAddress bind_addr, int start_port, int end_port)  
      static void bind​(java.nio.channels.ServerSocketChannel ch, java.net.InetAddress bind_addr, int start_port, int end_port, int backlog)  
      static java.lang.String bold​(java.lang.String msg)  
      static void bufferToArray​(Address sender, java.nio.ByteBuffer buf, Receiver target)  
      static java.lang.String byteArrayToHexString​(byte[] b)  
      static java.lang.String byteArrayToHexString​(byte[] b, int offset, int length)  
      static Message byteBufferToMessage​(byte[] buffer, int offset, int length)  
      static java.lang.String bytesToString​(byte[] bytes)  
      static <E> java.util.List<E> car​(java.util.List<E> l)  
      static <E> java.util.List<E> cdr​(java.util.List<E> l)  
      static boolean checkForAndroid()  
      static boolean checkForHp()  
      static boolean checkForLinux()  
      static boolean checkForMac()  
      static boolean checkForSolaris()  
      static boolean checkForWindows()  
      static void checkIfValidAddress​(java.net.InetAddress bind_addr, java.lang.String prot_name)  
      static <E> boolean checkOrder​(java.util.Collection<E> perm, java.util.List<E>... lists)  
      static byte clearFlags​(byte bits, byte flag)  
      static void close​(java.io.Closeable closeable)  
      static void close​(java.io.Closeable... closeables)  
      static void closeFast​(JChannel... channels)
      Closes all non-coordinators first, in parallel, then closes the coord.
      static void closeReverse​(java.io.Closeable... closeables)  
      static byte[] collectionToByteBuffer​(java.util.Collection<Address> c)  
      static <E> E[] combine​(E[]... arrays)  
      static <E> java.util.List<E> combine​(java.util.List<E> l1, java.util.List<E> l2)  
      static int compare​(byte[] left, byte[] right)
      Compares 2 byte arrays, elements are treated as unigned
      static java.lang.String[] components​(java.lang.String path, java.lang.String separator)  
      static java.util.List<Range> computeFragOffsets​(byte[] buf, int frag_size)  
      static java.util.List<Range> computeFragOffsets​(int offset, int length, int frag_size)
      Given a buffer and a fragmentation size, compute a list of fragmentation offset/length pairs, and return them in a list.
      static void connect​(java.net.Socket sock, java.net.SocketAddress dest, int sock_conn_timeout)
      Utility method.
      static boolean connect​(java.nio.channels.SocketChannel ch, java.net.SocketAddress dest)  
      static <T> boolean contains​(T key, T[] list)  
      static boolean containsId​(short id, short[] ids)  
      static boolean containsViewId​(java.util.Collection<View> views, ViewId vid)  
      static java.lang.Object convert​(java.lang.String arg, java.lang.Class<?> type)  
      static java.net.InetAddress convertToIPv6​(java.net.InetAddress ipv4_addr)  
      static byte[] createAuthenticationDigest​(java.lang.String passcode, long t1, double q1)  
      static <K,​V>
      java.util.concurrent.ConcurrentMap<K,​V>
      createConcurrentMap()  
      static <K,​V>
      java.util.concurrent.ConcurrentMap<K,​V>
      createConcurrentMap​(int initial_capacity)  
      static <K,​V>
      java.util.concurrent.ConcurrentMap<K,​V>
      createConcurrentMap​(int initial_capacity, float load_factor, int concurrency_level)  
      static java.net.DatagramSocket createDatagramSocket​(SocketFactory factory, java.lang.String service_name, java.net.InetAddress addr, int port)
      Creates a DatagramSocket bound to addr.
      static byte[] createDigest​(java.lang.String passcode, long t1, double q1)  
      static java.lang.Thread createFiber​(java.lang.Runnable r, java.lang.String name)
      Use of reflection to create fibers.
      static java.net.MulticastSocket createMulticastSocket​(SocketFactory factory, java.lang.String service_name, java.net.InetAddress mcast_addr, int port, Log log)  
      static Address createRandomAddress()  
      static Address createRandomAddress​(java.lang.String name)  
      static Address[] createRandomAddresses​(int num)
      Returns an array of num random addresses, named A, B, C etc
      static Address[] createRandomAddresses​(int num, boolean use_numbers)  
      static java.net.ServerSocket createServerSocket​(SocketFactory factory, java.lang.String service_name, java.net.InetAddress bind_addr, int start_port)  
      static java.net.ServerSocket createServerSocket​(SocketFactory factory, java.lang.String service_name, java.net.InetAddress bind_addr, int start_port, int end_port)  
      static java.net.ServerSocket createServerSocketAndBind​(SocketFactory factory, java.lang.String service_name, java.net.InetAddress bind_addr, int start_port, int end_port)
      Finds first available port starting at start_port and returns server socket.
      static java.nio.channels.ServerSocketChannel createServerSocketChannel​(SocketFactory factory, java.lang.String service_name, java.net.InetAddress bind_addr, int start_port, int end_port)  
      static java.nio.channels.ServerSocketChannel createServerSocketChannelAndBind​(java.net.InetAddress bind_addr, int start_port, int end_port)  
      static java.lang.Object[][] createTimer()  
      static byte[] defragmentBuffer​(byte[][] fragments)
      Concatenates smaller fragments into entire buffers.
      static java.util.List<View> detectDifferentViews​(java.util.Map<Address,​View> map)  
      static java.util.Collection<Address> determineActualMergeCoords​(java.util.Map<Address,​View> map)
      Similar to determineMergeCoords(java.util.Map) but only actual coordinators are counted: an actual coord is when the sender of a view is the first member of that view
      static java.util.List<Address> determineLeftMembers​(java.util.List<Address> old_mbrs, java.util.List<Address> new_mbrs)
      Returns all members that left between 2 views.
      static java.util.Collection<Address> determineMergeCoords​(java.util.Map<Address,​View> map)
      This is the same or a subset of determineMergeParticipants(java.util.Map) and contains only members which are currently sub-partition coordinators.
      static java.util.Collection<Address> determineMergeParticipants​(java.util.Map<Address,​View> map)
      Determines the members which take part in a merge.
      static <T> boolean different​(T one, T two)  
      static int discardUntilNewLine​(java.io.InputStream in)
      Reads and discards all characters from the input stream until a \r\n or EOF is encountered
      static java.lang.String dumpThreads()  
      static java.lang.Throwable exceptionFromBuffer​(byte[] buf, int offset, int length)  
      static java.lang.Throwable exceptionFromStream​(java.io.DataInput in)  
      protected static java.lang.Throwable exceptionFromStream​(java.io.DataInput in, int recursion_count)  
      static Buffer exceptionToBuffer​(java.lang.Throwable t)  
      static void exceptionToStream​(java.lang.Throwable t, java.io.DataOutput out)
      Poor man's serialization of an exception.
      protected static void exceptionToStream​(java.util.Set<java.lang.Throwable> causes, java.lang.Throwable t, java.io.DataOutput out)  
      static int factorial​(int n)  
      static boolean fibersAvailable()  
      static java.util.List<java.lang.Class<?>> findClassesAnnotatedWith​(java.lang.String packageName, java.lang.Class<? extends java.lang.annotation.Annotation> a)  
      static <T> java.util.Set<java.lang.Class<T>> findClassesAssignableFrom​(java.lang.String packageName, java.lang.Class<T> assignableFrom)  
      static java.lang.reflect.Field findField​(java.lang.Object target, java.util.List<java.lang.String> possible_names)  
      static java.lang.reflect.Method findMethod​(java.lang.Class<?> root_class, java.util.List<java.lang.String> possible_names, java.lang.Class<?>... parameter_types)  
      static java.lang.reflect.Method findMethod​(java.lang.Object target, java.util.List<java.lang.String> possible_names, java.lang.Class<?>... parameter_types)  
      static void forAllFieldsAndMethods​(java.lang.Object obj, java.util.function.Predicate<? super java.lang.reflect.AccessibleObject> filter, java.util.function.BiConsumer<java.lang.reflect.Field,​java.lang.Object> field_func, java.util.function.BiConsumer<java.lang.reflect.Method,​java.lang.Object> method_func)
      Applies a function against all fields and methods of a given target object which satisfy a given predicate.
      static byte[][] fragmentBuffer​(byte[] buf, int frag_size, int length)
      Fragments a byte buffer into smaller fragments of (max.) frag_size.
      static byte[] generateArray​(int size)  
      static java.lang.String generateLocalName()  
      static java.net.InetAddress getAddress​(java.lang.String value, StackType ip_version)  
      static java.net.InetAddress getAddress​(java.net.NetworkInterface intf, Util.AddressScope scope, StackType ip_version)
      Returns the first address on the given interface on the current host, which satisfies scope
      static java.net.InetAddress getAddress​(Util.AddressScope scope, StackType ip_version)
      Returns the first address on any interface which satisfies scope and ip_version.
      static java.net.InetAddress getAddressByCustomCode​(java.lang.String value)  
      static java.net.InetAddress getAddressByPatternMatch​(java.lang.String pattern, StackType ip_version)
      Returns a valid interface address based on a pattern.
      static java.util.Collection<java.net.InetAddress> getAllAvailableAddresses​(java.util.function.Predicate<java.net.InetAddress> filter)
      Returns all addresses of all interfaces (that are up) that satisfy a given filter (ignored if null)
      static java.util.List<java.net.NetworkInterface> getAllAvailableInterfaces()  
      static java.lang.reflect.Field[] getAllDeclaredFieldsWithAnnotations​(java.lang.Class clazz, java.lang.Class<? extends java.lang.annotation.Annotation>... annotations)  
      static java.lang.reflect.Method[] getAllDeclaredMethodsWithAnnotations​(java.lang.Class clazz, java.lang.Class<? extends java.lang.annotation.Annotation>... annotations)  
      static <A extends java.lang.annotation.Annotation>
      A
      getAnnotation​(java.lang.Class<?> clazz, java.lang.Class<A> annotationClass)  
      static java.net.NetworkInterface getByName​(java.lang.String name)
      Finds a network interface or sub-interface with the given name
      static java.net.InetAddress getByName​(java.lang.String host, StackType ip_version)
      Finds an address given a symbolic name.
      static java.lang.String getChild​(org.w3c.dom.Element root, java.lang.String path)  
      static java.lang.reflect.Field getField​(java.lang.Class clazz, java.lang.String field_name)  
      static java.lang.reflect.Field getField​(java.lang.Class clazz, java.lang.String field_name, boolean throw_exception)  
      static java.lang.Object getField​(java.lang.reflect.Field field, java.lang.Object target)  
      static StackType getIpStackType()  
      static java.net.InetAddress getLocalMulticastAddress​(StackType ip_version)  
      static java.net.InetAddress getLoopback()  
      static java.net.InetAddress getLoopback​(StackType ip_version)
      IP related utilities
      static javax.management.MBeanServer getMBeanServer()  
      static java.lang.String getMessage​(java.lang.String key)  
      static java.lang.String getNameFromAnnotation​(java.lang.reflect.AccessibleObject obj)  
      static int getNextHigherPowerOfTwo​(int num)  
      static java.net.InetAddress getNonLoopbackAddress()
      Returns the first non-loopback address on any interface on the current host
      static java.net.InetAddress getNonLoopbackAddress​(StackType ip_version)  
      static java.lang.String getProperty​(java.lang.String s)  
      static java.lang.String getProperty​(java.lang.String[] system_props, java.util.Properties props, java.lang.String prop_name, java.lang.String default_value)
      Returns a value associated wither with one or more system properties, or found in the props map
      static java.lang.String getProperty​(java.lang.String s, java.util.Properties p)  
      static int getRank​(java.util.Collection<Address> members, Address addr)  
      static int getRank​(View view, Address addr)
      Returns the rank of a member in a given view
      static java.io.InputStream getResourceAsStream​(java.lang.String name, java.lang.Class clazz)  
      static java.io.InputStream getResourceAsStream​(java.lang.String resourcePath, java.lang.ClassLoader userClassLoader)  
      static Protocol[] getTestStack​(Protocol... additional_protocols)
      Returns a default stack for testing with transport = SHARED_LOOPBACK
      static java.lang.Class<? extends Protocol>[] getUnicastProtocols()  
      protected static <E> int index​(java.util.Collection<E> list, E el)  
      static boolean interfaceHasIPAddresses​(java.net.NetworkInterface intf, StackType ip_version)
      A function to check if an interface supports an IP version (i.e has addresses defined for that IP version).
      static boolean interruptAndWaitToDie​(java.lang.Thread t)  
      static boolean interruptAndWaitToDie​(java.lang.Thread t, long timeout)  
      static boolean isAsciiString​(java.lang.String str)  
      static boolean isCoordinator​(JChannel ch)  
      static boolean isCoordinator​(View view, Address local_addr)  
      static boolean isFlagSet​(byte bits, byte flag)  
      static boolean isIpAddressPermissible​(java.net.InetAddress addr)  
      static boolean isIpv4StackAvailable()  
      static boolean isIpv6StackAvailable()  
      static boolean isStackAvailable​(boolean ipv4)  
      static boolean isUp​(java.net.NetworkInterface ni)
      Always returns true unless there is a socket exception - will be removed when GraalVM issue https://github.com/oracle/graal/pull/1076 has been fixed
      static int keyPress​(java.lang.String msg)  
      static java.util.List<Address> leftMembers​(java.util.Collection<Address> old_list, java.util.Collection<Address> new_list)  
      static java.lang.Class loadClass​(java.lang.String classname, java.lang.Class clazz)
      Tries to load the class from the current thread's context class loader.
      static java.lang.Class<?> loadClass​(java.lang.String classname, java.lang.ClassLoader preferredLoader)
      Tries to load the class from the preferred loader.
      static java.lang.Class<? extends Protocol> loadProtocolClass​(java.lang.String protocol_name, java.lang.Class<?> cl)  
      static java.lang.String mapToString​(java.util.Map<? extends java.lang.Object,​? extends java.lang.Object> map)  
      protected static byte[] marshalPrimitiveType​(byte type, java.lang.Object obj)  
      static boolean match​(long[] a1, long[] a2)  
      static boolean match​(java.net.InetAddress addr, Util.AddressScope scope)  
      static <T> boolean match​(T obj1, T obj2)  
      static java.lang.String md5​(java.lang.String source)
      Converts a java.lang.String in to a MD5 hashed String
      static Buffer messageToByteBuffer​(Message msg)  
      static java.lang.String methodNameToAttributeName​(java.lang.String methodName)
      Converts a method name to an attribute name, e.g.
      static java.lang.String methodNameToJavaAttributeName​(java.lang.String methodName)
      Converts a method name to a Java attribute name, e.g.
      static long micros()  
      static <T> java.util.List<T> newElements​(java.util.List<T> old_list, java.util.List<T> new_list)  
      static java.util.List<Address> newMembers​(java.util.List<Address> old_list, java.util.List<Address> new_list)  
      static <T> T objectFromByteBuffer​(byte[] buffer)
      Creates an object from a byte buffer
      static <T> T objectFromByteBuffer​(byte[] buffer, int offset, int length)  
      static <T> T objectFromByteBuffer​(byte[] buffer, int offset, int length, java.lang.ClassLoader loader)  
      static <T> T objectFromStream​(java.io.DataInput in)  
      static <T> T objectFromStream​(java.io.DataInput in, java.lang.ClassLoader loader)  
      static Buffer objectToBuffer​(java.lang.Object obj)  
      static byte[] objectToByteBuffer​(java.lang.Object obj)
      Serializes/Streams an object into a byte buffer.
      static void objectToStream​(java.lang.Object obj, java.io.DataOutput out)  
      static <E> java.util.Collection<java.util.List<E>> orderedPermutation​(java.util.List<E> a, java.util.List<E> b)
      Performs an ordered permutation of the elements of a and b such that the order of elements in list a and b is preserved.
      static void parse​(byte[] buf, int offset, int length, java.util.function.BiConsumer<java.lang.Short,​Message> msg_consumer, java.util.function.BiConsumer<java.lang.Short,​MessageBatch> batch_consumer, boolean tcp)  
      static void parse​(java.io.InputStream input, java.util.function.BiConsumer<java.lang.Short,​Message> msg_consumer, java.util.function.BiConsumer<java.lang.Short,​MessageBatch> batch_consumer, boolean tcp)  
      static void parse​(java.lang.String filename, java.util.function.BiConsumer<java.lang.Short,​Message> msg_consumer, java.util.function.BiConsumer<java.lang.Short,​MessageBatch> batch_consumer, boolean tcp)  
      static java.util.List<PhysicalAddress> parseCommaDelimitedHosts​(java.lang.String hosts, int port_range)
      Input is "daddy[8880],sindhu[8880],camille[5555].
      static java.util.List<java.net.InetSocketAddress> parseCommaDelimitedHosts2​(java.lang.String hosts, int port_range)
      Input is "daddy[8880],sindhu[8880],camille[5555].
      static boolean parseCommaDelimitedHostsInto​(java.util.Collection<PhysicalAddress> list, java.util.Collection<java.lang.String> unresolved_hosts, java.lang.String hosts, int port_range)
      Parses a string into a list of IpAddresses
      static int[] parseCommaDelimitedInts​(java.lang.String s)
      Parses comma-delimited longs; e.g., 2000,4000,8000.
      static long[] parseCommaDelimitedLongs​(java.lang.String s)
      Parses comma-delimited longs; e.g., 2000,4000,8000.
      static java.util.Map<java.lang.String,​java.lang.String> parseCommaDelimitedProps​(java.lang.String s)  
      static java.util.List<java.lang.String> parseCommaDelimitedStrings​(java.lang.String l)
      e.g.
      static java.util.List<java.net.NetworkInterface> parseInterfaceList​(java.lang.String s)  
      static java.util.concurrent.RejectedExecutionHandler parseRejectionPolicy​(java.lang.String rejection_policy)  
      static java.util.List<java.lang.String> parseStringList​(java.lang.String l, java.lang.String separator)  
      static boolean patternMatch​(java.lang.String pattern, java.lang.String str)  
      protected static <E> java.util.List<E> permute​(java.util.List<E> l1, java.util.List<E> l2, int remove_from_l1, int remove_from_l2)  
      protected static <E> void permute​(java.util.List<E> prefix, java.util.List<E> list, int index, java.util.List<java.util.List<E>> permutations)  
      static <E> void permute​(java.util.List<E> list, java.util.List<java.util.List<E>> permutations)
      Inefficient permutation of a generic list; uses too much copying among other things.
      static <T> T pickNext​(java.util.List<T> list, T obj)  
      static <T> java.util.List<T> pickNext​(java.util.List<T> list, T obj, int num)
      Returns the next min(N,list.size()) elements after obj
      static <T> T pickRandomElement​(java.util.List<T> list)  
      static <T> T pickRandomElement​(java.util.Set<T> set)  
      static <T> T pickRandomElement​(T[] array)  
      static java.lang.String print​(java.lang.Object[] objs)  
      static java.lang.String print​(java.nio.ByteBuffer buf)  
      static <T> java.lang.String print​(java.util.Collection<T> objs)  
      static java.lang.String print​(java.util.List<java.net.NetworkInterface> interfaces)  
      static <T> java.lang.String print​(java.util.Map<T,​T> map)  
      static java.lang.String printBytes​(double bytes)  
      static java.lang.String printBytes​(long bytes)
      MByte nowadays doesn't mean 1024 * 1024 bytes, but 1 million bytes, see http://en.wikipedia.org/wiki/Megabyte
      static <T> java.lang.String printListWithDelimiter​(java.util.Collection<T> list, java.lang.String delimiter)  
      static <T> java.lang.String printListWithDelimiter​(java.util.Collection<T> list, java.lang.String delimiter, int limit)  
      static <T> java.lang.String printListWithDelimiter​(java.util.Collection<T> list, java.lang.String delimiter, int limit, boolean print_size)  
      static <T> java.lang.String printListWithDelimiter​(T[] list, java.lang.String delimiter, int limit)  
      static <T> java.lang.String printMapWithDelimiter​(java.util.Map<T,​T> map, java.lang.String delimiter)  
      static java.lang.String printNanos​(double time_ns)  
      static java.lang.String printNanos​(long time_ns)  
      static java.lang.String printPingData​(java.util.List<PingData> rsps)  
      static java.lang.String printTime​(long time, java.util.concurrent.TimeUnit unit)  
      static java.lang.String printViews​(JChannel... channels)  
      static boolean productGreaterThan​(long n1, long n2, long val)  
      static long random​(long range)
      Returns a random value in the range [1 - range].
      static <T extends Streamable>
      T[]
      read​(java.lang.Class<T> clazz, java.io.DataInput in)  
      static Address readAddress​(java.io.DataInput in)  
      static Address[] readAddresses​(java.io.DataInput in)  
      static <T extends java.util.Collection<Address>>
      T
      readAddresses​(java.io.DataInput in, java.util.function.IntFunction<T> factory)  
      static byte[] readByteBuffer​(java.io.DataInput in)  
      static double readBytesDouble​(java.lang.String input)  
      static int readBytesInteger​(java.lang.String input)  
      static long readBytesLong​(java.lang.String input)  
      static java.lang.String readContents​(java.io.InputStream input)  
      static double readDoubleFromStdin​(java.lang.String message)  
      protected static java.lang.Throwable readException​(java.io.DataInput in, int recursion_count)  
      static java.lang.String readFile​(java.lang.String filename)  
      static byte[] readFileContents​(java.io.InputStream input)  
      static <T extends Streamable>
      T
      readGenericStreamable​(java.io.DataInput in)  
      static <T extends Streamable>
      T
      readGenericStreamable​(java.io.DataInput in, java.lang.ClassLoader loader)  
      static int readIntFromStdin​(java.lang.String message)  
      static java.lang.String readLine​(java.io.InputStream in)
      Reads a line of text.
      static long readLongFromStdin​(java.lang.String message)  
      static Message readMessage​(java.io.DataInput instream)  
      static MessageBatch[] readMessageBatch​(java.io.DataInput in, boolean multicast)
      Reads a list of messages into 4 MessageBatches: regular OOB INTERNAL-OOB (INTERNAL and OOB) INTERNAL (INTERNAL)
      static java.util.List<Message> readMessageList​(java.io.DataInput in, short transport_id)  
      static java.lang.Object readObject​(java.io.DataInput in)  
      static <T extends Streamable>
      T
      readStreamable​(java.util.function.Supplier<T> factory, java.io.DataInput in)  
      static java.lang.String readStringFromStdin​(java.lang.String message)  
      static java.lang.String readTillMatchingCharacter​(java.io.InputStream in, char c)
      Returns all characters read from the current position until the next occurrence of 'c' has been read (including 'c'), or eof, whichever comes first
      static java.lang.String readToken​(java.io.InputStream in)
      Returns whitespace-separated strings from the input stream, or null if the end of the stream has been reached
      static View readView​(java.io.DataInput in)  
      static ViewId readViewId​(java.io.DataInput in)  
      static void registerChannel​(JChannel channel, java.lang.String name)  
      static java.lang.String replaceProperties​(java.lang.String string, java.util.Properties props)
      static boolean sameAddresses​(java.net.InetAddress one, java.net.InetAddress two)
      Returns true if the 2 addresses are of the same type (IPv4 or IPv6)
      static boolean sameHost​(Address one, Address two)
      Checks whether 2 Addresses are on the same host
      static void setField​(java.lang.reflect.Field field, java.lang.Object target, java.lang.Object value)  
      static byte setFlag​(byte bits, byte flag)  
      static java.lang.String sha​(java.lang.String source)
      Converts a java.lang.String in to a SHA hashed String
      static java.lang.String shortName​(java.lang.String hostname)  
      static java.lang.String shortName​(java.net.InetAddress hostname)  
      static <T> void shuffle​(T[] array, int from, int to)
      Reorders elements of an array in-place.
      static void shutdown​(JChannel ch)
      Drops messages to/from other members and then closes the channel.
      static int size​(byte[] buf)  
      static int size​(java.lang.String s)  
      static long size​(java.util.Collection<? extends Address> addrs)
      Returns the marshalled size of a Collection of Addresses.
      static int size​(Address addr)  
      static long size​(Address[] addrs)  
      static int size​(View view)  
      static int size​(ViewId vid)  
      static void sleep​(long timeout)
      Sleep for timeout msecs.
      static void sleep​(long timeout, int nanos)  
      static void sleepRandom​(long floor, long ceiling)
      Sleeps between floor and ceiling milliseconds, chosen randomly
      static boolean startFlush​(JChannel c)
      Performs the flush of the given channel
      static boolean startFlush​(JChannel c, int numberOfAttempts, long randomSleepTimeoutFloor, long randomSleepTimeoutCeiling)
      Performs the flush of the given channel within the specfied number of attempts along with random sleep time after each such attempt.
      static boolean startFlush​(JChannel c, java.util.List<Address> flushParticipants)
      Performs the flush of the given channel and the specified flush participants
      static boolean startFlush​(JChannel c, java.util.List<Address> flushParticipants, int numberOfAttempts, long randomSleepTimeoutFloor, long randomSleepTimeoutCeiling)
      Performs the flush of the given channel for the specified flush participants and the given number of attempts along with random sleep time after each such attempt.
      static <T extends Streamable>
      T
      streamableFromBuffer​(java.util.function.Supplier<T> factory, byte[] buf, int off, int len)  
      static <T extends Streamable>
      T
      streamableFromByteBuffer​(java.lang.Class<? extends Streamable> cl, byte[] buffer)  
      static <T extends Streamable>
      T
      streamableFromByteBuffer​(java.lang.Class<? extends Streamable> cl, byte[] buffer, int offset, int length)  
      static <T extends Streamable>
      T
      streamableFromByteBuffer​(java.util.function.Supplier<T> factory, byte[] buffer)  
      static <T extends Streamable>
      T
      streamableFromByteBuffer​(java.util.function.Supplier<T> factory, byte[] buffer, int offset, int length)  
      static Buffer streamableToBuffer​(Streamable obj)  
      static byte[] streamableToByteBuffer​(Streamable obj)  
      static byte[] stringToBytes​(java.lang.String str)  
      static java.lang.String substituteVariable​(java.lang.String val)
      Replaces variables of ${var:default} with System.getProperty(var, default).
      static java.lang.String substituteVariable​(java.lang.String val, java.util.Properties p)
      Replaces variables of ${var:default} with Properties then uses System.getProperty(var, default) if the value was not found.
      protected static <E> java.util.List<E> swap​(java.util.List<E> l, int index)  
      static boolean tossWeightedCoin​(double probability)
      Tosses a coin weighted with probability and returns true or false.
      static java.net.InetAddress validateBindAddressFromInterface​(java.net.InetAddress bind_addr, java.lang.String bind_interface_str, StackType ip_version)
      Method used by PropertyConverters.BindInterface to check that a bind_addr is consistent with a specified interface
      static void waitUntil​(long timeout, long interval, Condition condition)  
      static void waitUntil​(long timeout, long interval, Condition condition, java.util.function.Supplier<java.lang.String> msg)  
      static void waitUntilAllChannelsHaveSameView​(long timeout, long interval, java.util.Collection<JChannel> channels)  
      static void waitUntilAllChannelsHaveSameView​(long timeout, long interval, JChannel... channels)
      Blocks until all channels have the same view
      static <T> void waitUntilListHasSize​(java.util.List<T> list, int expected_size, long timeout, long interval)
      Waits until a list has the expected number of elements.
      static boolean waitUntilTrue​(long timeout, long interval, java.util.function.BooleanSupplier condition)  
      static <T extends Streamable>
      void
      write​(T[] array, java.io.DataOutput out)  
      static void writeAddress​(Address addr, java.io.DataOutput out)  
      static void writeAddresses​(java.util.Collection<? extends Address> v, java.io.DataOutput out)
      Writes a list of Addresses.
      static void writeAddresses​(Address[] addrs, java.io.DataOutput out)  
      static void writeByteBuffer​(byte[] buf, int offset, int length, java.io.DataOutput out)  
      static void writeByteBuffer​(byte[] buf, java.io.DataOutput out)  
      protected static void writeException​(java.util.Set<java.lang.Throwable> causes, java.lang.Throwable t, java.io.DataOutput out)  
      static void writeGenericStreamable​(Streamable obj, java.io.DataOutput out)  
      static void writeMessage​(Message msg, java.io.DataOutput dos, boolean multicast)  
      static void writeMessageList​(Address dest, Address src, byte[] cluster_name, java.util.List<Message> msgs, java.io.DataOutput dos, boolean multicast, short transport_id)
      Write a list of messages with the *same* destination and src addresses.
      static void writeMessageListHeader​(Address dest, Address src, byte[] cluster_name, int numMsgs, java.io.DataOutput dos, boolean multicast)  
      static void writeObject​(java.lang.Object obj, java.io.DataOutput out)  
      static void writeStreamable​(Streamable obj, java.io.DataOutput out)  
      static void writeView​(View view, java.io.DataOutput out)  
      static void writeViewId​(ViewId vid, java.io.DataOutput out)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • CCHM_INITIAL_CAPACITY

        protected static int CCHM_INITIAL_CAPACITY
      • CCHM_LOAD_FACTOR

        protected static float CCHM_LOAD_FACTOR
      • CCHM_CONCURRENCY_LEVEL

        protected static int CCHM_CONCURRENCY_LEVEL
      • DEFAULT_HEADERS

        public static final int DEFAULT_HEADERS
      • MAX_LIST_PRINT_SIZE

        public static int MAX_LIST_PRINT_SIZE
        The max size of an address list, used in View or Digest when toString() is called. Limiting this reduces the amount of log data
      • can_bind_to_mcast_addr

        public static final boolean can_bind_to_mcast_addr
      • resource_bundle

        protected static java.util.ResourceBundle resource_bundle
      • BUILDER

        protected static java.lang.reflect.Method BUILDER
      • VIRTUAL

        protected static java.lang.reflect.Method VIRTUAL
      • TASK

        protected static java.lang.reflect.Method TASK
      • NAME

        protected static java.lang.reflect.Method NAME
      • BUILD

        protected static java.lang.reflect.Method BUILD
      • BUILDER_CLASS

        protected static java.lang.Class<?> BUILDER_CLASS
    • Constructor Detail

      • Util

        public Util()
    • Method Detail

      • getUnicastProtocols

        public static final java.lang.Class<? extends Protocol>[] getUnicastProtocols()
      • fibersAvailable

        public static boolean fibersAvailable()
      • createFiber

        public static java.lang.Thread createFiber​(java.lang.Runnable r,
                                                   java.lang.String name)
        Use of reflection to create fibers. If a JDK < 15/Loom is found, we'll create regular threads.
      • assertTrue

        public static void assertTrue​(boolean condition)
      • assertTrue

        public static void assertTrue​(java.lang.String message,
                                      boolean condition)
      • assertFalse

        public static void assertFalse​(boolean condition)
      • assertFalse

        public static void assertFalse​(java.lang.String message,
                                       boolean condition)
      • assertEquals

        public static void assertEquals​(java.lang.String message,
                                        java.lang.Object val1,
                                        java.lang.Object val2)
      • assertEquals

        public static void assertEquals​(java.lang.Object val1,
                                        java.lang.Object val2)
      • assertNotNull

        public static void assertNotNull​(java.lang.String message,
                                         java.lang.Object val)
      • assertNotNull

        public static void assertNotNull​(java.lang.Object val)
      • assertNull

        public static void assertNull​(java.lang.String message,
                                      java.lang.Object val)
      • getNextHigherPowerOfTwo

        public static int getNextHigherPowerOfTwo​(int num)
      • bold

        public static java.lang.String bold​(java.lang.String msg)
      • getMessage

        public static java.lang.String getMessage​(java.lang.String key)
      • getTestStack

        public static Protocol[] getTestStack​(Protocol... additional_protocols)
        Returns a default stack for testing with transport = SHARED_LOOPBACK
        Parameters:
        additional_protocols - Any number of protocols to add to the top of the returned protocol list
        Returns:
      • waitUntilAllChannelsHaveSameView

        public static void waitUntilAllChannelsHaveSameView​(long timeout,
                                                            long interval,
                                                            JChannel... channels)
                                                     throws java.util.concurrent.TimeoutException
        Blocks until all channels have the same view
        Parameters:
        timeout - How long to wait (max in ms)
        interval - Check every interval ms
        channels - The channels which should form the view. The expected view size is channels.length. Must be non-null
        Throws:
        java.util.concurrent.TimeoutException
      • waitUntilAllChannelsHaveSameView

        public static void waitUntilAllChannelsHaveSameView​(long timeout,
                                                            long interval,
                                                            java.util.Collection<JChannel> channels)
                                                     throws java.util.concurrent.TimeoutException
        Throws:
        java.util.concurrent.TimeoutException
      • waitUntil

        public static void waitUntil​(long timeout,
                                     long interval,
                                     Condition condition)
                              throws java.util.concurrent.TimeoutException
        Throws:
        java.util.concurrent.TimeoutException
      • waitUntil

        public static void waitUntil​(long timeout,
                                     long interval,
                                     Condition condition,
                                     java.util.function.Supplier<java.lang.String> msg)
                              throws java.util.concurrent.TimeoutException
        Throws:
        java.util.concurrent.TimeoutException
      • waitUntilTrue

        public static boolean waitUntilTrue​(long timeout,
                                            long interval,
                                            java.util.function.BooleanSupplier condition)
      • allChannelsHaveSameView

        public static boolean allChannelsHaveSameView​(JChannel... channels)
      • assertAllChannelsHaveSameView

        public static void assertAllChannelsHaveSameView​(JChannel... channels)
      • printViews

        public static java.lang.String printViews​(JChannel... channels)
      • waitUntilListHasSize

        public static <T> void waitUntilListHasSize​(java.util.List<T> list,
                                                    int expected_size,
                                                    long timeout,
                                                    long interval)
        Waits until a list has the expected number of elements. Throws an exception if not met
        Type Parameters:
        T - The type of the list
        Parameters:
        list - The list
        expected_size - The expected size
        timeout - The time to wait (in ms)
        interval - The interval at which to get the size of the list (in ms)
      • createAuthenticationDigest

        public static byte[] createAuthenticationDigest​(java.lang.String passcode,
                                                        long t1,
                                                        double q1)
                                                 throws java.io.IOException,
                                                        java.security.NoSuchAlgorithmException
        Throws:
        java.io.IOException
        java.security.NoSuchAlgorithmException
      • createDigest

        public static byte[] createDigest​(java.lang.String passcode,
                                          long t1,
                                          double q1)
                                   throws java.io.IOException,
                                          java.security.NoSuchAlgorithmException
        Throws:
        java.io.IOException
        java.security.NoSuchAlgorithmException
      • connect

        public static void connect​(java.net.Socket sock,
                                   java.net.SocketAddress dest,
                                   int sock_conn_timeout)
                            throws java.io.IOException
        Utility method. If the dest address is IPv6, convert scoped link-local addrs into unscoped ones
        Parameters:
        sock -
        dest -
        sock_conn_timeout -
        Throws:
        java.io.IOException
      • connect

        public static boolean connect​(java.nio.channels.SocketChannel ch,
                                      java.net.SocketAddress dest)
                               throws java.io.IOException
        Throws:
        java.io.IOException
      • close

        public static void close​(java.io.Closeable closeable)
      • close

        public static void close​(java.io.Closeable... closeables)
      • closeReverse

        public static void closeReverse​(java.io.Closeable... closeables)
      • closeFast

        public static void closeFast​(JChannel... channels)
        Closes all non-coordinators first, in parallel, then closes the coord. This should produce just 2 views
      • shutdown

        public static void shutdown​(JChannel ch)
                             throws java.lang.Exception
        Drops messages to/from other members and then closes the channel. Note that this member won't get excluded from the view until failure detection has kicked in and the new coord installed the new view
        Throws:
        java.lang.Exception
      • setFlag

        public static byte setFlag​(byte bits,
                                   byte flag)
      • isFlagSet

        public static boolean isFlagSet​(byte bits,
                                        byte flag)
      • clearFlags

        public static byte clearFlags​(byte bits,
                                      byte flag)
      • objectFromByteBuffer

        public static <T> T objectFromByteBuffer​(byte[] buffer)
                                          throws java.io.IOException,
                                                 java.lang.ClassNotFoundException
        Creates an object from a byte buffer
        Throws:
        java.io.IOException
        java.lang.ClassNotFoundException
      • objectFromByteBuffer

        public static <T> T objectFromByteBuffer​(byte[] buffer,
                                                 int offset,
                                                 int length)
                                          throws java.io.IOException,
                                                 java.lang.ClassNotFoundException
        Throws:
        java.io.IOException
        java.lang.ClassNotFoundException
      • objectFromByteBuffer

        public static <T> T objectFromByteBuffer​(byte[] buffer,
                                                 int offset,
                                                 int length,
                                                 java.lang.ClassLoader loader)
                                          throws java.io.IOException,
                                                 java.lang.ClassNotFoundException
        Throws:
        java.io.IOException
        java.lang.ClassNotFoundException
      • objectToByteBuffer

        public static byte[] objectToByteBuffer​(java.lang.Object obj)
                                         throws java.io.IOException
        Serializes/Streams an object into a byte buffer. The object has to implement interface Serializable or Externalizable or Streamable.
        Throws:
        java.io.IOException
      • objectToBuffer

        public static Buffer objectToBuffer​(java.lang.Object obj)
                                     throws java.io.IOException
        Throws:
        java.io.IOException
      • marshalPrimitiveType

        protected static byte[] marshalPrimitiveType​(byte type,
                                                     java.lang.Object obj)
      • objectToStream

        public static void objectToStream​(java.lang.Object obj,
                                          java.io.DataOutput out)
                                   throws java.io.IOException
        Throws:
        java.io.IOException
      • objectFromStream

        public static <T> T objectFromStream​(java.io.DataInput in)
                                      throws java.io.IOException,
                                             java.lang.ClassNotFoundException
        Throws:
        java.io.IOException
        java.lang.ClassNotFoundException
      • objectFromStream

        public static <T> T objectFromStream​(java.io.DataInput in,
                                             java.lang.ClassLoader loader)
                                      throws java.io.IOException,
                                             java.lang.ClassNotFoundException
        Throws:
        java.io.IOException
        java.lang.ClassNotFoundException
      • streamableFromByteBuffer

        public static <T extends Streamable> T streamableFromByteBuffer​(java.lang.Class<? extends Streamable> cl,
                                                                        byte[] buffer)
                                                                 throws java.lang.Exception
        Throws:
        java.lang.Exception
      • streamableFromByteBuffer

        public static <T extends Streamable> T streamableFromByteBuffer​(java.util.function.Supplier<T> factory,
                                                                        byte[] buffer)
                                                                 throws java.lang.Exception
        Throws:
        java.lang.Exception
      • exceptionToStream

        public static void exceptionToStream​(java.lang.Throwable t,
                                             java.io.DataOutput out)
                                      throws java.io.IOException
        Poor man's serialization of an exception. Serializes only the message, stack trace and cause (not suppressed exceptions)
        Throws:
        java.io.IOException
      • exceptionToStream

        protected static void exceptionToStream​(java.util.Set<java.lang.Throwable> causes,
                                                java.lang.Throwable t,
                                                java.io.DataOutput out)
                                         throws java.io.IOException
        Throws:
        java.io.IOException
      • exceptionToBuffer

        public static Buffer exceptionToBuffer​(java.lang.Throwable t)
                                        throws java.io.IOException
        Throws:
        java.io.IOException
      • exceptionFromStream

        public static java.lang.Throwable exceptionFromStream​(java.io.DataInput in)
                                                       throws java.io.IOException,
                                                              java.lang.ClassNotFoundException
        Throws:
        java.io.IOException
        java.lang.ClassNotFoundException
      • exceptionFromStream

        protected static java.lang.Throwable exceptionFromStream​(java.io.DataInput in,
                                                                 int recursion_count)
                                                          throws java.io.IOException,
                                                                 java.lang.ClassNotFoundException
        Throws:
        java.io.IOException
        java.lang.ClassNotFoundException
      • writeException

        protected static void writeException​(java.util.Set<java.lang.Throwable> causes,
                                             java.lang.Throwable t,
                                             java.io.DataOutput out)
                                      throws java.io.IOException
        Throws:
        java.io.IOException
      • readException

        protected static java.lang.Throwable readException​(java.io.DataInput in,
                                                           int recursion_count)
                                                    throws java.io.IOException,
                                                           java.lang.ClassNotFoundException
        Throws:
        java.io.IOException
        java.lang.ClassNotFoundException
      • exceptionFromBuffer

        public static java.lang.Throwable exceptionFromBuffer​(byte[] buf,
                                                              int offset,
                                                              int length)
                                                       throws java.io.IOException,
                                                              java.lang.ClassNotFoundException
        Throws:
        java.io.IOException
        java.lang.ClassNotFoundException
      • bufferToArray

        public static void bufferToArray​(Address sender,
                                         java.nio.ByteBuffer buf,
                                         Receiver target)
      • streamableFromByteBuffer

        public static <T extends Streamable> T streamableFromByteBuffer​(java.lang.Class<? extends Streamable> cl,
                                                                        byte[] buffer,
                                                                        int offset,
                                                                        int length)
                                                                 throws java.lang.Exception
        Throws:
        java.lang.Exception
      • streamableFromByteBuffer

        public static <T extends Streamable> T streamableFromByteBuffer​(java.util.function.Supplier<T> factory,
                                                                        byte[] buffer,
                                                                        int offset,
                                                                        int length)
                                                                 throws java.lang.Exception
        Throws:
        java.lang.Exception
      • streamableFromBuffer

        public static <T extends Streamable> T streamableFromBuffer​(java.util.function.Supplier<T> factory,
                                                                    byte[] buf,
                                                                    int off,
                                                                    int len)
                                                             throws java.io.IOException,
                                                                    java.lang.ClassNotFoundException
        Throws:
        java.io.IOException
        java.lang.ClassNotFoundException
      • streamableToByteBuffer

        public static byte[] streamableToByteBuffer​(Streamable obj)
                                             throws java.io.IOException
        Throws:
        java.io.IOException
      • streamableToBuffer

        public static Buffer streamableToBuffer​(Streamable obj)
      • collectionToByteBuffer

        public static byte[] collectionToByteBuffer​(java.util.Collection<Address> c)
                                             throws java.io.IOException
        Throws:
        java.io.IOException
      • stringToBytes

        public static byte[] stringToBytes​(java.lang.String str)
      • bytesToString

        public static java.lang.String bytesToString​(byte[] bytes)
      • byteArrayToHexString

        public static java.lang.String byteArrayToHexString​(byte[] b)
      • byteArrayToHexString

        public static java.lang.String byteArrayToHexString​(byte[] b,
                                                            int offset,
                                                            int length)
      • binaryToAscii

        public static void binaryToAscii​(java.io.InputStream in,
                                         java.io.OutputStream out)
                                  throws java.io.IOException
        Throws:
        java.io.IOException
      • isAsciiString

        public static boolean isAsciiString​(java.lang.String str)
      • compare

        public static int compare​(byte[] left,
                                  byte[] right)
        Compares 2 byte arrays, elements are treated as unigned
      • convert

        public static java.lang.Object convert​(java.lang.String arg,
                                               java.lang.Class<?> type)
      • writeMessage

        public static void writeMessage​(Message msg,
                                        java.io.DataOutput dos,
                                        boolean multicast)
                                 throws java.io.IOException
        Throws:
        java.io.IOException
      • readMessage

        public static Message readMessage​(java.io.DataInput instream)
                                   throws java.io.IOException,
                                          java.lang.ClassNotFoundException
        Throws:
        java.io.IOException
        java.lang.ClassNotFoundException
      • writeMessageList

        public static void writeMessageList​(Address dest,
                                            Address src,
                                            byte[] cluster_name,
                                            java.util.List<Message> msgs,
                                            java.io.DataOutput dos,
                                            boolean multicast,
                                            short transport_id)
                                     throws java.io.IOException
        Write a list of messages with the *same* destination and src addresses. The message list is marshalled as follows (see doc/design/MarshallingFormat.txt for details):
         List: * | version | flags | dest | src | cluster-name | [Message*] |
        
         Message:  | presence | leading | flags | [src] | length | [buffer] | size | [Headers*] |
        
         
        Parameters:
        dest -
        src -
        msgs -
        dos -
        multicast -
        Throws:
        java.lang.Exception
        java.io.IOException
      • writeMessageListHeader

        public static void writeMessageListHeader​(Address dest,
                                                  Address src,
                                                  byte[] cluster_name,
                                                  int numMsgs,
                                                  java.io.DataOutput dos,
                                                  boolean multicast)
                                           throws java.io.IOException
        Throws:
        java.io.IOException
      • readMessageList

        public static java.util.List<Message> readMessageList​(java.io.DataInput in,
                                                              short transport_id)
                                                       throws java.io.IOException,
                                                              java.lang.ClassNotFoundException
        Throws:
        java.io.IOException
        java.lang.ClassNotFoundException
      • readMessageBatch

        public static MessageBatch[] readMessageBatch​(java.io.DataInput in,
                                                      boolean multicast)
                                               throws java.io.IOException,
                                                      java.lang.ClassNotFoundException
        Reads a list of messages into 4 MessageBatches:
        1. regular
        2. OOB
        3. INTERNAL-OOB (INTERNAL and OOB)
        4. INTERNAL (INTERNAL)
        Parameters:
        in -
        Returns:
        an array of 4 MessageBatches in the order above, the first batch is at index 0
        Throws:
        java.lang.Exception
        java.io.IOException
        java.lang.ClassNotFoundException
      • parse

        public static void parse​(byte[] buf,
                                 int offset,
                                 int length,
                                 java.util.function.BiConsumer<java.lang.Short,​Message> msg_consumer,
                                 java.util.function.BiConsumer<java.lang.Short,​MessageBatch> batch_consumer,
                                 boolean tcp)
      • parse

        public static void parse​(java.lang.String filename,
                                 java.util.function.BiConsumer<java.lang.Short,​Message> msg_consumer,
                                 java.util.function.BiConsumer<java.lang.Short,​MessageBatch> batch_consumer,
                                 boolean tcp)
                          throws java.io.FileNotFoundException
        Throws:
        java.io.FileNotFoundException
      • parse

        public static void parse​(java.io.InputStream input,
                                 java.util.function.BiConsumer<java.lang.Short,​Message> msg_consumer,
                                 java.util.function.BiConsumer<java.lang.Short,​MessageBatch> batch_consumer,
                                 boolean tcp)
      • writeView

        public static void writeView​(View view,
                                     java.io.DataOutput out)
                              throws java.io.IOException
        Throws:
        java.io.IOException
      • readView

        public static View readView​(java.io.DataInput in)
                             throws java.io.IOException,
                                    java.lang.ClassNotFoundException
        Throws:
        java.io.IOException
        java.lang.ClassNotFoundException
      • writeViewId

        public static void writeViewId​(ViewId vid,
                                       java.io.DataOutput out)
                                throws java.io.IOException
        Throws:
        java.io.IOException
      • readViewId

        public static ViewId readViewId​(java.io.DataInput in)
                                 throws java.io.IOException,
                                        java.lang.ClassNotFoundException
        Throws:
        java.io.IOException
        java.lang.ClassNotFoundException
      • writeAddress

        public static void writeAddress​(Address addr,
                                        java.io.DataOutput out)
                                 throws java.io.IOException
        Throws:
        java.io.IOException
      • readAddress

        public static Address readAddress​(java.io.DataInput in)
                                   throws java.io.IOException,
                                          java.lang.ClassNotFoundException
        Throws:
        java.io.IOException
        java.lang.ClassNotFoundException
      • size

        public static int size​(Address addr)
      • size

        public static int size​(View view)
      • size

        public static int size​(ViewId vid)
      • size

        public static int size​(java.lang.String s)
      • size

        public static int size​(byte[] buf)
      • writeAddresses

        public static void writeAddresses​(java.util.Collection<? extends Address> v,
                                          java.io.DataOutput out)
                                   throws java.io.IOException
        Writes a list of Addresses. Can contain 65K addresses at most
        Parameters:
        v - A Collection
        out -
        Throws:
        java.lang.Exception
        java.io.IOException
      • writeAddresses

        public static void writeAddresses​(Address[] addrs,
                                          java.io.DataOutput out)
                                   throws java.io.IOException
        Throws:
        java.io.IOException
      • readAddresses

        public static <T extends java.util.Collection<Address>> T readAddresses​(java.io.DataInput in,
                                                                                java.util.function.IntFunction<T> factory)
                                                                         throws java.io.IOException,
                                                                                java.lang.ClassNotFoundException
        Throws:
        java.io.IOException
        java.lang.ClassNotFoundException
      • readAddresses

        public static Address[] readAddresses​(java.io.DataInput in)
                                       throws java.io.IOException,
                                              java.lang.ClassNotFoundException
        Throws:
        java.io.IOException
        java.lang.ClassNotFoundException
      • size

        public static long size​(java.util.Collection<? extends Address> addrs)
        Returns the marshalled size of a Collection of Addresses. Assumes elements are of the same type !
        Parameters:
        addrs - Collection
        Returns:
        long size
      • size

        public static long size​(Address[] addrs)
      • writeStreamable

        public static void writeStreamable​(Streamable obj,
                                           java.io.DataOutput out)
                                    throws java.io.IOException
        Throws:
        java.io.IOException
      • readStreamable

        public static <T extends Streamable> T readStreamable​(java.util.function.Supplier<T> factory,
                                                              java.io.DataInput in)
                                                       throws java.io.IOException,
                                                              java.lang.ClassNotFoundException
        Throws:
        java.io.IOException
        java.lang.ClassNotFoundException
      • writeGenericStreamable

        public static void writeGenericStreamable​(Streamable obj,
                                                  java.io.DataOutput out)
                                           throws java.io.IOException
        Throws:
        java.io.IOException
      • readGenericStreamable

        public static <T extends Streamable> T readGenericStreamable​(java.io.DataInput in)
                                                              throws java.io.IOException,
                                                                     java.lang.ClassNotFoundException
        Throws:
        java.io.IOException
        java.lang.ClassNotFoundException
      • readGenericStreamable

        public static <T extends Streamable> T readGenericStreamable​(java.io.DataInput in,
                                                                     java.lang.ClassLoader loader)
                                                              throws java.io.IOException,
                                                                     java.lang.ClassNotFoundException
        Throws:
        java.io.IOException
        java.lang.ClassNotFoundException
      • write

        public static <T extends Streamable> void write​(T[] array,
                                                        java.io.DataOutput out)
                                                 throws java.io.IOException
        Throws:
        java.io.IOException
      • read

        public static <T extends Streamable> T[] read​(java.lang.Class<T> clazz,
                                                      java.io.DataInput in)
                                               throws java.lang.Exception
        Throws:
        java.lang.Exception
      • writeObject

        public static void writeObject​(java.lang.Object obj,
                                       java.io.DataOutput out)
                                throws java.io.IOException
        Throws:
        java.io.IOException
      • readObject

        public static java.lang.Object readObject​(java.io.DataInput in)
                                           throws java.io.IOException,
                                                  java.lang.ClassNotFoundException
        Throws:
        java.io.IOException
        java.lang.ClassNotFoundException
      • readFile

        public static java.lang.String readFile​(java.lang.String filename)
                                         throws java.io.FileNotFoundException
        Throws:
        java.io.FileNotFoundException
      • readContents

        public static java.lang.String readContents​(java.io.InputStream input)
      • readFileContents

        public static byte[] readFileContents​(java.io.InputStream input)
                                       throws java.io.IOException
        Throws:
        java.io.IOException
      • readToken

        public static java.lang.String readToken​(java.io.InputStream in)
        Returns whitespace-separated strings from the input stream, or null if the end of the stream has been reached
      • readTillMatchingCharacter

        public static java.lang.String readTillMatchingCharacter​(java.io.InputStream in,
                                                                 char c)
                                                          throws java.io.IOException
        Returns all characters read from the current position until the next occurrence of 'c' has been read (including 'c'), or eof, whichever comes first
        Throws:
        java.io.IOException
      • readStringFromStdin

        public static java.lang.String readStringFromStdin​(java.lang.String message)
                                                    throws java.lang.Exception
        Throws:
        java.lang.Exception
      • readLongFromStdin

        public static long readLongFromStdin​(java.lang.String message)
                                      throws java.lang.Exception
        Throws:
        java.lang.Exception
      • readDoubleFromStdin

        public static double readDoubleFromStdin​(java.lang.String message)
                                          throws java.lang.Exception
        Throws:
        java.lang.Exception
      • readIntFromStdin

        public static int readIntFromStdin​(java.lang.String message)
                                    throws java.lang.Exception
        Throws:
        java.lang.Exception
      • writeByteBuffer

        public static void writeByteBuffer​(byte[] buf,
                                           java.io.DataOutput out)
                                    throws java.io.IOException
        Throws:
        java.io.IOException
      • writeByteBuffer

        public static void writeByteBuffer​(byte[] buf,
                                           int offset,
                                           int length,
                                           java.io.DataOutput out)
                                    throws java.io.IOException
        Throws:
        java.io.IOException
      • readByteBuffer

        public static byte[] readByteBuffer​(java.io.DataInput in)
                                     throws java.io.IOException
        Throws:
        java.io.IOException
      • messageToByteBuffer

        public static Buffer messageToByteBuffer​(Message msg)
                                          throws java.io.IOException
        Throws:
        java.io.IOException
      • byteBufferToMessage

        public static Message byteBufferToMessage​(byte[] buffer,
                                                  int offset,
                                                  int length)
                                           throws java.io.IOException,
                                                  java.lang.ClassNotFoundException
        Throws:
        java.io.IOException
        java.lang.ClassNotFoundException
      • match

        public static <T> boolean match​(T obj1,
                                        T obj2)
      • match

        public static boolean match​(long[] a1,
                                    long[] a2)
      • patternMatch

        public static boolean patternMatch​(java.lang.String pattern,
                                           java.lang.String str)
      • productGreaterThan

        public static boolean productGreaterThan​(long n1,
                                                 long n2,
                                                 long val)
      • different

        public static <T> boolean different​(T one,
                                            T two)
      • sleep

        public static void sleep​(long timeout)
        Sleep for timeout msecs. Returns when timeout has elapsed or thread was interrupted
      • sleep

        public static void sleep​(long timeout,
                                 int nanos)
      • keyPress

        public static int keyPress​(java.lang.String msg)
      • available

        public static int available​(java.io.InputStream in)
      • micros

        public static long micros()
      • factorial

        public static int factorial​(int n)
      • permute

        public static <E> void permute​(java.util.List<E> list,
                                       java.util.List<java.util.List<E>> permutations)
        Inefficient permutation of a generic list; uses too much copying among other things. PR welcome! :-)
        Type Parameters:
        E - the type of the list elements
        Parameters:
        list - The list
        permutations - All permutations will be added to this list (as copies)
      • permute

        protected static <E> void permute​(java.util.List<E> prefix,
                                          java.util.List<E> list,
                                          int index,
                                          java.util.List<java.util.List<E>> permutations)
      • car

        public static <E> java.util.List<E> car​(java.util.List<E> l)
      • cdr

        public static <E> java.util.List<E> cdr​(java.util.List<E> l)
      • combine

        public static <E> java.util.List<E> combine​(java.util.List<E> l1,
                                                    java.util.List<E> l2)
      • combine

        @SafeVarargs
        public static <E> E[] combine​(E[]... arrays)
      • swap

        protected static <E> java.util.List<E> swap​(java.util.List<E> l,
                                                    int index)
      • orderedPermutation

        public static <E> java.util.Collection<java.util.List<E>> orderedPermutation​(java.util.List<E> a,
                                                                                     java.util.List<E> b)
        Performs an ordered permutation of the elements of a and b such that the order of elements in list a and b is preserved. Example: {A1,A2} and {B1,B2} -> {A1,B1,A2,B2} but not {A1,B2,B1,A2}
      • permute

        protected static <E> java.util.List<E> permute​(java.util.List<E> l1,
                                                       java.util.List<E> l2,
                                                       int remove_from_l1,
                                                       int remove_from_l2)
      • checkOrder

        @SafeVarargs
        public static <E> boolean checkOrder​(java.util.Collection<E> perm,
                                             java.util.List<E>... lists)
      • index

        protected static <E> int index​(java.util.Collection<E> list,
                                       E el)
      • shuffle

        public static <T> void shuffle​(T[] array,
                                       int from,
                                       int to)
        Reorders elements of an array in-place. No bounds checking is performed. Null elements are shuffled, too
        Type Parameters:
        T - the type of the array's elements
        Parameters:
        array - the array to be shuffled; the array will be modified
        from - the start index inclusive
        to - the end index (exclusive), must be >= from (not checked)
      • random

        public static long random​(long range)
        Returns a random value in the range [1 - range]. If range is 0, 1 will be returned. If range is negative, an exception will be thrown
      • sleepRandom

        public static void sleepRandom​(long floor,
                                       long ceiling)
        Sleeps between floor and ceiling milliseconds, chosen randomly
      • tossWeightedCoin

        public static boolean tossWeightedCoin​(double probability)
        Tosses a coin weighted with probability and returns true or false. Example: if probability=0.8, chances are that in 80% of all cases, true will be returned and false in 20%.
      • dumpThreads

        public static java.lang.String dumpThreads()
      • interruptAndWaitToDie

        public static boolean interruptAndWaitToDie​(java.lang.Thread t)
      • interruptAndWaitToDie

        public static boolean interruptAndWaitToDie​(java.lang.Thread t,
                                                    long timeout)
      • mapToString

        public static java.lang.String mapToString​(java.util.Map<? extends java.lang.Object,​? extends java.lang.Object> map)
      • printNanos

        public static java.lang.String printNanos​(long time_ns)
      • printNanos

        public static java.lang.String printNanos​(double time_ns)
      • printTime

        public static java.lang.String printTime​(long time,
                                                 java.util.concurrent.TimeUnit unit)
      • readBytesLong

        public static long readBytesLong​(java.lang.String input)
      • readBytesInteger

        public static int readBytesInteger​(java.lang.String input)
      • readBytesDouble

        public static double readBytesDouble​(java.lang.String input)
      • printBytes

        public static java.lang.String printBytes​(long bytes)
        MByte nowadays doesn't mean 1024 * 1024 bytes, but 1 million bytes, see http://en.wikipedia.org/wiki/Megabyte
        Parameters:
        bytes -
        Returns:
      • printBytes

        public static java.lang.String printBytes​(double bytes)
      • components

        public static java.lang.String[] components​(java.lang.String path,
                                                    java.lang.String separator)
      • fragmentBuffer

        public static byte[][] fragmentBuffer​(byte[] buf,
                                              int frag_size,
                                              int length)
        Fragments a byte buffer into smaller fragments of (max.) frag_size. Example: a byte buffer of 1024 bytes and a frag_size of 248 gives 4 fragments of 248 bytes each and 1 fragment of 32 bytes.
        Returns:
        An array of byte buffers (byte[]).
      • computeFragOffsets

        public static java.util.List<Range> computeFragOffsets​(int offset,
                                                               int length,
                                                               int frag_size)
        Given a buffer and a fragmentation size, compute a list of fragmentation offset/length pairs, and return them in a list. Example:
        Buffer is 10 bytes, frag_size is 4 bytes. Return value will be ({0,4}, {4,4}, {8,2}). This is a total of 3 fragments: the first fragment starts at 0, and has a length of 4 bytes, the second fragment starts at offset 4 and has a length of 4 bytes, and the last fragment starts at offset 8 and has a length of 2 bytes.
        Parameters:
        frag_size -
        Returns:
        List. A List of offset/length pairs
      • computeFragOffsets

        public static java.util.List<Range> computeFragOffsets​(byte[] buf,
                                                               int frag_size)
      • defragmentBuffer

        public static byte[] defragmentBuffer​(byte[][] fragments)
        Concatenates smaller fragments into entire buffers.
        Parameters:
        fragments - An array of byte buffers (byte[])
        Returns:
        A byte buffer
      • allEqual

        public static <T> boolean allEqual​(java.util.Collection<T> elements)
        Returns true if all elements in the collection are the same
      • printListWithDelimiter

        public static <T> java.lang.String printListWithDelimiter​(java.util.Collection<T> list,
                                                                  java.lang.String delimiter)
      • printListWithDelimiter

        public static <T> java.lang.String printListWithDelimiter​(java.util.Collection<T> list,
                                                                  java.lang.String delimiter,
                                                                  int limit)
      • printListWithDelimiter

        public static <T> java.lang.String printListWithDelimiter​(java.util.Collection<T> list,
                                                                  java.lang.String delimiter,
                                                                  int limit,
                                                                  boolean print_size)
      • printListWithDelimiter

        public static <T> java.lang.String printListWithDelimiter​(T[] list,
                                                                  java.lang.String delimiter,
                                                                  int limit)
      • printMapWithDelimiter

        public static <T> java.lang.String printMapWithDelimiter​(java.util.Map<T,​T> map,
                                                                 java.lang.String delimiter)
      • leftMembers

        public static java.util.List<Address> leftMembers​(java.util.Collection<Address> old_list,
                                                          java.util.Collection<Address> new_list)
      • newMembers

        public static java.util.List<Address> newMembers​(java.util.List<Address> old_list,
                                                         java.util.List<Address> new_list)
      • newElements

        public static <T> java.util.List<T> newElements​(java.util.List<T> old_list,
                                                        java.util.List<T> new_list)
      • contains

        public static <T> boolean contains​(T key,
                                           T[] list)
      • containsViewId

        public static boolean containsViewId​(java.util.Collection<View> views,
                                             ViewId vid)
      • containsId

        public static boolean containsId​(short id,
                                         short[] ids)
      • detectDifferentViews

        public static java.util.List<View> detectDifferentViews​(java.util.Map<Address,​View> map)
      • determineMergeParticipants

        public static java.util.Collection<Address> determineMergeParticipants​(java.util.Map<Address,​View> map)
        Determines the members which take part in a merge. The resulting list consists of all merge coordinators plus members outside a merge partition, e.g. for views A={B,A,C}, B={B,C} and C={B,C}, the merge coordinator is B, but the merge participants are B and A.
        Parameters:
        map -
        Returns:
      • determineMergeCoords

        public static java.util.Collection<Address> determineMergeCoords​(java.util.Map<Address,​View> map)
        This is the same or a subset of determineMergeParticipants(java.util.Map) and contains only members which are currently sub-partition coordinators.
        Parameters:
        map -
        Returns:
      • determineActualMergeCoords

        public static java.util.Collection<Address> determineActualMergeCoords​(java.util.Map<Address,​View> map)
        Similar to determineMergeCoords(java.util.Map) but only actual coordinators are counted: an actual coord is when the sender of a view is the first member of that view
      • getRank

        public static int getRank​(View view,
                                  Address addr)
        Returns the rank of a member in a given view
        Parameters:
        view - The view
        addr - The address of a member
        Returns:
        A value between 1 and view.size(). The first member has rank 1, the second 2 and so on. If the member is not found, 0 is returned
      • getRank

        public static int getRank​(java.util.Collection<Address> members,
                                  Address addr)
      • pickRandomElement

        public static <T> T pickRandomElement​(java.util.List<T> list)
      • pickRandomElement

        public static <T> T pickRandomElement​(java.util.Set<T> set)
      • pickRandomElement

        public static <T> T pickRandomElement​(T[] array)
      • pickNext

        public static <T> T pickNext​(java.util.List<T> list,
                                     T obj)
      • pickNext

        public static <T> java.util.List<T> pickNext​(java.util.List<T> list,
                                                     T obj,
                                                     int num)
        Returns the next min(N,list.size()) elements after obj
      • generateArray

        public static byte[] generateArray​(int size)
      • createRandomAddress

        public static Address createRandomAddress()
      • createRandomAddresses

        public static Address[] createRandomAddresses​(int num)
        Returns an array of num random addresses, named A, B, C etc
      • createRandomAddresses

        public static Address[] createRandomAddresses​(int num,
                                                      boolean use_numbers)
      • createRandomAddress

        public static Address createRandomAddress​(java.lang.String name)
      • createTimer

        public static java.lang.Object[][] createTimer()
      • determineLeftMembers

        public static java.util.List<Address> determineLeftMembers​(java.util.List<Address> old_mbrs,
                                                                   java.util.List<Address> new_mbrs)
        Returns all members that left between 2 views. All members that are element of old_mbrs but not element of new_mbrs are returned.
      • print

        public static <T> java.lang.String print​(java.util.Collection<T> objs)
      • print

        public static java.lang.String print​(java.lang.Object[] objs)
      • print

        public static <T> java.lang.String print​(java.util.Map<T,​T> map)
      • printPingData

        public static java.lang.String printPingData​(java.util.List<PingData> rsps)
      • print

        public static java.lang.String print​(java.nio.ByteBuffer buf)
      • loadClass

        public static java.lang.Class loadClass​(java.lang.String classname,
                                                java.lang.Class clazz)
                                         throws java.lang.ClassNotFoundException
        Tries to load the class from the current thread's context class loader. If not successful, tries to load the class from the current instance.
        Parameters:
        classname - Desired class.
        clazz - Class object used to obtain a class loader if no context class loader is available.
        Returns:
        Class, or null on failure.
        Throws:
        java.lang.ClassNotFoundException
      • loadClass

        public static java.lang.Class<?> loadClass​(java.lang.String classname,
                                                   java.lang.ClassLoader preferredLoader)
                                            throws java.lang.ClassNotFoundException
        Tries to load the class from the preferred loader. If not successful, tries to load the class from the current thread's context class loader or system class loader.
        Parameters:
        classname - Desired class name.
        preferredLoader - The preferred class loader
        Returns:
        the loaded class.
        Throws:
        java.lang.ClassNotFoundException - if the class could not be loaded by any loader
      • loadProtocolClass

        public static java.lang.Class<? extends Protocol> loadProtocolClass​(java.lang.String protocol_name,
                                                                            java.lang.Class<?> cl)
                                                                     throws java.lang.Exception
        Throws:
        java.lang.Exception
      • getAllDeclaredFieldsWithAnnotations

        @SafeVarargs
        public static java.lang.reflect.Field[] getAllDeclaredFieldsWithAnnotations​(java.lang.Class clazz,
                                                                                    java.lang.Class<? extends java.lang.annotation.Annotation>... annotations)
      • forAllFieldsAndMethods

        public static void forAllFieldsAndMethods​(java.lang.Object obj,
                                                  java.util.function.Predicate<? super java.lang.reflect.AccessibleObject> filter,
                                                  java.util.function.BiConsumer<java.lang.reflect.Field,​java.lang.Object> field_func,
                                                  java.util.function.BiConsumer<java.lang.reflect.Method,​java.lang.Object> method_func)
        Applies a function against all fields and methods of a given target object which satisfy a given predicate.
        Parameters:
        obj - The target object
        filter - The filter. Needs to be able to handle Fields and Methods (superclass: AccessibleObject). If null, all fields/methods will be selected
        field_func - The function to be applied to all found fields. No-op if null.
        method_func - The function to be applied to all found methods. No-op if null.
      • getNameFromAnnotation

        public static java.lang.String getNameFromAnnotation​(java.lang.reflect.AccessibleObject obj)
      • getAllDeclaredMethodsWithAnnotations

        @SafeVarargs
        public static java.lang.reflect.Method[] getAllDeclaredMethodsWithAnnotations​(java.lang.Class clazz,
                                                                                      java.lang.Class<? extends java.lang.annotation.Annotation>... annotations)
      • getAnnotation

        public static <A extends java.lang.annotation.Annotation> A getAnnotation​(java.lang.Class<?> clazz,
                                                                                  java.lang.Class<A> annotationClass)
      • getField

        public static java.lang.reflect.Field getField​(java.lang.Class clazz,
                                                       java.lang.String field_name)
      • getField

        public static java.lang.reflect.Field getField​(java.lang.Class clazz,
                                                       java.lang.String field_name,
                                                       boolean throw_exception)
                                                throws java.lang.NoSuchFieldException
        Throws:
        java.lang.NoSuchFieldException
      • setField

        public static void setField​(java.lang.reflect.Field field,
                                    java.lang.Object target,
                                    java.lang.Object value)
      • getField

        public static java.lang.Object getField​(java.lang.reflect.Field field,
                                                java.lang.Object target)
      • findField

        public static java.lang.reflect.Field findField​(java.lang.Object target,
                                                        java.util.List<java.lang.String> possible_names)
      • findMethod

        public static java.lang.reflect.Method findMethod​(java.lang.Object target,
                                                          java.util.List<java.lang.String> possible_names,
                                                          java.lang.Class<?>... parameter_types)
      • findMethod

        public static java.lang.reflect.Method findMethod​(java.lang.Class<?> root_class,
                                                          java.util.List<java.lang.String> possible_names,
                                                          java.lang.Class<?>... parameter_types)
      • findClassesAssignableFrom

        public static <T> java.util.Set<java.lang.Class<T>> findClassesAssignableFrom​(java.lang.String packageName,
                                                                                      java.lang.Class<T> assignableFrom)
                                                                               throws java.io.IOException,
                                                                                      java.lang.ClassNotFoundException
        Throws:
        java.io.IOException
        java.lang.ClassNotFoundException
      • findClassesAnnotatedWith

        public static java.util.List<java.lang.Class<?>> findClassesAnnotatedWith​(java.lang.String packageName,
                                                                                  java.lang.Class<? extends java.lang.annotation.Annotation> a)
                                                                           throws java.io.IOException,
                                                                                  java.lang.ClassNotFoundException
        Throws:
        java.io.IOException
        java.lang.ClassNotFoundException
      • getResourceAsStream

        public static java.io.InputStream getResourceAsStream​(java.lang.String resourcePath,
                                                              java.lang.ClassLoader userClassLoader)
      • getResourceAsStream

        public static java.io.InputStream getResourceAsStream​(java.lang.String name,
                                                              java.lang.Class clazz)
      • getChild

        public static java.lang.String getChild​(org.w3c.dom.Element root,
                                                java.lang.String path)
      • sameHost

        public static boolean sameHost​(Address one,
                                       Address two)
        Checks whether 2 Addresses are on the same host
      • parseRejectionPolicy

        public static java.util.concurrent.RejectedExecutionHandler parseRejectionPolicy​(java.lang.String rejection_policy)
      • parseCommaDelimitedInts

        public static int[] parseCommaDelimitedInts​(java.lang.String s)
        Parses comma-delimited longs; e.g., 2000,4000,8000. Returns array of long, or null.
      • parseCommaDelimitedLongs

        public static long[] parseCommaDelimitedLongs​(java.lang.String s)
        Parses comma-delimited longs; e.g., 2000,4000,8000. Returns array of long, or null.
      • parseCommaDelimitedStrings

        public static java.util.List<java.lang.String> parseCommaDelimitedStrings​(java.lang.String l)
        e.g. "bela,jeannette,michelle" --> List{"bela", "jeannette", "michelle"}
      • parseCommaDelimitedHosts

        public static java.util.List<PhysicalAddress> parseCommaDelimitedHosts​(java.lang.String hosts,
                                                                               int port_range)
                                                                        throws java.net.UnknownHostException
        Input is "daddy[8880],sindhu[8880],camille[5555]. Returns a list of IpAddresses
        Throws:
        java.net.UnknownHostException
      • parseCommaDelimitedHostsInto

        public static boolean parseCommaDelimitedHostsInto​(java.util.Collection<PhysicalAddress> list,
                                                           java.util.Collection<java.lang.String> unresolved_hosts,
                                                           java.lang.String hosts,
                                                           int port_range)
        Parses a string into a list of IpAddresses
        Parameters:
        list - The list to which to add parsed elements
        hosts - The string with host:port pairs
        unresolved_hosts - A list of unresolved hosts
        port_range - The port range to consider
        Returns:
        True if all hostnames resolved fine, false otherwise
      • parseCommaDelimitedHosts2

        public static java.util.List<java.net.InetSocketAddress> parseCommaDelimitedHosts2​(java.lang.String hosts,
                                                                                           int port_range)
                                                                                    throws java.net.UnknownHostException
        Input is "daddy[8880],sindhu[8880],camille[5555]. Return List of InetSocketAddress
        Throws:
        java.net.UnknownHostException
      • parseStringList

        public static java.util.List<java.lang.String> parseStringList​(java.lang.String l,
                                                                       java.lang.String separator)
      • parseCommaDelimitedProps

        public static java.util.Map<java.lang.String,​java.lang.String> parseCommaDelimitedProps​(java.lang.String s)
      • discardUntilNewLine

        public static int discardUntilNewLine​(java.io.InputStream in)
        Reads and discards all characters from the input stream until a \r\n or EOF is encountered
        Parameters:
        in -
        Returns:
      • readLine

        public static java.lang.String readLine​(java.io.InputStream in)
                                         throws java.io.IOException
        Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.
        Returns:
        A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached
        Throws:
        java.io.IOException - If an I/O error occurs
      • parseInterfaceList

        public static java.util.List<java.net.NetworkInterface> parseInterfaceList​(java.lang.String s)
                                                                            throws java.lang.Exception
        Parameters:
        s -
        Returns:
        List
        Throws:
        java.lang.Exception
      • print

        public static java.lang.String print​(java.util.List<java.net.NetworkInterface> interfaces)
      • shortName

        public static java.lang.String shortName​(java.lang.String hostname)
      • startFlush

        public static boolean startFlush​(JChannel c,
                                         java.util.List<Address> flushParticipants,
                                         int numberOfAttempts,
                                         long randomSleepTimeoutFloor,
                                         long randomSleepTimeoutCeiling)
        Performs the flush of the given channel for the specified flush participants and the given number of attempts along with random sleep time after each such attempt.
        Parameters:
        c - the channel
        flushParticipants - the flush participants in this flush attempt
        numberOfAttempts - the number of flush attempts
        randomSleepTimeoutFloor - the minimum sleep time between attempts in ms
        randomSleepTimeoutCeiling - the maximum sleep time between attempts in ms
        Returns:
        true if channel was flushed successfully, false otherwise
        See Also:
        JChannel.startFlush(List,boolean)
      • startFlush

        public static boolean startFlush​(JChannel c,
                                         java.util.List<Address> flushParticipants)
        Performs the flush of the given channel and the specified flush participants
        Parameters:
        c - the channel
        flushParticipants - the flush participants in this flush attempt
        See Also:
        JChannel.startFlush(List,boolean)
      • startFlush

        public static boolean startFlush​(JChannel c,
                                         int numberOfAttempts,
                                         long randomSleepTimeoutFloor,
                                         long randomSleepTimeoutCeiling)
        Performs the flush of the given channel within the specfied number of attempts along with random sleep time after each such attempt.
        Parameters:
        c - the channel
        numberOfAttempts - the number of flush attempts
        randomSleepTimeoutFloor - the minimum sleep time between attempts in ms
        randomSleepTimeoutCeiling - the maximum sleep time between attempts in ms
        Returns:
        true if channel was flushed successfully, false otherwise
        See Also:
        JChannel.startFlush(boolean)
      • startFlush

        public static boolean startFlush​(JChannel c)
        Performs the flush of the given channel
        Parameters:
        c - the channel
        Returns:
        true if channel was flushed successfully, false otherwise
        See Also:
        JChannel.startFlush(boolean)
      • shortName

        public static java.lang.String shortName​(java.net.InetAddress hostname)
      • generateLocalName

        public static java.lang.String generateLocalName()
      • createConcurrentMap

        public static <K,​V> java.util.concurrent.ConcurrentMap<K,​V> createConcurrentMap​(int initial_capacity,
                                                                                                    float load_factor,
                                                                                                    int concurrency_level)
      • createConcurrentMap

        public static <K,​V> java.util.concurrent.ConcurrentMap<K,​V> createConcurrentMap​(int initial_capacity)
      • createConcurrentMap

        public static <K,​V> java.util.concurrent.ConcurrentMap<K,​V> createConcurrentMap()
      • createServerSocket

        public static java.net.ServerSocket createServerSocket​(SocketFactory factory,
                                                               java.lang.String service_name,
                                                               java.net.InetAddress bind_addr,
                                                               int start_port)
      • createServerSocket

        public static java.net.ServerSocket createServerSocket​(SocketFactory factory,
                                                               java.lang.String service_name,
                                                               java.net.InetAddress bind_addr,
                                                               int start_port,
                                                               int end_port)
                                                        throws java.lang.Exception
        Throws:
        java.lang.Exception
      • createServerSocketAndBind

        public static java.net.ServerSocket createServerSocketAndBind​(SocketFactory factory,
                                                                      java.lang.String service_name,
                                                                      java.net.InetAddress bind_addr,
                                                                      int start_port,
                                                                      int end_port)
                                                               throws java.lang.Exception
        Finds first available port starting at start_port and returns server socket. Will not bind to port >end_port. Sets srv_port
        Throws:
        java.lang.Exception
      • bind

        public static void bind​(java.net.ServerSocket srv_sock,
                                java.net.InetAddress bind_addr,
                                int start_port,
                                int end_port)
                         throws java.lang.Exception
        Throws:
        java.lang.Exception
      • bind

        public static void bind​(java.net.ServerSocket srv_sock,
                                java.net.InetAddress bind_addr,
                                int start_port,
                                int end_port,
                                int backlog)
                         throws java.lang.Exception
        Throws:
        java.lang.Exception
      • bind

        public static void bind​(java.net.Socket sock,
                                java.net.InetAddress bind_addr,
                                int start_port,
                                int end_port)
                         throws java.lang.Exception
        Throws:
        java.lang.Exception
      • createServerSocketChannelAndBind

        public static java.nio.channels.ServerSocketChannel createServerSocketChannelAndBind​(java.net.InetAddress bind_addr,
                                                                                             int start_port,
                                                                                             int end_port)
                                                                                      throws java.lang.Exception
        Throws:
        java.lang.Exception
      • createServerSocketChannel

        public static java.nio.channels.ServerSocketChannel createServerSocketChannel​(SocketFactory factory,
                                                                                      java.lang.String service_name,
                                                                                      java.net.InetAddress bind_addr,
                                                                                      int start_port,
                                                                                      int end_port)
                                                                               throws java.lang.Exception
        Throws:
        java.lang.Exception
      • bind

        public static void bind​(java.nio.channels.ServerSocketChannel ch,
                                java.net.InetAddress bind_addr,
                                int start_port,
                                int end_port)
                         throws java.lang.Exception
        Throws:
        java.lang.Exception
      • bind

        public static void bind​(java.nio.channels.ServerSocketChannel ch,
                                java.net.InetAddress bind_addr,
                                int start_port,
                                int end_port,
                                int backlog)
                         throws java.lang.Exception
        Throws:
        java.lang.Exception
      • createDatagramSocket

        public static java.net.DatagramSocket createDatagramSocket​(SocketFactory factory,
                                                                   java.lang.String service_name,
                                                                   java.net.InetAddress addr,
                                                                   int port)
                                                            throws java.lang.Exception
        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.
        Parameters:
        addr - The InetAddress to which the socket should be bound. If null, the socket will not be bound.
        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.
        Throws:
        java.lang.Exception
      • createMulticastSocket

        public static java.net.MulticastSocket createMulticastSocket​(SocketFactory factory,
                                                                     java.lang.String service_name,
                                                                     java.net.InetAddress mcast_addr,
                                                                     int port,
                                                                     Log log)
                                                              throws java.io.IOException
        Throws:
        java.io.IOException
      • validateBindAddressFromInterface

        public static java.net.InetAddress validateBindAddressFromInterface​(java.net.InetAddress bind_addr,
                                                                            java.lang.String bind_interface_str,
                                                                            StackType ip_version)
                                                                     throws java.net.UnknownHostException,
                                                                            java.net.SocketException
        Method used by PropertyConverters.BindInterface to check that a bind_addr is consistent with a specified interface

        Idea: 1. We are passed a bind_addr, which may be null 2. If non-null, check that bind_addr is on bind_interface - if not, throw exception, otherwise, return the original bind_addr 3. If null, get first non-loopback address on bind_interface, using stack preference to get the IP version. If no non-loopback address, then just return null (i.e. bind_interface did not influence the decision).

        Throws:
        java.net.UnknownHostException
        java.net.SocketException
      • checkForLinux

        public static boolean checkForLinux()
      • checkForHp

        public static boolean checkForHp()
      • checkForSolaris

        public static boolean checkForSolaris()
      • checkForWindows

        public static boolean checkForWindows()
      • checkForAndroid

        public static boolean checkForAndroid()
      • checkForMac

        public static boolean checkForMac()
      • getLoopback

        public static java.net.InetAddress getLoopback​(StackType ip_version)
                                                throws java.net.UnknownHostException
        IP related utilities
        Throws:
        java.net.UnknownHostException
      • getLoopback

        public static java.net.InetAddress getLoopback()
                                                throws java.net.UnknownHostException
        Throws:
        java.net.UnknownHostException
      • getLocalMulticastAddress

        public static java.net.InetAddress getLocalMulticastAddress​(StackType ip_version)
                                                             throws java.net.UnknownHostException
        Throws:
        java.net.UnknownHostException
      • getNonLoopbackAddress

        public static java.net.InetAddress getNonLoopbackAddress()
                                                          throws java.net.SocketException
        Returns the first non-loopback address on any interface on the current host
        Throws:
        java.net.SocketException
      • getNonLoopbackAddress

        public static java.net.InetAddress getNonLoopbackAddress​(StackType ip_version)
                                                          throws java.net.SocketException
        Throws:
        java.net.SocketException
      • getByName

        public static java.net.NetworkInterface getByName​(java.lang.String name)
                                                   throws java.net.SocketException
        Finds a network interface or sub-interface with the given name
        Throws:
        java.net.SocketException
      • getByName

        public static java.net.InetAddress getByName​(java.lang.String host,
                                                     StackType ip_version)
                                              throws java.net.UnknownHostException
        Finds an address given a symbolic name. Parameter ip_version has precedence over system props such as java.net.preferIPv4Stack or java.net.preferIPv6Addresses
        Parameters:
        host - The symbolic nbame of the host
        ip_version - The IP version, e.g. StackType.IPv4 or StackType.IPv6
        Returns:
        The resolved address
        Throws:
        java.net.UnknownHostException - Thrown if host cannot be resolved to an InetAddress
      • getAddress

        public static java.net.InetAddress getAddress​(java.lang.String value,
                                                      StackType ip_version)
                                               throws java.lang.Exception
        Throws:
        java.lang.Exception
      • getAddress

        public static java.net.InetAddress getAddress​(Util.AddressScope scope,
                                                      StackType ip_version)
                                               throws java.net.SocketException
        Returns the first address on any interface which satisfies scope and ip_version. If ip_version is Dual, then IPv4 addresses are favored
        Throws:
        java.net.SocketException
      • getAddress

        public static java.net.InetAddress getAddress​(java.net.NetworkInterface intf,
                                                      Util.AddressScope scope,
                                                      StackType ip_version)
        Returns the first address on the given interface on the current host, which satisfies scope
        Parameters:
        intf - the interface to be checked
      • match

        public static boolean match​(java.net.InetAddress addr,
                                    Util.AddressScope scope)
      • getAddressByPatternMatch

        public static java.net.InetAddress getAddressByPatternMatch​(java.lang.String pattern,
                                                                    StackType ip_version)
                                                             throws java.lang.Exception
        Returns a valid interface address based on a pattern. Iterates over all interfaces that are up and returns the first match, based on the address or interface name
        Parameters:
        pattern - Can be "match-addr:" or "match-interface:". Example:

        match-addr:192.168.*

        Returns:
        InetAddress or null if not found
        Throws:
        java.lang.Exception
      • getAddressByCustomCode

        public static java.net.InetAddress getAddressByCustomCode​(java.lang.String value)
                                                           throws java.lang.Exception
        Throws:
        java.lang.Exception
      • isUp

        public static boolean isUp​(java.net.NetworkInterface ni)
                            throws java.net.SocketException
        Always returns true unless there is a socket exception - will be removed when GraalVM issue https://github.com/oracle/graal/pull/1076 has been fixed
        Throws:
        java.net.SocketException
      • interfaceHasIPAddresses

        public static boolean interfaceHasIPAddresses​(java.net.NetworkInterface intf,
                                                      StackType ip_version)
                                               throws java.net.UnknownHostException
        A function to check if an interface supports an IP version (i.e has addresses defined for that IP version).
        Parameters:
        intf -
        Returns:
        Throws:
        java.net.UnknownHostException
      • getIpStackType

        public static StackType getIpStackType()
      • isIpv4StackAvailable

        public static boolean isIpv4StackAvailable()
      • isIpv6StackAvailable

        public static boolean isIpv6StackAvailable()
      • isIpAddressPermissible

        public static boolean isIpAddressPermissible​(java.net.InetAddress addr)
      • sameAddresses

        public static boolean sameAddresses​(java.net.InetAddress one,
                                            java.net.InetAddress two)
        Returns true if the 2 addresses are of the same type (IPv4 or IPv6)
      • isStackAvailable

        public static boolean isStackAvailable​(boolean ipv4)
      • getAllAvailableInterfaces

        public static java.util.List<java.net.NetworkInterface> getAllAvailableInterfaces()
                                                                                   throws java.net.SocketException
        Throws:
        java.net.SocketException
      • getAllAvailableAddresses

        public static java.util.Collection<java.net.InetAddress> getAllAvailableAddresses​(java.util.function.Predicate<java.net.InetAddress> filter)
        Returns all addresses of all interfaces (that are up) that satisfy a given filter (ignored if null)
      • checkIfValidAddress

        public static void checkIfValidAddress​(java.net.InetAddress bind_addr,
                                               java.lang.String prot_name)
                                        throws java.lang.Exception
        Throws:
        java.lang.Exception
      • convertToIPv6

        public static java.net.InetAddress convertToIPv6​(java.net.InetAddress ipv4_addr)
                                                  throws java.net.UnknownHostException
        Throws:
        java.net.UnknownHostException
      • getProperty

        public static java.lang.String getProperty​(java.lang.String[] system_props,
                                                   java.util.Properties props,
                                                   java.lang.String prop_name,
                                                   java.lang.String default_value)
        Returns a value associated wither with one or more system properties, or found in the props map
        Parameters:
        system_props -
        props - List of properties read from the configuration file
        prop_name - The name of the property, will be removed from props if found
        default_value - Used to return a default value if the properties or system properties didn't have the value
        Returns:
        The value, or null if not found
      • isCoordinator

        public static boolean isCoordinator​(JChannel ch)
      • isCoordinator

        public static boolean isCoordinator​(View view,
                                            Address local_addr)
      • getMBeanServer

        public static javax.management.MBeanServer getMBeanServer()
      • registerChannel

        public static void registerChannel​(JChannel channel,
                                           java.lang.String name)
      • replaceProperties

        @Deprecated
        public static java.lang.String replaceProperties​(java.lang.String string,
                                                         java.util.Properties props)
        Go through the input string and replace any occurance of ${p} with the props.getProperty(p) value. If there is no such property p defined, then the ${p} reference will remain unchanged.

        If the property reference is of the form ${p:v} and there is no such property p, then the default value v will be returned.

        If the property reference is of the form ${p1,p2} or ${p1,p2:v} then the primary and the secondary properties will be tried in turn, before returning either the unchanged input, or the default value.

        The property ${/} is replaced with System.getProperty("file.separator") value and the property ${:} is replaced with System.getProperty("path.separator").

        Parameters:
        string - the string with possible ${} references
        props - the source for ${x} property ref values, null means use System.getProperty()
        Returns:
        the input string with all property references replaced if any. If there are no valid references the input string will be returned.
      • append

        protected static void append​(java.lang.StringBuilder sb,
                                     java.lang.String str)
      • substituteVariable

        public static java.lang.String substituteVariable​(java.lang.String val)
        Replaces variables of ${var:default} with System.getProperty(var, default). If no variables are found, returns the same string, otherwise a copy of the string with variables substituted
        Parameters:
        val -
        Returns:
        A string with vars replaced, or the same string if no vars found
      • substituteVariable

        public static java.lang.String substituteVariable​(java.lang.String val,
                                                          java.util.Properties p)
        Replaces variables of ${var:default} with Properties then uses System.getProperty(var, default) if the value was not found. If no variables are found, returns the same string, otherwise a copy of the string with variables substituted
        Parameters:
        val -
        p -
        Returns:
        A string with vars replaced, or the same string if no vars found
      • getProperty

        public static java.lang.String getProperty​(java.lang.String s,
                                                   java.util.Properties p)
      • getProperty

        public static java.lang.String getProperty​(java.lang.String s)
      • md5

        public static java.lang.String md5​(java.lang.String source)
        Converts a java.lang.String in to a MD5 hashed String
        Parameters:
        source - the source String
        Returns:
        the MD5 hashed version of the string
      • sha

        public static java.lang.String sha​(java.lang.String source)
        Converts a java.lang.String in to a SHA hashed String
        Parameters:
        source - the source String
        Returns:
        the MD5 hashed version of the string
      • methodNameToAttributeName

        public static java.lang.String methodNameToAttributeName​(java.lang.String methodName)
        Converts a method name to an attribute name, e.g. getFooBar() --> foo_bar, isFlag --> flag
      • methodNameToJavaAttributeName

        public static java.lang.String methodNameToJavaAttributeName​(java.lang.String methodName)
        Converts a method name to a Java attribute name, e.g. getFooBar() --> fooBar, isFlag --> flag.
        Parameters:
        methodName -
        Returns:
      • attributeNameToMethodName

        public static java.lang.String attributeNameToMethodName​(java.lang.String attr_name)