Path: blob/master/test/jdk/com/sun/nio/sctp/SctpMultiChannel/Util.java
41155 views
/*1* Copyright (c) 2009, 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*/2223import java.net.NetworkInterface;24import java.net.InetSocketAddress;25import java.net.SocketAddress;26import java.net.InetAddress;27import java.net.Inet4Address;28import java.net.SocketException;29import java.io.IOException;30import java.io.PrintStream;31import java.io.UnsupportedEncodingException;32import java.util.List;33import java.util.ArrayList;34import java.util.Set;35import java.util.Collections;36import java.util.Enumeration;37import java.util.Iterator;38import java.nio.ByteBuffer;39import com.sun.nio.sctp.SctpChannel;40import static java.lang.System.out;4142public class Util {43static final int SMALL_BUFFER = 128;44static final String SMALL_MESSAGE =45"Under the bridge and over the dam, looking for berries, berries for jam";4647static final int LARGE_BUFFER = 32768;48static final String LARGE_MESSAGE;4950static {51StringBuffer sb = new StringBuffer(LARGE_BUFFER);52for (int i=0; i<460; i++)53sb.append(SMALL_MESSAGE);5455LARGE_MESSAGE = sb.toString();56}5758static boolean isSCTPSupported() {59try {60SctpChannel c = SctpChannel.open();61c.close();62return true;63} catch (IOException ioe) {64ioe.printStackTrace();65} catch (UnsupportedOperationException e) {66out.println(e);67}6869return false;70}71/**72* Returns a list of all the addresses on the system.73* @param inclLoopback74* if {@code true}, include the loopback addresses75* @param ipv4Only76* it {@code true}, only IPv4 addresses will be included77*/78static List<InetAddress> getAddresses(boolean inclLoopback,79boolean ipv4Only)80throws SocketException {81ArrayList<InetAddress> list = new ArrayList<InetAddress>();82Enumeration<NetworkInterface> nets =83NetworkInterface.getNetworkInterfaces();84for (NetworkInterface netInf : Collections.list(nets)) {85Enumeration<InetAddress> addrs = netInf.getInetAddresses();86for (InetAddress addr : Collections.list(addrs)) {87if (!list.contains(addr) &&88(inclLoopback ? true : !addr.isLoopbackAddress()) &&89(ipv4Only ? (addr instanceof Inet4Address) : true)) {90list.add(addr);91}92}93}9495return list;96}9798static void dumpAddresses(SctpChannel channel,99PrintStream printStream)100throws IOException {101Set<SocketAddress> addrs = channel.getAllLocalAddresses();102printStream.println("Local Addresses: ");103for (Iterator<SocketAddress> it = addrs.iterator(); it.hasNext(); ) {104InetSocketAddress addr = (InetSocketAddress)it.next();105printStream.println("\t" + addr);106}107}108109/**110* Compare the contents of the given ByteBuffer with the contens of the111* given byte array. true if, and only if, the contents are the same.112*/113static boolean compare(ByteBuffer bb, byte[] message) {114if (message.length != bb.remaining()) {115out.println("Compare failed, byte array length != to buffer remaining");116return false;117}118119for (int i=0; i<message.length; i++) {120byte b = bb.get();121if (message[i] != b) {122out.println("Position " + i + ": " + message[i] + " != " + b);123return false;124}125}126127return true;128}129130static boolean compare(ByteBuffer bb, String str) {131try{132return Util.compare(bb, str.getBytes("ISO-8859-1"));133} catch (UnsupportedEncodingException unsupported) {134throw new AssertionError(unsupported);135}136}137}138139140