001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package java.net;
019:
020: import java.io.IOException;
021: import java.io.ObjectInputStream;
022:
023: public class InetSocketAddress extends SocketAddress {
024:
025: private static final long serialVersionUID = 5076001401234631237L;
026:
027: private String hostname;
028:
029: private InetAddress addr;
030:
031: private int port;
032:
033: public InetSocketAddress(int port) {
034: this ((InetAddress) null, port);
035: }
036:
037: public InetSocketAddress(InetAddress address, int port) {
038: if (port < 0 || port > 65535) {
039: throw new IllegalArgumentException();
040: }
041: if (address == null) {
042: addr = InetAddress.ANY;
043: } else {
044: addr = address;
045: }
046: hostname = addr.getHostName();
047: this .port = port;
048: }
049:
050: public InetSocketAddress(String host, int port) {
051: this (host, port, true);
052: }
053:
054: /*
055: * Internal constructor for InetSocketAddress(String, int) and
056: * createUnresolved(String, int);
057: */
058: InetSocketAddress(String host, int port, boolean needResolved) {
059: if (host == null || port < 0 || port > 65535) {
060: throw new IllegalArgumentException();
061: }
062: hostname = host;
063: this .port = port;
064: if (needResolved) {
065: try {
066: addr = InetAddress.getByName(hostname);
067: hostname = null;
068: } catch (UnknownHostException e) {
069: // Ignored
070: }
071: } else {
072: addr = null;
073: }
074: }
075:
076: /**
077: * Creats an <code>InetSocketAddress</code> without trying to resolve
078: * hostname into an InetAddress. The address field is marked as unresolved.
079: *
080: * @param host
081: * @param port
082: * @return an <code>InetSocketAddress</code> instance.
083: * @throws IllegalArgumentException
084: * if host is null or the port is not in the range between 0 and
085: * 65535.
086: */
087: public static InetSocketAddress createUnresolved(String host,
088: int port) {
089: return new InetSocketAddress(host, port, false);
090: }
091:
092: public final int getPort() {
093: return port;
094: }
095:
096: public final InetAddress getAddress() {
097: return addr;
098: }
099:
100: public final String getHostName() {
101: return (null != addr) ? addr.getHostName() : hostname;
102: }
103:
104: public final boolean isUnresolved() {
105: return addr == null;
106: }
107:
108: @Override
109: public String toString() {
110: String host;
111: if (addr != null) {
112: host = addr.toString();
113: } else {
114: host = hostname;
115: }
116: return host + ":" + port; //$NON-NLS-1$
117: }
118:
119: @Override
120: public final boolean equals(Object socketAddr) {
121: if (this == socketAddr) {
122: return true;
123: }
124: if (!(socketAddr instanceof InetSocketAddress)) {
125: return false;
126: }
127: InetSocketAddress iSockAddr = (InetSocketAddress) socketAddr;
128:
129: // check the ports as we always need to do this
130: if (port != iSockAddr.port) {
131: return false;
132: }
133:
134: // we only use the hostnames in the comparison if the addrs were not
135: // resolved
136: if ((addr == null) && (iSockAddr.addr == null)) {
137: return hostname.equals(iSockAddr.hostname);
138: }
139:
140: // addrs were resolved so use them for the comparison
141: if (addr == null) {
142: // if we are here we know iSockAddr is not null so just return
143: // false
144: return false;
145: }
146: return addr.equals(iSockAddr.addr);
147: }
148:
149: @Override
150: public final int hashCode() {
151: if (addr == null) {
152: return hostname.hashCode() + port;
153: }
154: return addr.hashCode() + port;
155: }
156:
157: private void readObject(ObjectInputStream stream)
158: throws IOException, ClassNotFoundException {
159: stream.defaultReadObject();
160: }
161: }
|