Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 6th, 2012  |  syntax: None  |  size: 3.59 KB  |  hits: 6  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Java: Mysterious Java uncaught exception handler [with code]
  2. package staticexception;
  3.  
  4. import java.net.InterfaceAddress;
  5. import java.net.NetworkInterface;
  6. import java.net.SocketException;
  7. import java.util.ArrayList;
  8. import java.util.Collections;
  9. import java.util.Enumeration;
  10. import java.util.List;
  11. import javax.swing.UIManager;
  12.  
  13. public class StaticException {
  14.     // Don't need this fully implemented.
  15.     public static class IPAddress {
  16.  
  17.         public static IPAddress getBroadcast(IPAddress mask, IPAddress myip) {
  18.             return new IPAddress();
  19.         }
  20.  
  21.         public IPAddress() {}
  22.  
  23.         public IPAddress(int maskval) {}
  24.  
  25.         public IPAddress(byte[] addr) {}
  26.  
  27.         public IPAddress mask(IPAddress netmask) {
  28.             return this;
  29.         }
  30.  
  31.         public int prefixLength() {
  32.             return 0;
  33.         }
  34.  
  35.     }
  36.  
  37.     public static class Network {
  38.         public IPAddress broadcast, netmask, ip;
  39.         boolean remember;
  40.  
  41.         public Network(IPAddress br, IPAddress nm, IPAddress ip) {
  42.             broadcast = br;
  43.             netmask = nm;
  44.             this.ip = ip;
  45.         }
  46.  
  47.         boolean match(IPAddress ip) {
  48.             IPAddress a = ip.mask(netmask);
  49.             IPAddress b = this.ip.mask(netmask);
  50.             return (a.equals(b));
  51.         }
  52.  
  53.         @Override
  54.         public String toString() {
  55.             return ip.toString() + "/" + netmask.prefixLength();
  56.         }
  57.     }
  58.  
  59.     static List<Network> my_networks;
  60.  
  61.     static void enumerateNetworks() {
  62.         my_networks = new ArrayList<Network>();
  63.  
  64.         Enumeration<NetworkInterface> nets = null;
  65.         try {
  66.             nets = NetworkInterface.getNetworkInterfaces();
  67.         } catch (SocketException ex) {
  68.             ex.printStackTrace();
  69.         }
  70.         for (NetworkInterface netint : Collections.list(nets)) {
  71.             for (InterfaceAddress address : netint.getInterfaceAddresses()) {
  72.                 // *** Exception would occur on the next line when
  73.                 // *** address.getAddress() would return null
  74.                 byte[] addr = address.getAddress().getAddress();
  75.                 if (addr.length == 4 && addr[0] != 127) {
  76.                     int prefixlen = address.getNetworkPrefixLength();
  77.                     int maskval = -1 << (32 - prefixlen);
  78.                     IPAddress mask = new IPAddress(maskval);
  79.                     //my_netmask = mask;
  80.                     System.out.println("Netmask   = " + mask);
  81.  
  82.                     IPAddress myip = new IPAddress(addr);
  83.                     //my_ip_address = myip;
  84.                     System.out.println("Local IP  = " + myip);
  85.  
  86.                     IPAddress broadcast = IPAddress.getBroadcast(mask, myip);
  87.                     System.out.println("Broadcast = " + broadcast);
  88.  
  89.                     my_networks.add(new Network(broadcast, mask, myip));
  90.  
  91.                     System.out.print(address.getAddress().getAddress().length + " ");
  92.                     System.out.print(address.getAddress() + " ");
  93.                     System.out.print(address.getAddress().getHostAddress() + " ");
  94.                     System.out.println(address.getNetworkPrefixLength());
  95.                 }
  96.             }
  97.         }
  98.     }
  99.  
  100.     static private void setupNetwork() {
  101.         System.setProperty("java.net.preferIPv4Stack","true");
  102.  
  103.         enumerateNetworks();
  104.  
  105.         // ... stuff that would happen after the exception
  106.     }
  107.  
  108.     public static void main(String[] args) {
  109.         try {        
  110.             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());    
  111.         } catch (Exception e) {}
  112.  
  113.         setupNetwork();
  114.  
  115.         // ... stuff that would happen after the exception
  116.     }
  117. }