InetSocketAddress.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /* InetSocketAddress.java --
  2. Copyright (C) 2002, 2006 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.net;
  32. /**
  33. * InetSocketAddress instances represent socket addresses
  34. * in the java.nio package. They encapsulate a InetAddress and
  35. * a port number.
  36. *
  37. * @since 1.4
  38. */
  39. public class InetSocketAddress extends SocketAddress
  40. {
  41. /**
  42. * Compatible with JDK 1.4+
  43. */
  44. private static final long serialVersionUID = 5076001401234631237L;
  45. /**
  46. * Name of host.
  47. */
  48. private String hostname;
  49. /**
  50. * Address of host.
  51. */
  52. private InetAddress addr;
  53. /**
  54. * Port of host.
  55. */
  56. private int port;
  57. /**
  58. * Constructs an InetSocketAddress instance.
  59. *
  60. * @param addr Address of the socket
  61. * @param port Port if the socket
  62. *
  63. * @exception IllegalArgumentException If the port number is illegel
  64. */
  65. public InetSocketAddress(InetAddress addr, int port)
  66. throws IllegalArgumentException
  67. {
  68. if (port < 0 || port > 65535)
  69. throw new IllegalArgumentException("Bad port number: " + port);
  70. if (addr == null)
  71. addr = InetAddress.ANY_IF;
  72. this.addr = addr;
  73. this.port = port;
  74. }
  75. /**
  76. * Constructs an InetSocketAddress instance.
  77. *
  78. * @param port Port if the socket
  79. *
  80. * @exception IllegalArgumentException If the port number is illegal
  81. */
  82. public InetSocketAddress(int port) throws IllegalArgumentException
  83. {
  84. this((InetAddress) null, port);
  85. }
  86. /**
  87. * Constructs an InetSocketAddress instance.
  88. *
  89. * @param hostname The hostname for the socket address
  90. * @param port The port for the socket address
  91. *
  92. * @exception IllegalArgumentException If the port number is illegal or
  93. * the hostname argument is null
  94. */
  95. public InetSocketAddress(String hostname, int port)
  96. {
  97. this(hostname, port, true);
  98. }
  99. /**
  100. * Constructs an InetSocketAddress instance.
  101. *
  102. * @param hostname The hostname for the socket address
  103. * @param port The port for the socket address
  104. * @param resolve <code>true</code> if the address has to be resolved,
  105. * <code>false</code> otherwise
  106. *
  107. * @exception IllegalArgumentException If the port number is illegal or
  108. * the hostname argument is null
  109. */
  110. private InetSocketAddress(String hostname, int port, boolean resolve)
  111. {
  112. if (hostname == null)
  113. throw new IllegalArgumentException("Null host name value");
  114. if (port < 0 || port > 65535)
  115. throw new IllegalArgumentException("Bad port number: " + port);
  116. this.port = port;
  117. this.hostname = hostname;
  118. this.addr = null;
  119. if (resolve)
  120. {
  121. try
  122. {
  123. this.addr = InetAddress.getByName(hostname);
  124. }
  125. catch (Exception e) // UnknownHostException, SecurityException
  126. {
  127. // Do nothing here. this.addr is already set to null.
  128. }
  129. }
  130. }
  131. /**
  132. * Creates an unresolved <code>InetSocketAddress</code> object.
  133. *
  134. * @param hostname The hostname for the socket address
  135. * @param port The port for the socket address
  136. *
  137. * @exception IllegalArgumentException If the port number is illegal or
  138. * the hostname argument is null
  139. *
  140. * @since 1.5
  141. */
  142. public static InetSocketAddress createUnresolved(String hostname, int port)
  143. {
  144. return new InetSocketAddress(hostname, port, false);
  145. }
  146. /**
  147. * Test if obj is a <code>InetSocketAddress</code> and
  148. * has the same address and port
  149. *
  150. * @param obj The obj to compare this address with.
  151. *
  152. * @return True if obj is equal.
  153. */
  154. public final boolean equals(Object obj)
  155. {
  156. // InetSocketAddress objects are equal when addr and port are equal.
  157. // The hostname may differ.
  158. if (obj instanceof InetSocketAddress)
  159. {
  160. InetSocketAddress sa = (InetSocketAddress) obj;
  161. if (addr == null && sa.addr != null)
  162. return false;
  163. else if (addr == null && sa.addr == null) // we know hostname != null
  164. return hostname.equals(sa.hostname) && sa.port == port;
  165. else
  166. return addr.equals(sa.addr) && sa.port == port;
  167. }
  168. return false;
  169. }
  170. /**
  171. * Returns the <code>InetAddress</code> or
  172. * <code>null</code> if its unresolved
  173. *
  174. * @return The IP address of this address.
  175. */
  176. public final InetAddress getAddress()
  177. {
  178. return addr;
  179. }
  180. /**
  181. * Returns <code>hostname</code>
  182. *
  183. * @return The hostname of this address.
  184. */
  185. public final String getHostName()
  186. {
  187. if (hostname == null) // we know addr != null
  188. hostname = addr.getHostName();
  189. return hostname;
  190. }
  191. /**
  192. * Returns the <code>port</code>
  193. *
  194. * @return The port of this address.
  195. */
  196. public final int getPort()
  197. {
  198. return port;
  199. }
  200. /**
  201. * Returns the hashcode of the <code>InetSocketAddress</code>
  202. *
  203. * @return The hashcode for this address.
  204. */
  205. public final int hashCode()
  206. {
  207. return port + addr.hashCode();
  208. }
  209. /**
  210. * Checks wether the address has been resolved or not
  211. *
  212. * @return True if address is unresolved.
  213. */
  214. public final boolean isUnresolved()
  215. {
  216. return addr == null;
  217. }
  218. /**
  219. * Returns the <code>InetSocketAddress</code> as string
  220. *
  221. * @return A string representation of this address.
  222. */
  223. public String toString()
  224. {
  225. // Note: if addr is null, then hostname != null.
  226. return (addr == null ? hostname : addr.toString()) + ":" + port;
  227. }
  228. }