jdk/src/sample/share/nio/multicast/MulticastAddress.java
author emc
Wed, 05 Nov 2014 08:37:04 -0500
changeset 27386 784414cffd9a
parent 25859 3317bb8137f4
permissions -rw-r--r--
8035259: javac, incorrect shadowing of classes vs type parameters Summary: Cause javac to look at type variables first when resolving names in an extends/implements list Reviewed-by: mcimadamore, jlahoda
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 10292
diff changeset
     2
 * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
     3
 *
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
     5
 * modification, are permitted provided that the following conditions
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
     6
 * are met:
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
     7
 *
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    10
 *
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    12
 *     notice, this list of conditions and the following disclaimer in the
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    13
 *     documentation and/or other materials provided with the distribution.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    14
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1152
diff changeset
    15
 *   - Neither the name of Oracle nor the names of its
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    16
 *     contributors may be used to endorse or promote products derived
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    17
 *     from this software without specific prior written permission.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    18
 *
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    30
 */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    31
10292
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    32
/*
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    33
 * This source code is provided to illustrate the usage of a given feature
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    34
 * or technique and has been deliberately simplified. Additional steps
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    35
 * required for a production-quality application, such as security checks,
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    36
 * input validation and proper error handling, might not be present in
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    37
 * this sample code.
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    38
 */
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    39
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    40
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    41
import java.net.InetAddress;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    42
import java.net.NetworkInterface;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    43
import java.net.UnknownHostException;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    44
import java.net.SocketException;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    45
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    46
/**
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    47
 * Parses and represents a multicast address.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    48
 */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    49
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    50
class MulticastAddress {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    51
    private final InetAddress group;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    52
    private final int port;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    53
    private final NetworkInterface interf;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    54
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    55
    private MulticastAddress(InetAddress group, int port, NetworkInterface interf) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    56
        this.group = group;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    57
        this.port = port;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    58
        this.interf = interf;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    59
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    60
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    61
    InetAddress group() {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    62
        return group;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    63
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    64
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    65
    int port() {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    66
        return port;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    67
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    68
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    69
    /**
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    70
     * @return  The network interface, may be {@code null}
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    71
     */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    72
    NetworkInterface interf() {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    73
        return interf;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    74
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    75
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    76
    /**
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    77
     * Parses a string of the form "group:port[@interface]", returning
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    78
     * a MulticastAddress representing the address
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    79
     */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    80
    static MulticastAddress parse(String s) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    81
        String[] components = s.split("@");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    82
        if (components.length > 2)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    83
            throw new IllegalArgumentException("At most one '@' expected");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    84
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    85
        // get group and port
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    86
        String target = components[0];
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    87
        int len = components[0].length();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    88
        int colon = components[0].lastIndexOf(':');
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    89
        if ((colon < 1) || (colon > (len-2)))
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    90
            throw new IllegalArgumentException("group:port expected");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    91
        String groupString = target.substring(0, colon);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    92
        int port = -1;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    93
        try {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    94
            port = Integer.parseInt(target.substring(colon+1, len));
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    95
        } catch (NumberFormatException x) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    96
             throw new IllegalArgumentException(x);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    97
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    98
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    99
        // handle IPv6 literal address
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   100
        if (groupString.charAt(0) == '[') {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   101
            len = groupString.length();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   102
            if (groupString.charAt(len-1) != ']')
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   103
                throw new IllegalArgumentException("missing ']'");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   104
            groupString = groupString.substring(1,len-1);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   105
            if (groupString.length() == 0)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   106
                throw new IllegalArgumentException("missing IPv6 address");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   107
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   108
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   109
        // get group address
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   110
        InetAddress group = null;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   111
        try {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   112
            group = InetAddress.getByName(groupString);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   113
        } catch (UnknownHostException x) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   114
            throw new IllegalArgumentException(x);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   115
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   116
        if (!group.isMulticastAddress()) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   117
            throw new IllegalArgumentException("'" + group.getHostAddress() +
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   118
                "' is not multicast address");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   119
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   120
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   121
        // optional interface
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   122
        NetworkInterface interf = null;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   123
        if (components.length == 2) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   124
            try {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   125
                interf = NetworkInterface.getByName(components[1]);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   126
            } catch (SocketException x) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   127
                throw new IllegalArgumentException(x);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   128
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   129
            if (interf == null) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   130
                throw new IllegalArgumentException("'" + components[1] +
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   131
                   "' is not valid interface");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   132
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   133
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   134
        return new MulticastAddress(group, port, interf);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   135
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   136
}