org.jgroups.util
Class Util
public
class
Util
extends Object
Collection of various utility routines that can not be assigned to other classes.
Method Summary |
static String | activeThreads() |
static String | array2String(long[] array) |
static String | array2String(int[] array) |
static String | array2String(boolean[] array) |
static LinkedList | byteBufferToMessageList(byte[] buffer, int offset, int length) |
static boolean | checkForLinux() |
static boolean | checkForSolaris() |
static boolean | checkForWindows() |
static void | closeInputStream(InputStream inp) |
static void | closeOutputStream(OutputStream out) |
static byte[] | collectionToByteBuffer(Collection c) |
static List | 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 List | computeFragOffsets(byte[] buf, int frag_size) |
static void | crash() |
static DatagramSocket | createDatagramSocket(InetAddress addr, int port)
Creates a DatagramSocket bound to addr. |
static ServerSocket | createServerSocket(int start_port) Finds first available port starting at start_port and returns server socket |
static ServerSocket | createServerSocket(InetAddress bind_addr, int start_port) |
static byte[] | defragmentBuffer(byte[][] fragments)
Concatenates smaller fragments into entire buffers. |
static Vector | determineLeftMembers(Vector old_mbrs, Vector new_mbrs)
Returns all members that left between 2 views. |
static void | doubleWrite(byte[] buf, OutputStream out)
Makes sure that we detect when a peer connection is in the closed state (not closed while we send data,
but before we send data). |
static void | doubleWrite(byte[] buf, int offset, int length, OutputStream out)
Makes sure that we detect when a peer connection is in the closed state (not closed while we send data,
but before we send data). |
static String | dumpQueue(Queue q)
Debugging method used to dump the content of a protocol queue in a condensed form. |
static void | dumpStack(boolean exit) |
static boolean | fileExists(String fname) |
static byte[][] | fragmentBuffer(byte[] buf, int frag_size, int length)
Fragments a byte buffer into smaller fragments of (max.) frag_size.
|
static byte[][] | fragmentBuffer(byte[] buf, int frag_size) |
static List | getAllAvailableInterfaces() |
static InetAddress | getFirstNonLoopbackAddress() |
static String | getHostname() |
static int | getJavaVersion() |
static InputStream | getResourceAsStream(String name, Class clazz) |
static String | getStackTrace(Throwable t) |
static boolean | isBindAddressPropertyIgnored() |
static Class | loadClass(String classname, Class clazz)
Tries to load the class from the current thread's context class loader. |
static void | main(String[] args) |
static boolean | match(Object obj1, Object obj2) |
static boolean | match(long[] a1, long[] a2) |
static String | memStats(boolean gc) |
static Buffer | msgListToByteBuffer(LinkedList xmit_list)
Marshalls a list of messages. |
static Object | objectFromByteBuffer(byte[] buffer)
Creates an object from a byte buffer |
static byte[] | objectToByteBuffer(Object obj)
Serializes an object into a byte buffer.
|
static long[] | parseCommaDelimitedLongs(String s)
Parses comma-delimited longs; e.g., 2000,4000,8000.
|
static List | parseCommaDelimitedStrings(String l) e.g. |
static Object | pickRandomElement(Vector list) |
static Vector | pickSubset(Vector members, double subset_percentage)
Selects a random subset of members according to subset_percentage and returns them.
|
static String | print(Throwable t) |
static String | printBytes(long bytes) |
static String | printEvent(Event evt) |
static void | printFragments(byte[][] frags) |
static String | printMembers(Vector v) |
static String | printMessage(Message msg) Tries to read an object from the message's buffer and prints it |
static String | printMethodCall(Message msg) Tries to read a MethodCall object from the message's buffer and prints it.
|
static String | printStackTrace(Throwable t)
Use with caution: lots of overhead |
static void | printThreads() |
static void | prompt(String s) |
static long | random(long range) Returns a random value in the range [1 - range] |
static Address | readAddress(DataInputStream in) |
static Collection | readAddresses(DataInputStream in, Class cl)
|
static byte[] | readByteBuffer(DataInputStream in) |
static Streamable | readGenericStreamable(DataInputStream in) |
static Object | readObject(DataInputStream in) |
static Streamable | readStreamable(Class clazz, DataInputStream in) |
static String | readString(DataInputStream in) |
static boolean | sameHost(Address one, Address two) Checks whether 2 Addresses are on the same host |
static String | shortName(String hostname) |
static String | shortName(InetAddress hostname) |
static int | size(Address addr) |
static long | size(Collection addrs)
Returns the marshalled size of a Collection of Addresses.
|
static long | sizeOf(String classname) |
static long | sizeOf(Object inst) |
static long | sizeOf(Streamable inst) |
static void | sleep(long timeout) Sleep for timeout msecs. |
static void | sleep(long msecs, boolean busy_sleep)
On most UNIX systems, the minimum sleep time is 10-20ms. |
static void | sleepRandom(long timeout) Sleeps between 1 and timeout milliseconds, chosen randomly. |
static Streamable | streamableFromByteBuffer(Class cl, byte[] buffer) |
static byte[] | streamableToByteBuffer(Streamable obj) |
static boolean | tossWeightedCoin(double probability)
Tosses a coin weighted with probability and returns true or false. |
static void | writeAddress(Address addr, DataOutputStream out) |
static void | writeAddresses(Collection v, DataOutputStream out)
Writes a Vector of Addresses. |
static void | writeByteBuffer(byte[] buf, DataOutputStream out) |
static void | writeFully(ByteBuffer buf, WritableByteChannel out)
if we were to register for OP_WRITE and send the remaining data on
readyOps for this channel we have to either block the caller thread or
queue the message buffers that may arrive while waiting for OP_WRITE.
|
static void | writeGenericStreamable(Streamable obj, DataOutputStream out) |
static void | writeObject(Object obj, DataOutputStream out) |
static void | writeStreamable(Streamable obj, DataOutputStream out) |
static void | writeString(String s, DataOutputStream out) |
public static final String DIAG_GROUP
public static final int MAX_PORT
public static String activeThreads()
public static String array2String(long[] array)
public static String array2String(int[] array)
public static String array2String(boolean[] array)
public static LinkedList byteBufferToMessageList(byte[] buffer, int offset, int length)
public static boolean checkForLinux()
public static boolean checkForSolaris()
public static boolean checkForWindows()
public static void closeInputStream(InputStream inp)
public static void closeOutputStream(OutputStream out)
public static byte[] collectionToByteBuffer(Collection c)
public static List 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
public static List computeFragOffsets(byte[] buf, int frag_size)
public static void crash()
public static DatagramSocket createDatagramSocket(InetAddress addr, int port)
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.
public static ServerSocket createServerSocket(int start_port)
Finds first available port starting at start_port and returns server socket
public static ServerSocket createServerSocket(InetAddress bind_addr, int start_port)
public static byte[] defragmentBuffer(byte[][] fragments)
Concatenates smaller fragments into entire buffers.
Parameters: fragments An array of byte buffers (byte[]
)
Returns: A byte buffer
public static Vector determineLeftMembers(Vector old_mbrs, Vector 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.
public static void doubleWrite(byte[] buf, OutputStream out)
Makes sure that we detect when a peer connection is in the closed state (not closed while we send data,
but before we send data). Two writes ensure that, if the peer closed the connection, the first write
will send the peer from FIN to RST state, and the second will cause a signal (IOException).
public static void doubleWrite(byte[] buf, int offset, int length, OutputStream out)
Makes sure that we detect when a peer connection is in the closed state (not closed while we send data,
but before we send data). Two writes ensure that, if the peer closed the connection, the first write
will send the peer from FIN to RST state, and the second will cause a signal (IOException).
public static String dumpQueue(
Queue q)
Debugging method used to dump the content of a protocol queue in a condensed form. Useful
to follow the evolution of the queue's content in time.
public static void dumpStack(boolean exit)
public static boolean fileExists(String fname)
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[]
).
public static byte[][] fragmentBuffer(byte[] buf, int frag_size)
public static List getAllAvailableInterfaces()
public static InetAddress getFirstNonLoopbackAddress()
public static String getHostname()
public static int getJavaVersion()
public static InputStream getResourceAsStream(String name, Class clazz)
public static String getStackTrace(Throwable t)
public static boolean isBindAddressPropertyIgnored()
public static Class loadClass(String classname, Class clazz)
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.
public static void main(String[] args)
public static boolean match(Object obj1, Object obj2)
public static boolean match(long[] a1, long[] a2)
public static String memStats(boolean gc)
public static
Buffer msgListToByteBuffer(LinkedList xmit_list)
Marshalls a list of messages.
Parameters: xmit_list LinkedList
Returns: Buffer
Throws: IOException
public static Object objectFromByteBuffer(byte[] buffer)
Creates an object from a byte buffer
public static byte[] objectToByteBuffer(Object obj)
Serializes an object into a byte buffer.
The object has to implement interface Serializable or Externalizable
public static long[] parseCommaDelimitedLongs(String s)
Parses comma-delimited longs; e.g., 2000,4000,8000.
Returns array of long, or null.
public static List parseCommaDelimitedStrings(String l)
e.g. "bela,jeannette,michelle" --> List{"bela", "jeannette", "michelle"}
public static Object pickRandomElement(Vector list)
public static Vector pickSubset(Vector members, double subset_percentage)
Selects a random subset of members according to subset_percentage and returns them.
Picks no member twice from the same membership. If the percentage is smaller than 1 -> picks 1 member.
public static String print(Throwable t)
public static String printBytes(long bytes)
public static String printEvent(
Event evt)
public static void printFragments(byte[][] frags)
public static String printMembers(Vector v)
public static String printMessage(
Message msg)
Tries to read an object from the message's buffer and prints it
public static String printMethodCall(
Message msg)
Tries to read a MethodCall
object from the message's buffer and prints it.
Returns empty string if object is not a method call
public static String printStackTrace(Throwable t)
Use with caution: lots of overhead
public static void printThreads()
public static void prompt(String s)
public static long random(long range)
Returns a random value in the range [1 - range]
public static
Address readAddress(DataInputStream in)
public static Collection readAddresses(DataInputStream in, Class cl)
Parameters: in cl The type of Collection, e.g. Vector.class
Returns: Collection of Address objects
Throws: IOException IllegalAccessException InstantiationException
public static byte[] readByteBuffer(DataInputStream in)
public static
Streamable readGenericStreamable(DataInputStream in)
public static Object readObject(DataInputStream in)
public static
Streamable readStreamable(Class clazz, DataInputStream in)
public static String readString(DataInputStream in)
Checks whether 2 Addresses are on the same host
public static String shortName(String hostname)
public static String shortName(InetAddress hostname)
public static int size(
Address addr)
public static long size(Collection addrs)
Returns the marshalled size of a Collection of Addresses.
Assumes elements are of the same type !Parameters: addrs Collection
Returns: long size
public static long sizeOf(String classname)
public static long sizeOf(Object inst)
public static void sleep(long timeout)
Sleep for timeout msecs. Returns when timeout has elapsed or thread was interrupted
public static void sleep(long msecs, boolean busy_sleep)
On most UNIX systems, the minimum sleep time is 10-20ms. Even if we specify sleep(1), the thread will
sleep for at least 10-20ms. On Windows, sleep() seems to be implemented as a busy sleep, that is the
thread never relinquishes control and therefore the sleep(x) is exactly x ms long.
public static void sleepRandom(long timeout)
Sleeps between 1 and timeout milliseconds, chosen randomly. Timeout must be > 1
public static
Streamable streamableFromByteBuffer(Class cl, byte[] buffer)
public static byte[] streamableToByteBuffer(
Streamable obj)
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%.
public static void writeAddress(
Address addr, DataOutputStream out)
public static void writeAddresses(Collection v, DataOutputStream out)
Writes a Vector of Addresses. Can contain 65K addresses at most
Parameters: v A Collection out
Throws: IOException
public static void writeByteBuffer(byte[] buf, DataOutputStream out)
public static void writeFully(ByteBuffer buf, WritableByteChannel out)
if we were to register for OP_WRITE and send the remaining data on
readyOps for this channel we have to either block the caller thread or
queue the message buffers that may arrive while waiting for OP_WRITE.
Instead of the above approach this method will continuously write to the
channel until the buffer sent fully.
public static void writeGenericStreamable(
Streamable obj, DataOutputStream out)
public static void writeObject(Object obj, DataOutputStream out)
public static void writeStreamable(
Streamable obj, DataOutputStream out)
public static void writeString(String s, DataOutputStream out)
Copyright ? 1998-2005 Bela Ban. All Rights Reserved.