1096N/A/*
2362N/A * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
1096N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1096N/A *
1096N/A * This code is free software; you can redistribute it and/or modify it
1096N/A * under the terms of the GNU General Public License version 2 only, as
1096N/A * published by the Free Software Foundation.
1096N/A *
1096N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1096N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1096N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1096N/A * version 2 for more details (a copy is included in the LICENSE file that
1096N/A * accompanied this code).
1096N/A *
1096N/A * You should have received a copy of the GNU General Public License version
1096N/A * 2 along with this work; if not, write to the Free Software Foundation,
1096N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1096N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1096N/A */
1096N/A
1096N/Aimport java.net.NetworkInterface;
1096N/Aimport java.net.InetSocketAddress;
1096N/Aimport java.net.SocketAddress;
1096N/Aimport java.net.InetAddress;
1096N/Aimport java.net.Inet4Address;
1096N/Aimport java.net.SocketException;
1096N/Aimport java.io.IOException;
1096N/Aimport java.io.PrintStream;
1096N/Aimport java.io.UnsupportedEncodingException;
1096N/Aimport java.util.List;
1096N/Aimport java.util.ArrayList;
1096N/Aimport java.util.Set;
1096N/Aimport java.util.Collections;
1096N/Aimport java.util.Enumeration;
1096N/Aimport java.util.Iterator;
1096N/Aimport java.nio.ByteBuffer;
1096N/Aimport com.sun.nio.sctp.SctpChannel;
1096N/Aimport static java.lang.System.out;
1096N/A
1096N/Apublic class Util {
1096N/A static final int SMALL_BUFFER = 128;
1096N/A static final String SMALL_MESSAGE =
1096N/A "Under the bridge and over the dam, looking for berries, berries for jam";
1096N/A
1096N/A static final int LARGE_BUFFER = 32768;
1096N/A static final String LARGE_MESSAGE;
1096N/A
1096N/A static {
1096N/A StringBuffer sb = new StringBuffer(LARGE_BUFFER);
1096N/A for (int i=0; i<460; i++)
1096N/A sb.append(SMALL_MESSAGE);
1096N/A
1096N/A LARGE_MESSAGE = sb.toString();
1096N/A }
1096N/A
1096N/A static boolean isSCTPSupported() {
1096N/A try {
1096N/A SctpChannel c = SctpChannel.open();
1096N/A c.close();
1096N/A return true;
1096N/A } catch (IOException ioe) {
1096N/A ioe.printStackTrace();
1096N/A } catch (UnsupportedOperationException e) {
1096N/A out.println(e);
1096N/A }
1096N/A
1096N/A return false;
1096N/A }
1096N/A /**
1096N/A * Returns a list of all the addresses on the system.
1096N/A * @param inclLoopback
1096N/A * if {@code true}, include the loopback addresses
1096N/A * @param ipv4Only
1096N/A * it {@code true}, only IPv4 addresses will be included
1096N/A */
1096N/A static List<InetAddress> getAddresses(boolean inclLoopback,
1096N/A boolean ipv4Only)
1096N/A throws SocketException {
1096N/A ArrayList<InetAddress> list = new ArrayList<InetAddress>();
1096N/A Enumeration<NetworkInterface> nets =
1096N/A NetworkInterface.getNetworkInterfaces();
1096N/A for (NetworkInterface netInf : Collections.list(nets)) {
1096N/A Enumeration<InetAddress> addrs = netInf.getInetAddresses();
1096N/A for (InetAddress addr : Collections.list(addrs)) {
1096N/A if (!list.contains(addr) &&
1096N/A (inclLoopback ? true : !addr.isLoopbackAddress()) &&
1096N/A (ipv4Only ? (addr instanceof Inet4Address) : true)) {
1096N/A list.add(addr);
1096N/A }
1096N/A }
1096N/A }
1096N/A
1096N/A return list;
1096N/A }
1096N/A
1096N/A static void dumpAddresses(SctpChannel channel,
1096N/A PrintStream printStream)
1096N/A throws IOException {
1096N/A Set<SocketAddress> addrs = channel.getAllLocalAddresses();
1096N/A printStream.println("Local Addresses: ");
1096N/A for (Iterator<SocketAddress> it = addrs.iterator(); it.hasNext(); ) {
1096N/A InetSocketAddress addr = (InetSocketAddress)it.next();
1096N/A printStream.println("\t" + addr);
1096N/A }
1096N/A }
1096N/A
1096N/A /**
1096N/A * Compare the contents of the given ByteBuffer with the contens of the
1096N/A * given byte array. true if, and only if, the contents are the same.
1096N/A */
1096N/A static boolean compare(ByteBuffer bb, byte[] message) {
1096N/A if (message.length != bb.remaining()) {
1096N/A out.println("Compare failed, byte array length != to buffer remaining");
1096N/A return false;
1096N/A }
1096N/A
1096N/A for (int i=0; i<message.length; i++) {
1096N/A byte b = bb.get();
1096N/A if (message[i] != b) {
1096N/A out.println("Position " + i + ": " + message[i] + " != " + b);
1096N/A return false;
1096N/A }
1096N/A }
1096N/A
1096N/A return true;
1096N/A }
1096N/A
1096N/A static boolean compare(ByteBuffer bb, String str) {
1096N/A try{
1096N/A return Util.compare(bb, str.getBytes("ISO-8859-1"));
1096N/A } catch (UnsupportedEncodingException unsupported) {
1096N/A throw new AssertionError(unsupported);
1096N/A }
1096N/A }
1096N/A}