Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/sample/nio/multicast/MulticastAddress.java
38829 views
/*1* Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6*7* - Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9*10* - Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* - Neither the name of Oracle nor the names of its15* contributors may be used to endorse or promote products derived16* from this software without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS19* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,20* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR21* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR22* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,23* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,24* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR25* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF26* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING27* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS28* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.29*/3031/*32* This source code is provided to illustrate the usage of a given feature33* or technique and has been deliberately simplified. Additional steps34* required for a production-quality application, such as security checks,35* input validation and proper error handling, might not be present in36* this sample code.37*/383940import java.net.InetAddress;41import java.net.NetworkInterface;42import java.net.UnknownHostException;43import java.net.SocketException;4445/**46* Parses and represents a multicast address.47*/4849class MulticastAddress {50private final InetAddress group;51private final int port;52private final NetworkInterface interf;5354private MulticastAddress(InetAddress group, int port, NetworkInterface interf) {55this.group = group;56this.port = port;57this.interf = interf;58}5960InetAddress group() {61return group;62}6364int port() {65return port;66}6768/**69* @return The network interface, may be {@code null}70*/71NetworkInterface interf() {72return interf;73}7475/**76* Parses a string of the form "group:port[@interface]", returning77* a MulticastAddress representing the address78*/79static MulticastAddress parse(String s) {80String[] components = s.split("@");81if (components.length > 2)82throw new IllegalArgumentException("At most one '@' expected");8384// get group and port85String target = components[0];86int len = components[0].length();87int colon = components[0].lastIndexOf(':');88if ((colon < 1) || (colon > (len-2)))89throw new IllegalArgumentException("group:port expected");90String groupString = target.substring(0, colon);91int port = -1;92try {93port = Integer.parseInt(target.substring(colon+1, len));94} catch (NumberFormatException x) {95throw new IllegalArgumentException(x);96}9798// handle IPv6 literal address99if (groupString.charAt(0) == '[') {100len = groupString.length();101if (groupString.charAt(len-1) != ']')102throw new IllegalArgumentException("missing ']'");103groupString = groupString.substring(1,len-1);104if (groupString.length() == 0)105throw new IllegalArgumentException("missing IPv6 address");106}107108// get group address109InetAddress group = null;110try {111group = InetAddress.getByName(groupString);112} catch (UnknownHostException x) {113throw new IllegalArgumentException(x);114}115if (!group.isMulticastAddress()) {116throw new IllegalArgumentException("'" + group.getHostAddress() +117"' is not multicast address");118}119120// optional interface121NetworkInterface interf = null;122if (components.length == 2) {123try {124interf = NetworkInterface.getByName(components[1]);125} catch (SocketException x) {126throw new IllegalArgumentException(x);127}128if (interf == null) {129throw new IllegalArgumentException("'" + components[1] +130"' is not valid interface");131}132}133return new MulticastAddress(group, port, interf);134}135}136137138