Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/InetAddress/nameservice/chaining/SimpleNameService.java
38861 views
/*1* Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24*/2526import java.net.*;27import java.util.*;28import sun.net.spi.nameservice.*;293031public class SimpleNameService implements NameService {32// host name <-> host addr mapping33private HashMap<String, String> hosts = new LinkedHashMap<String, String>();3435public void put(String host, String addr) {36hosts.put(host, addr);37}3839private static String addrToString(byte addr[]) {40return Byte.toString(addr[0]) + "." +41Byte.toString(addr[1]) + "." +42Byte.toString(addr[2]) + "." +43Byte.toString(addr[3]);44}4546public SimpleNameService() {47}4849public InetAddress[] lookupAllHostAddr(String host) throws UnknownHostException {50String addr = hosts.get(host);51if (addr == null) {52throw new UnknownHostException(host);53}5455StringTokenizer tokenizer = new StringTokenizer(addr, ".");56byte addrs[] = new byte[4];57for (int i = 0; i < 4; i++) {58addrs[i] = (byte)Integer.parseInt(tokenizer.nextToken());59}60InetAddress[] ret = new InetAddress[1];61ret[0] = InetAddress.getByAddress(host, addrs);62return ret;63}6465public String getHostByAddr(byte[] addr) throws UnknownHostException {66String addrString = addrToString(addr);67Iterator i = hosts.keySet().iterator();68while (i.hasNext()) {69String host = (String)i.next();70String value = (String)hosts.get(host);71if (value.equals(addrString)) {72return host;73}74}75throw new UnknownHostException();76}77}787980