Class NetworkUtility


  • public class NetworkUtility
    extends java.lang.Object
    Utility class that can be used by any calling code to do common routines about Network I/O
    Author:
    Bongjae Chang
    • Constructor Summary

      Constructors 
      Constructor Description
      NetworkUtility()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void deserialize​(java.io.InputStream is, int count, java.util.Map<java.lang.String,​java.io.Serializable> messages)  
      static java.util.List<java.net.InetAddress> getAllLocalAddresses()
      Returns all local addresses except for lookback and any local address But, if any addresses were not found locally, the lookback is added to the list.
      static java.net.InetAddress getAnyAddress()  
      static java.net.InetAddress getFirstInetAddress()
      Return a first InetAddress of the first network interface check java property java.net.preferIPv6Addresses for whether to favor IPv4 or IPv6.
      static java.net.InetAddress getFirstInetAddress​(boolean preferIPv6)
      Return a first InetAddress of the first network interface But, if any network interfaces were not found locally, null could be returned.
      static java.net.NetworkInterface getFirstNetworkInterface​(boolean preferIPv6)
      Return a first network interface except for the lookback But, if any network interfaces were not found locally, the lookback interface is returned.
      static int getIntFromByteArray​(byte[] bytes, int offset)  
      static java.net.InetAddress getLocalHostAddress()  
      static java.net.InetAddress getLoopbackAddress()  
      static java.net.InetAddress getNetworkInetAddress​(java.net.NetworkInterface anInterface, boolean preferIPv6)
      Return a first InetAddress of network interface But, if any network interfaces were not found locally, null could be returned.
      static boolean getPreferIpv6Addresses()  
      static boolean isBindAddressValid​(java.lang.String addressString)  
      static boolean isLoopbackNetworkInterface​(java.net.NetworkInterface anInterface)  
      static boolean isUp​(java.net.NetworkInterface anInterface)  
      static void main​(java.lang.String[] args)  
      static java.net.InetAddress resolveBindInterfaceName​(java.lang.String addressString)  
      static int serialize​(java.io.OutputStream baos, java.util.Map<java.lang.String,​java.io.Serializable> messages)  
      static int serialize​(java.io.OutputStream baos, java.util.Map<java.lang.String,​java.io.Serializable> messages, boolean debug)  
      static boolean supportsMulticast​(java.net.NetworkInterface anInterface)  
      static void writeIntToByteArray​(byte[] bytes, int offset, int value)  
      • Methods inherited from class java.lang.Object

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

      • ANYADDRESS

        public static final java.net.InetAddress ANYADDRESS
        Constant which works as the IP "Any Address" value
      • ANYADDRESSV4

        public static final java.net.InetAddress ANYADDRESSV4
      • ANYADDRESSV6

        public static final java.net.InetAddress ANYADDRESSV6
      • LOOPBACK

        public static final java.net.InetAddress LOOPBACK
        Constant which works as the IP "Local Loopback" value;
      • LOOPBACKV4

        public static final java.net.InetAddress LOOPBACKV4
      • LOOPBACKV6

        public static final java.net.InetAddress LOOPBACKV6
      • allLocalAddresses

        public static volatile java.util.List<java.net.InetAddress> allLocalAddresses
      • firstInetAddressV4

        public static volatile java.net.InetAddress firstInetAddressV4
      • firstInetAddressV6

        public static volatile java.net.InetAddress firstInetAddressV6
    • Constructor Detail

      • NetworkUtility

        public NetworkUtility()
    • Method Detail

      • getAllLocalAddresses

        public static java.util.List<java.net.InetAddress> getAllLocalAddresses()
        Returns all local addresses except for lookback and any local address But, if any addresses were not found locally, the lookback is added to the list.
        Returns:
        List which contains available addresses locally
      • getAnyAddress

        public static java.net.InetAddress getAnyAddress()
      • getLoopbackAddress

        public static java.net.InetAddress getLoopbackAddress()
      • getFirstNetworkInterface

        public static java.net.NetworkInterface getFirstNetworkInterface​(boolean preferIPv6)
                                                                  throws java.io.IOException
        Return a first network interface except for the lookback But, if any network interfaces were not found locally, the lookback interface is returned.
        Parameters:
        preferIPv6 - flag to indicate if IPV6 is preferred
        Returns:
        a first network interface
        Throws:
        java.io.IOException - if an I/O error occurs or a network interface was not found
      • getLocalHostAddress

        public static java.net.InetAddress getLocalHostAddress()
      • getPreferIpv6Addresses

        public static boolean getPreferIpv6Addresses()
      • getFirstInetAddress

        public static java.net.InetAddress getFirstInetAddress()
                                                        throws java.io.IOException
        Return a first InetAddress of the first network interface check java property java.net.preferIPv6Addresses for whether to favor IPv4 or IPv6. (java default is to favor IPv4 addresses) If unable to find a valid network interface, then fallback to trying to get localhost address as last resort.
        Returns:
        a first found InetAddress.
        Throws:
        java.io.IOException - if an I/O error occurs or a network interface was not found
      • getFirstInetAddress

        public static java.net.InetAddress getFirstInetAddress​(boolean preferIPv6)
                                                        throws java.io.IOException
        Return a first InetAddress of the first network interface But, if any network interfaces were not found locally, null could be returned.
        Parameters:
        preferIPv6 - if true, prefer IPv6 InetAddress. otherwise prefer IPv4 InetAddress
        Returns:
        a first found InetAddress.
        Throws:
        java.io.IOException - if an I/O error occurs or a network interface was not found
      • getNetworkInetAddress

        public static java.net.InetAddress getNetworkInetAddress​(java.net.NetworkInterface anInterface,
                                                                 boolean preferIPv6)
                                                          throws java.io.IOException
        Return a first InetAddress of network interface But, if any network interfaces were not found locally, null could be returned.
        Parameters:
        anInterface - the type of network interface
        preferIPv6 - if true, prefer IPv6 InetAddress. otherwise prefer IPv4 InetAddress
        Returns:
        a first found InetAddress.
        Throws:
        java.io.IOException - if an I/O error occurs or a network interface was not found
      • isLoopbackNetworkInterface

        public static boolean isLoopbackNetworkInterface​(java.net.NetworkInterface anInterface)
      • supportsMulticast

        public static boolean supportsMulticast​(java.net.NetworkInterface anInterface)
      • isUp

        public static boolean isUp​(java.net.NetworkInterface anInterface)
      • writeIntToByteArray

        public static void writeIntToByteArray​(byte[] bytes,
                                               int offset,
                                               int value)
                                        throws java.lang.IllegalArgumentException
        Throws:
        java.lang.IllegalArgumentException
      • getIntFromByteArray

        public static int getIntFromByteArray​(byte[] bytes,
                                              int offset)
                                       throws java.lang.IllegalArgumentException
        Throws:
        java.lang.IllegalArgumentException
      • serialize

        public static int serialize​(java.io.OutputStream baos,
                                    java.util.Map<java.lang.String,​java.io.Serializable> messages)
                             throws MessageIOException
        Throws:
        MessageIOException
      • serialize

        public static int serialize​(java.io.OutputStream baos,
                                    java.util.Map<java.lang.String,​java.io.Serializable> messages,
                                    boolean debug)
                             throws MessageIOException
        Throws:
        MessageIOException
      • deserialize

        public static void deserialize​(java.io.InputStream is,
                                       int count,
                                       java.util.Map<java.lang.String,​java.io.Serializable> messages)
                                throws MessageIOException
        Throws:
        MessageIOException
      • isBindAddressValid

        public static boolean isBindAddressValid​(java.lang.String addressString)
      • resolveBindInterfaceName

        public static java.net.InetAddress resolveBindInterfaceName​(java.lang.String addressString)
      • main

        public static void main​(java.lang.String[] args)
                         throws java.io.IOException
        Throws:
        java.io.IOException