InetSocketAddress.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* InetSocketAddress.java --
  2. Copyright (C) 2002 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., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 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. String hostname;
  46. InetAddress addr;
  47. int port;
  48. /**
  49. * Constructs an InetSocketAddress instance.
  50. *
  51. * @param addr Address of the socket
  52. * @param port Port if the socket
  53. *
  54. * @exception IllegalArgumentException If the port number is illegel
  55. */
  56. public InetSocketAddress(InetAddress addr, int port)
  57. throws IllegalArgumentException
  58. {
  59. if (port < 0 || port > 65535)
  60. throw new IllegalArgumentException();
  61. this.addr = addr;
  62. this.port = port;
  63. this.hostname = addr.getHostName ();
  64. }
  65. /**
  66. * Constructs an InetSocketAddress instance.
  67. *
  68. * @param port Port if the socket
  69. *
  70. * @exception IllegalArgumentException If the port number is illegal
  71. */
  72. public InetSocketAddress(int port)
  73. throws IllegalArgumentException
  74. {
  75. if (port < 0 || port > 65535)
  76. throw new IllegalArgumentException();
  77. this.port = port;
  78. try
  79. {
  80. byte[] any = { 0, 0, 0, 0 };
  81. this.addr = InetAddress.getByAddress (any);
  82. this.hostname = "0.0.0.0";
  83. }
  84. catch (UnknownHostException e)
  85. {
  86. this.addr = null;
  87. this.hostname = "";
  88. }
  89. }
  90. /**
  91. * Constructs an InetSocketAddress instance.
  92. *
  93. * @param addr Address of the socket
  94. * @param port Port if the socket
  95. *
  96. * @exception IllegalArgumentException If the port number is illegal
  97. */
  98. public InetSocketAddress(String hostname, int port)
  99. throws IllegalArgumentException
  100. {
  101. if (port < 0 || port > 65535)
  102. throw new IllegalArgumentException();
  103. this.port = port;
  104. this.hostname = hostname;
  105. try
  106. {
  107. this.addr = InetAddress.getByName(hostname);
  108. }
  109. catch (Exception e) // UnknownHostException, SecurityException
  110. {
  111. this.addr = null;
  112. }
  113. }
  114. /**
  115. * Test if obj is a <code>InetSocketAddress</code> and
  116. * has the same address and port
  117. */
  118. public final boolean equals (Object obj)
  119. {
  120. // InetSocketAddress objects are equal when addr and port are equal.
  121. // The hostname may differ.
  122. if (obj instanceof InetSocketAddress)
  123. {
  124. InetSocketAddress a = (InetSocketAddress) obj;
  125. return addr.equals(a.addr) && a.port == port;
  126. }
  127. return false;
  128. }
  129. /**
  130. * Returns the <code>InetAddress</code> or
  131. * <code>null</code> if its unresolved
  132. */
  133. public final InetAddress getAddress()
  134. {
  135. return addr;
  136. }
  137. /**
  138. * Returns <code>hostname</code>
  139. */
  140. public final String getHostName()
  141. {
  142. return hostname;
  143. }
  144. /**
  145. * Returns the <code>port</code>
  146. */
  147. public final int getPort()
  148. {
  149. return port;
  150. }
  151. /**
  152. * Returns the hashcode of the <code>InetSocketAddress</code>
  153. */
  154. public final int hashCode()
  155. {
  156. return port + addr.hashCode();
  157. }
  158. /**
  159. * Checks wether the address has been resolved or not
  160. */
  161. public final boolean isUnresolved()
  162. {
  163. return addr == null;
  164. }
  165. /**
  166. * Returns the <code>InetSocketAddress</code> as string
  167. */
  168. public String toString()
  169. {
  170. return addr + ":" + port;
  171. }
  172. }