org.jlab.coda.cMsg
Class cMsgUtilities

java.lang.Object
  extended by org.jlab.coda.cMsg.cMsgUtilities

public class cMsgUtilities
extends java.lang.Object

This class stores methods which are neatly self-contained and may be used in more that one place.

Version:
1.0
Author:
Carl Timmer

Constructor Summary
cMsgUtilities()
           
 
Method Summary
static int bytesToInt(byte[] b, int off)
          Converts 4 bytes of a byte array into an integer.
static long bytesToLong(byte[] b, int off)
          Converts 8 bytes of a byte array into a long.
static short bytesToShort(byte[] b, int off)
          Converts 2 bytes of a byte array into a short.
static java.lang.String constructServerName(java.lang.String s)
          This method tests its input argument to see if it is in the proper format for a server; namely, "host:port".
static void intToBytes(int intVal, byte[] b, int off)
          Copies an integer value into 4 bytes of a byte array.
static boolean isHostLocal(java.lang.String hostName)
          Determine whether a given host name refers to the local host.
static boolean isHostSame(java.lang.String hostName1, java.lang.String hostName2)
          Determine whether two given host names refers to the same host.
static java.lang.String printError(int error, int debug)
          Method that returns and/or prints an error explanation.
static int readSocketBytes(java.nio.ByteBuffer buffer, java.nio.channels.SocketChannel channel, int bytes, int debug)
          This methods reads a minimum of number of bytes from a channel into a buffer.
static int readSocketBytesPlain(java.nio.ByteBuffer buffer, java.nio.channels.SocketChannel channel, int bytes)
          This methods reads a minimum of number of bytes from a channel into a buffer.
static void shortToBytes(short val, byte[] b, int off)
          Copies a short value into 2 bytes of a byte array.
static java.nio.channels.ByteChannel wrapChannel(java.nio.channels.ByteChannel channel)
          This method wraps a SocketChannel so that the input and output streams derived from that channel do not block each other (SUN java bug id 4774871).
static java.nio.channels.ByteChannel wrapChannel2(java.nio.channels.ByteChannel channel)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

cMsgUtilities

public cMsgUtilities()
Method Detail

bytesToInt

public static final int bytesToInt(byte[] b,
                                   int off)
Converts 4 bytes of a byte array into an integer.

Parameters:
b - byte array
off - offset into the byte array (0 = start at first element)
Returns:
integer value

intToBytes

public static final void intToBytes(int intVal,
                                    byte[] b,
                                    int off)
Copies an integer value into 4 bytes of a byte array.

Parameters:
intVal - integer value
b - byte array
off - offset into the byte array

bytesToShort

public static final short bytesToShort(byte[] b,
                                       int off)
Converts 2 bytes of a byte array into a short.

Parameters:
b - byte array
off - offset into the byte array (0 = start at first element)
Returns:
integer value

shortToBytes

public static final void shortToBytes(short val,
                                      byte[] b,
                                      int off)
Copies a short value into 2 bytes of a byte array.

Parameters:
val - short value
b - byte array
off - offset into the byte array

bytesToLong

public static final long bytesToLong(byte[] b,
                                     int off)
Converts 8 bytes of a byte array into a long.

Parameters:
b - byte array
off - offset into the byte array (0 = start at first element)
Returns:
ling value

isHostLocal

public static final boolean isHostLocal(java.lang.String hostName)
Determine whether a given host name refers to the local host.

Parameters:
hostName - host name that is checked to see if its local or not

isHostSame

public static final boolean isHostSame(java.lang.String hostName1,
                                       java.lang.String hostName2)
Determine whether two given host names refers to the same host.

Parameters:
hostName1 - host name that is checked to see if it is the same as the other arg or not.
hostName2 - host name that is checked to see if it is the same as the other arg or not.

wrapChannel2

public static final java.nio.channels.ByteChannel wrapChannel2(java.nio.channels.ByteChannel channel)

wrapChannel

public static final java.nio.channels.ByteChannel wrapChannel(java.nio.channels.ByteChannel channel)
This method wraps a SocketChannel so that the input and output streams derived from that channel do not block each other (SUN java bug id 4774871). The problem is that a SocketChannel is also a SelectableChannel which has this synchronization issue, and that is eliminated by making it a ByteChannel.

Parameters:
channel - the SelectableChannel which has sync problems
Returns:
a ByteChannel which does not have sync problems

readSocketBytesPlain

public static final int readSocketBytesPlain(java.nio.ByteBuffer buffer,
                                             java.nio.channels.SocketChannel channel,
                                             int bytes)
                                      throws java.io.IOException
This methods reads a minimum of number of bytes from a channel into a buffer.

Parameters:
buffer - a byte buffer which channel data is read into
channel - nio socket communication channel
bytes - minimum number of bytes to read from channel
Returns:
number of bytes read
Throws:
java.io.IOException - If channel is closed or cannot be read from

readSocketBytes

public static final int readSocketBytes(java.nio.ByteBuffer buffer,
                                        java.nio.channels.SocketChannel channel,
                                        int bytes,
                                        int debug)
                                 throws java.io.IOException
This methods reads a minimum of number of bytes from a channel into a buffer.

Parameters:
buffer - a byte buffer which channel data is read into
channel - nio socket communication channel
bytes - minimum number of bytes to read from channel
debug - level of debug output
Returns:
number of bytes read
Throws:
java.io.IOException - If channel is closed or cannot be read from

printError

public static final java.lang.String printError(int error,
                                                int debug)
Method that returns and/or prints an error explanation.

Parameters:
error - error number
debug - level of debug output
Returns:
error explanation

constructServerName

public static final java.lang.String constructServerName(java.lang.String s)
                                                  throws cMsgException
This method tests its input argument to see if it is in the proper format for a server; namely, "host:port". If it is, it makes sure the host is in its canonical form.

Parameters:
s - input string of a possible server name
Returns:
an acceptable server name with the canonical form of the host name
Throws:
cMsgException - if input string is not in the proper form (host:port) or the host is unknown