1 16 package org.mortbay.util; 17 18 import java.io.Serializable ; 19 import java.net.InetAddress ; 20 21 22 24 public class InetAddrPort implements Serializable 25 { 26 27 public final static String __0_0_0_0 = "0.0.0.0"; 28 29 30 private InetAddress _addr=null; 31 private boolean _addrIsHost=false; 32 private int _port=0; 33 34 35 public InetAddrPort() 36 {} 37 38 39 42 public InetAddrPort(int port) 43 { 44 _port=port; 45 } 46 47 48 52 public InetAddrPort(InetAddress addr, int port) 53 { 54 _addr=addr; 55 _port=port; 56 } 57 58 59 63 public InetAddrPort(String host, int port) 64 throws java.net.UnknownHostException 65 { 66 setHost(host); 67 setPort(port); 68 } 69 70 71 74 public InetAddrPort(String inetAddrPort) 75 throws java.net.UnknownHostException 76 { 77 int c = inetAddrPort.indexOf(':'); 78 if (c>=0) 79 { 80 String addr=inetAddrPort.substring(0,c); 81 if (addr.indexOf('/')>0) 82 addr=addr.substring(addr.indexOf('/')+1); 83 inetAddrPort=inetAddrPort.substring(c+1); 84 85 if (addr.length()>0 && ! __0_0_0_0.equals(addr)) 86 { 87 _addrIsHost=!Character.isDigit((addr.charAt(0))); 88 this._addr=InetAddress.getByName(addr); 89 } 90 } 91 92 _port = Integer.parseInt(inetAddrPort); 93 } 94 95 96 99 public InetAddrPort(InetAddrPort address) 100 { 101 if (address!=null) 102 { 103 _addr=address._addr; 104 _port=address._port; 105 } 106 } 107 108 109 112 public String getHost() 113 { 114 if (_addr==null) 115 return __0_0_0_0; 116 117 return _addrIsHost?_addr.getHostName():_addr.getHostAddress(); 118 } 119 120 121 125 public void setHost(String host) 126 throws java.net.UnknownHostException 127 { 128 _addr=null; 129 if (host!=null) 130 { 131 if (host.indexOf('/')>0) 132 host=host.substring(0,host.indexOf('/')); 133 _addrIsHost=!Character.isDigit((host.charAt(0))); 134 _addr=InetAddress.getByName(host); 135 } 136 } 137 138 139 142 public InetAddress getInetAddress() 143 { 144 return _addr; 145 } 146 147 148 151 public void setInetAddress(InetAddress addr) 152 { 153 _addrIsHost=false; 154 _addr=addr; 155 } 156 157 158 161 public int getPort() 162 { 163 return _port; 164 } 165 166 167 170 public void setPort(int port) 171 { 172 _port=port; 173 } 174 175 176 177 public String toString() 178 { 179 return getHost()+':'+_port; 180 } 181 182 183 186 public Object clone() 187 { 188 return new InetAddrPort(this); 189 } 190 191 192 195 public int hashCode() 196 { 197 return _port+((_addr==null)?0:_addr.hashCode()); 198 } 199 200 201 205 public boolean equals(Object o) 206 { 207 if (o==null) 208 return false; 209 if (o==this) 210 return true; 211 if (o instanceof InetAddrPort) 212 { 213 InetAddrPort addr=(InetAddrPort)o; 214 return addr._port==_port && 215 ( addr._addr==_addr || 216 addr._addr!=null && addr._addr.equals(_addr)); 217 } 218 return false; 219 } 220 } 221 222 223 224 225 226 227 | Popular Tags |