public class cMsgUtilities
extends java.lang.Object
Constructor and Description |
---|
cMsgUtilities() |
Modifier and Type | Method and Description |
---|---|
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 java.util.Collection<java.lang.String> |
getAllBroadcastAddresses()
Get all local IP broadcast addresses in a list in dotted-decimal form.
|
static java.util.Collection<java.lang.String> |
getAllIpAddresses()
Get all local IP addresses in a list in dotted-decimal form.
|
static java.util.List<java.net.InterfaceAddress> |
getAllIpInfo()
Get all local IP addresses, broadcast addresses and subnet masks in a
single list of InterfaceAddress objects.
|
static java.lang.String |
getBroadcastAddress(java.lang.String ip)
Given a local IP address as an argument, this method will return its
broadcast or subnet address.
|
static java.lang.String |
getMatchingLocalIpAddress(java.lang.String ip)
Given an IP address as an argument, this method will return that if it matches
one of the local IP addresses.
|
static int |
getNetworkAddressInt(byte[] addr,
short netPrefixLen)
Get the network or subnet address (32-bit integer format)
of the given IP address (byte array format)
for the given netmask (# of bits format).
|
static java.lang.String |
getNetworkAddressString(byte[] addr,
short netPrefixLen)
Get the network or subnet address as a dotted-decimal string
of the given IP address (byte array format)
for the given netmask (# of bits format).
|
static java.lang.String |
getNetworkAddressString(java.lang.String hostDottedDecimal,
short netPrefixLen)
Get the network or subnet address as a dotted-decimal string
of the given IP address (dotted-decimal string format)
for the given netmask (# of bits format).
|
static void |
intToBytes(int intVal,
byte[] b,
int off)
Copies an integer value into 4 bytes of a byte array.
|
static byte[] |
isDottedDecimal(java.lang.String ipAddress)
This method tells whether the given IPv4 address is in dot-decimal notation or not.
|
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.util.List<java.lang.String> |
orderIPAddresses(java.util.List<java.lang.String> ipAddresses,
java.util.List<java.lang.String> broadAddresses,
java.lang.String preferredAddress)
Takes a list of dotted-decimal formatted IP address strings and their corresponding
broadcast addresses and orders them so that those on the local, preferred subnet are first,
those on other local subnets are next, and all others come last.
|
static void |
printBuffer(java.nio.ByteBuffer buf,
int position,
int words,
java.lang.String label)
This method takes a byte buffer and prints out the desired number of words
from the given position.
|
static void |
printBytes(byte[] array,
int offset,
int bytes,
java.lang.String label)
This method takes a byte array and prints out the desired number of bytes
from the given index.
|
static void |
printBytes(java.nio.ByteBuffer buf,
int position,
int bytes,
java.lang.String label)
This method takes a byte buffer and prints out the desired number of bytes
from the given position.
|
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) |
public static int bytesToInt(byte[] b, int off)
b
- byte arrayoff
- offset into the byte array (0 = start at first element)public static void intToBytes(int intVal, byte[] b, int off)
intVal
- integer valueb
- byte arrayoff
- offset into the byte arraypublic static short bytesToShort(byte[] b, int off)
b
- byte arrayoff
- offset into the byte array (0 = start at first element)public static void shortToBytes(short val, byte[] b, int off)
val
- short valueb
- byte arrayoff
- offset into the byte arraypublic static long bytesToLong(byte[] b, int off)
b
- byte arrayoff
- offset into the byte array (0 = start at first element)public static void printBuffer(java.nio.ByteBuffer buf, int position, int words, java.lang.String label)
buf
- buffer to print outposition
- position of data (bytes) in buffer to start printingwords
- number of 32 bit words to print in hexlabel
- a label to print as headerpublic static void printBytes(java.nio.ByteBuffer buf, int position, int bytes, java.lang.String label)
buf
- buffer to print outposition
- position of data (bytes) in buffer to start printing (starting from 0)bytes
- number of bytes to print in hexlabel
- a label to print as headerpublic static void printBytes(byte[] array, int offset, int bytes, java.lang.String label)
array
- byte array to print outoffset
- offset into array to start printingbytes
- number of bytes to print in hexlabel
- a label to print as headerpublic static java.util.Collection<java.lang.String> getAllIpAddresses()
public static java.util.Collection<java.lang.String> getAllBroadcastAddresses()
public static java.lang.String getBroadcastAddress(java.lang.String ip) throws cMsgException
ip
- IP or subnet address in dot-decimal formatcMsgException
- if arg is not in dot-decimal formatpublic static java.util.List<java.net.InterfaceAddress> getAllIpInfo()
public static java.lang.String getMatchingLocalIpAddress(java.lang.String ip) throws cMsgException
ip
- IP or subnet address in dotted-decimal formatcMsgException
- if arg not in dotted-decimal formatpublic static java.util.List<java.lang.String> orderIPAddresses(java.util.List<java.lang.String> ipAddresses, java.util.List<java.lang.String> broadAddresses, java.lang.String preferredAddress)
ipAddresses
- list of addresses to orderbroadAddresses
- list of broadcast addresses - each associated w/ corresponding
ip addresspreferredAddress
- if not null, it is the preferred local subnet(broadcast) address
used in sorting so that addresses on the given list which
exist on the preferred subnet are first on the returned list.
If a valid, local, non-subnet ip address is given, it will be
converted internally to the subnet it is on.public static int getNetworkAddressInt(byte[] addr, short netPrefixLen)
addr
- IP address (byte array format)netPrefixLen
- netmask (# of bits format)public static java.lang.String getNetworkAddressString(byte[] addr, short netPrefixLen)
addr
- IP address (byte array format)netPrefixLen
- netmask (# of bits format)public static java.lang.String getNetworkAddressString(java.lang.String hostDottedDecimal, short netPrefixLen)
hostDottedDecimal
- IP address (dotted-decimal string format)netPrefixLen
- netmask (# of bits format)public static byte[] isDottedDecimal(java.lang.String ipAddress)
ipAddress
- IPV4 addresspublic static boolean isHostLocal(java.lang.String hostName)
hostName
- host name that is checked to see if its local or nottrue
if arg is name of local host, else false
.public static boolean isHostSame(java.lang.String hostName1, java.lang.String hostName2)
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.true
if both args refer to the same host, else false
.public static java.nio.channels.ByteChannel wrapChannel2(java.nio.channels.ByteChannel channel)
public static java.nio.channels.ByteChannel wrapChannel(java.nio.channels.ByteChannel channel)
channel
- the SelectableChannel which has sync problemspublic static int readSocketBytesPlain(java.nio.ByteBuffer buffer, java.nio.channels.SocketChannel channel, int bytes) throws java.io.IOException
buffer
- a byte buffer which channel data is read intochannel
- nio socket communication channelbytes
- minimum number of bytes to read from channeljava.io.IOException
- If channel is closed or cannot be read frompublic static int readSocketBytes(java.nio.ByteBuffer buffer, java.nio.channels.SocketChannel channel, int bytes, int debug) throws java.io.IOException
buffer
- a byte buffer which channel data is read intochannel
- nio socket communication channelbytes
- minimum number of bytes to read from channeldebug
- level of debug outputjava.io.IOException
- If channel is closed or cannot be read frompublic static java.lang.String printError(int error, int debug)
error
- error numberdebug
- level of debug outputpublic static java.lang.String constructServerName(java.lang.String s) throws cMsgException
s
- input string of a possible server namecMsgException
- if input string is not in the proper form (host:port)
or the host is unknown