1 17 package org.alfresco.filesys.netbios; 18 19 import java.net.InetAddress ; 20 21 25 public class NetworkSettings 26 { 27 28 30 private static String m_broadcastMask; 31 32 34 private static String m_domain; 35 36 38 private static InetAddress m_subnetAddr; 39 40 45 public static String GenerateBroadcastMask(String addr) throws java.net.UnknownHostException 46 { 47 48 50 if (m_broadcastMask != null) 51 return m_broadcastMask; 52 53 55 String localIP = addr; 56 57 if (localIP == null) 58 localIP = InetAddress.getLocalHost().getHostAddress(); 59 60 62 int dotPos = localIP.indexOf('.'); 63 if (dotPos != -1) 64 { 65 66 68 String ipStr = localIP.substring(0, dotPos); 69 int ipVal = Integer.valueOf(ipStr).intValue(); 70 71 73 if (ipVal <= 127) 74 { 75 76 78 m_broadcastMask = "" + ipVal + ".255.255.255"; 79 } 80 else if (ipVal <= 191) 81 { 82 83 85 dotPos++; 86 while (localIP.charAt(dotPos) != '.' && dotPos < localIP.length()) 87 dotPos++; 88 89 if (dotPos < localIP.length()) 90 m_broadcastMask = localIP.substring(0, dotPos) + ".255.255"; 91 } 92 else if (ipVal <= 223) 93 { 94 95 97 dotPos++; 98 int dotCnt = 1; 99 100 while (dotCnt < 3 && dotPos < localIP.length()) 101 { 102 103 105 if (localIP.charAt(dotPos++) == '.') 106 dotCnt++; 107 } 108 109 if (dotPos < localIP.length()) 110 m_broadcastMask = localIP.substring(0, dotPos - 1) + ".255"; 111 } 112 } 113 114 117 if (m_broadcastMask == null) 118 { 119 120 123 m_broadcastMask = "255.255.255.255"; 124 } 125 126 128 return m_broadcastMask; 129 } 130 131 136 public final static InetAddress getBroadcastAddress() throws java.net.UnknownHostException 137 { 138 139 141 if (m_subnetAddr == null) 142 { 143 144 146 String subnet = GenerateBroadcastMask(null); 147 m_subnetAddr = InetAddress.getByName(subnet); 148 } 149 150 152 return m_subnetAddr; 153 } 154 155 160 public static String getBroadcastMask() 161 { 162 return m_broadcastMask; 163 } 164 165 168 public static String getDomain() 169 { 170 return m_domain; 171 } 172 173 176 public static boolean hasBroadcastMask() 177 { 178 if (m_broadcastMask == null) 179 return false; 180 return true; 181 } 182 183 188 public static void setBroadcastMask(String mask) 189 { 190 m_broadcastMask = mask; 191 } 192 193 198 public static void setDomain(String domain) 199 { 200 m_domain = domain; 201 } 202 } | Popular Tags |