|
1 /* |
|
2 * Copyright 2009 Sun Microsystems, Inc. All Rights Reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 * have any questions. |
|
22 */ |
|
23 |
|
24 import java.net.NetworkInterface; |
|
25 import java.net.InetSocketAddress; |
|
26 import java.net.SocketAddress; |
|
27 import java.net.InetAddress; |
|
28 import java.net.Inet4Address; |
|
29 import java.net.SocketException; |
|
30 import java.io.IOException; |
|
31 import java.io.PrintStream; |
|
32 import java.io.UnsupportedEncodingException; |
|
33 import java.util.List; |
|
34 import java.util.ArrayList; |
|
35 import java.util.Set; |
|
36 import java.util.Collections; |
|
37 import java.util.Enumeration; |
|
38 import java.util.Iterator; |
|
39 import java.nio.ByteBuffer; |
|
40 import com.sun.nio.sctp.SctpChannel; |
|
41 import static java.lang.System.out; |
|
42 |
|
43 public class Util { |
|
44 static final int SMALL_BUFFER = 128; |
|
45 static final String SMALL_MESSAGE = |
|
46 "Under the bridge and over the dam, looking for berries, berries for jam"; |
|
47 |
|
48 static final int LARGE_BUFFER = 32768; |
|
49 static final String LARGE_MESSAGE; |
|
50 |
|
51 static { |
|
52 StringBuffer sb = new StringBuffer(LARGE_BUFFER); |
|
53 for (int i=0; i<460; i++) |
|
54 sb.append(SMALL_MESSAGE); |
|
55 |
|
56 LARGE_MESSAGE = sb.toString(); |
|
57 } |
|
58 |
|
59 static boolean isSCTPSupported() { |
|
60 try { |
|
61 SctpChannel c = SctpChannel.open(); |
|
62 c.close(); |
|
63 return true; |
|
64 } catch (IOException ioe) { |
|
65 ioe.printStackTrace(); |
|
66 } catch (UnsupportedOperationException e) { |
|
67 out.println(e); |
|
68 } |
|
69 |
|
70 return false; |
|
71 } |
|
72 /** |
|
73 * Returns a list of all the addresses on the system. |
|
74 * @param inclLoopback |
|
75 * if {@code true}, include the loopback addresses |
|
76 * @param ipv4Only |
|
77 * it {@code true}, only IPv4 addresses will be included |
|
78 */ |
|
79 static List<InetAddress> getAddresses(boolean inclLoopback, |
|
80 boolean ipv4Only) |
|
81 throws SocketException { |
|
82 ArrayList<InetAddress> list = new ArrayList<InetAddress>(); |
|
83 Enumeration<NetworkInterface> nets = |
|
84 NetworkInterface.getNetworkInterfaces(); |
|
85 for (NetworkInterface netInf : Collections.list(nets)) { |
|
86 Enumeration<InetAddress> addrs = netInf.getInetAddresses(); |
|
87 for (InetAddress addr : Collections.list(addrs)) { |
|
88 if (!list.contains(addr) && |
|
89 (inclLoopback ? true : !addr.isLoopbackAddress()) && |
|
90 (ipv4Only ? (addr instanceof Inet4Address) : true)) { |
|
91 list.add(addr); |
|
92 } |
|
93 } |
|
94 } |
|
95 |
|
96 return list; |
|
97 } |
|
98 |
|
99 static void dumpAddresses(SctpChannel channel, |
|
100 PrintStream printStream) |
|
101 throws IOException { |
|
102 Set<SocketAddress> addrs = channel.getAllLocalAddresses(); |
|
103 printStream.println("Local Addresses: "); |
|
104 for (Iterator<SocketAddress> it = addrs.iterator(); it.hasNext(); ) { |
|
105 InetSocketAddress addr = (InetSocketAddress)it.next(); |
|
106 printStream.println("\t" + addr); |
|
107 } |
|
108 } |
|
109 |
|
110 /** |
|
111 * Compare the contents of the given ByteBuffer with the contens of the |
|
112 * given byte array. true if, and only if, the contents are the same. |
|
113 */ |
|
114 static boolean compare(ByteBuffer bb, byte[] message) { |
|
115 if (message.length != bb.remaining()) { |
|
116 out.println("Compare failed, byte array length != to buffer remaining"); |
|
117 return false; |
|
118 } |
|
119 |
|
120 for (int i=0; i<message.length; i++) { |
|
121 byte b = bb.get(); |
|
122 if (message[i] != b) { |
|
123 out.println("Position " + i + ": " + message[i] + " != " + b); |
|
124 return false; |
|
125 } |
|
126 } |
|
127 |
|
128 return true; |
|
129 } |
|
130 |
|
131 static boolean compare(ByteBuffer bb, String str) { |
|
132 try{ |
|
133 return Util.compare(bb, str.getBytes("ISO-8859-1")); |
|
134 } catch (UnsupportedEncodingException unsupported) { |
|
135 throw new AssertionError(unsupported); |
|
136 } |
|
137 } |
|
138 } |