jdk/src/share/sample/nio/multicast/MulticastAddress.java
changeset 1152 29d6145d1097
child 5506 202f599c92aa
equal deleted inserted replaced
1151:4070cecdb99d 1152:29d6145d1097
       
     1 /*
       
     2  * Copyright 2007-2008 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  *
       
     4  * Redistribution and use in source and binary forms, with or without
       
     5  * modification, are permitted provided that the following conditions
       
     6  * are met:
       
     7  *
       
     8  *   - Redistributions of source code must retain the above copyright
       
     9  *     notice, this list of conditions and the following disclaimer.
       
    10  *
       
    11  *   - Redistributions in binary form must reproduce the above copyright
       
    12  *     notice, this list of conditions and the following disclaimer in the
       
    13  *     documentation and/or other materials provided with the distribution.
       
    14  *
       
    15  *   - Neither the name of Sun Microsystems nor the names of its
       
    16  *     contributors may be used to endorse or promote products derived
       
    17  *     from this software without specific prior written permission.
       
    18  *
       
    19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
       
    20  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
       
    21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
       
    22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
       
    23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
       
    24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
       
    25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
       
    26  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
       
    27  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
       
    28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
       
    29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    30  */
       
    31 
       
    32 import java.net.InetAddress;
       
    33 import java.net.NetworkInterface;
       
    34 import java.net.UnknownHostException;
       
    35 import java.net.SocketException;
       
    36 
       
    37 /**
       
    38  * Parses and represents a multicast address.
       
    39  */
       
    40 
       
    41 class MulticastAddress {
       
    42     private final InetAddress group;
       
    43     private final int port;
       
    44     private final NetworkInterface interf;
       
    45 
       
    46     private MulticastAddress(InetAddress group, int port, NetworkInterface interf) {
       
    47         this.group = group;
       
    48         this.port = port;
       
    49         this.interf = interf;
       
    50     }
       
    51 
       
    52     InetAddress group() {
       
    53         return group;
       
    54     }
       
    55 
       
    56     int port() {
       
    57         return port;
       
    58     }
       
    59 
       
    60     /**
       
    61      * @return  The network interface, may be {@code null}
       
    62      */
       
    63     NetworkInterface interf() {
       
    64         return interf;
       
    65     }
       
    66 
       
    67     /**
       
    68      * Parses a string of the form "group:port[@interface]", returning
       
    69      * a MulticastAddress representing the address
       
    70      */
       
    71     static MulticastAddress parse(String s) {
       
    72         String[] components = s.split("@");
       
    73         if (components.length > 2)
       
    74             throw new IllegalArgumentException("At most one '@' expected");
       
    75 
       
    76         // get group and port
       
    77         String target = components[0];
       
    78         int len = components[0].length();
       
    79         int colon = components[0].lastIndexOf(':');
       
    80         if ((colon < 1) || (colon > (len-2)))
       
    81             throw new IllegalArgumentException("group:port expected");
       
    82         String groupString = target.substring(0, colon);
       
    83         int port = -1;
       
    84         try {
       
    85             port = Integer.parseInt(target.substring(colon+1, len));
       
    86         } catch (NumberFormatException x) {
       
    87              throw new IllegalArgumentException(x);
       
    88         }
       
    89 
       
    90         // handle IPv6 literal address
       
    91         if (groupString.charAt(0) == '[') {
       
    92             len = groupString.length();
       
    93             if (groupString.charAt(len-1) != ']')
       
    94                 throw new IllegalArgumentException("missing ']'");
       
    95             groupString = groupString.substring(1,len-1);
       
    96             if (groupString.length() == 0)
       
    97                 throw new IllegalArgumentException("missing IPv6 address");
       
    98         }
       
    99 
       
   100         // get group address
       
   101         InetAddress group = null;
       
   102         try {
       
   103             group = InetAddress.getByName(groupString);
       
   104         } catch (UnknownHostException x) {
       
   105             throw new IllegalArgumentException(x);
       
   106         }
       
   107         if (!group.isMulticastAddress()) {
       
   108             throw new IllegalArgumentException("'" + group.getHostAddress() +
       
   109                 "' is not multicast address");
       
   110         }
       
   111 
       
   112         // optional interface
       
   113         NetworkInterface interf = null;
       
   114         if (components.length == 2) {
       
   115             try {
       
   116                 interf = NetworkInterface.getByName(components[1]);
       
   117             } catch (SocketException x) {
       
   118                 throw new IllegalArgumentException(x);
       
   119             }
       
   120             if (interf == null) {
       
   121                 throw new IllegalArgumentException("'" + components[1] +
       
   122                    "' is not valid interface");
       
   123             }
       
   124         }
       
   125         return new MulticastAddress(group, port, interf);
       
   126     }
       
   127 }