119 else if (hostname != null)
120 sameIP = (that.addr == null) &&
121 hostname.equalsIgnoreCase(that.hostname);
122 else
123 sameIP = (that.addr == null) && (that.hostname == null);
124 return sameIP && (port == that.port);
125 }
126
127 @Override
128 public final int hashCode() {
129 if (addr != null)
130 return addr.hashCode() + port;
131 if (hostname != null)
132 return hostname.toLowerCase().hashCode() + port;
133 return port;
134 }
135 }
136
137 private final transient InetSocketAddressHolder holder;
138
139 private static final long serialVersionUID = 5076001401234631237L;
140
141 private static int checkPort(int port) {
142 if (port < 0 || port > 0xFFFF)
143 throw new IllegalArgumentException("port out of range:" + port);
144 return port;
145 }
146
147 private static String checkHost(String hostname) {
148 if (hostname == null)
149 throw new IllegalArgumentException("hostname can't be null");
150 return hostname;
151 }
152
153 /**
154 * Creates a socket address where the IP address is the wildcard address
155 * and the port number a specified value.
156 * <p>
157 * A valid port value is between 0 and 65535.
158 * A port number of {@code zero} will let the system pick up an
242 *
243 * @param host the Host name
244 * @param port The port number
245 * @throws IllegalArgumentException if the port parameter is outside
246 * the range of valid port values, or if the hostname
247 * parameter is {@code null}.
248 * @see #isUnresolved()
249 * @return an {@code InetSocketAddress} representing the unresolved
250 * socket address
251 * @since 1.5
252 */
253 public static InetSocketAddress createUnresolved(String host, int port) {
254 return new InetSocketAddress(checkPort(port), checkHost(host));
255 }
256
257 /**
258 * @serialField hostname String
259 * @serialField addr InetAddress
260 * @serialField port int
261 */
262 private static final ObjectStreamField[] serialPersistentFields = {
263 new ObjectStreamField("hostname", String.class),
264 new ObjectStreamField("addr", InetAddress.class),
265 new ObjectStreamField("port", int.class)};
266
267 private void writeObject(ObjectOutputStream out)
268 throws IOException
269 {
270 // Don't call defaultWriteObject()
271 ObjectOutputStream.PutField pfields = out.putFields();
272 pfields.put("hostname", holder.hostname);
273 pfields.put("addr", holder.addr);
274 pfields.put("port", holder.port);
275 out.writeFields();
276 }
277
278 private void readObject(ObjectInputStream in)
279 throws IOException, ClassNotFoundException
280 {
281 // Don't call defaultReadObject()
282 ObjectInputStream.GetField oisFields = in.readFields();
283 final String oisHostname = (String)oisFields.get("hostname", null);
284 final InetAddress oisAddr = (InetAddress)oisFields.get("addr", null);
285 final int oisPort = oisFields.get("port", -1);
286
287 // Check that our invariants are satisfied
288 checkPort(oisPort);
289 if (oisHostname == null && oisAddr == null)
290 throw new InvalidObjectException("hostname and addr " +
291 "can't both be null");
292
293 InetSocketAddressHolder h = new InetSocketAddressHolder(oisHostname,
294 oisAddr,
295 oisPort);
296 UNSAFE.putReference(this, FIELDS_OFFSET, h);
297 }
298
299 private void readObjectNoData()
300 throws ObjectStreamException
301 {
302 throw new InvalidObjectException("Stream data required");
303 }
304
305 private static final jdk.internal.misc.Unsafe UNSAFE
306 = jdk.internal.misc.Unsafe.getUnsafe();
307 private static final long FIELDS_OFFSET
308 = UNSAFE.objectFieldOffset(InetSocketAddress.class, "holder");
309
310 /**
311 * Gets the port number.
312 *
313 * @return the port number.
314 */
315 public final int getPort() {
316 return holder.getPort();
317 }
318
|
119 else if (hostname != null)
120 sameIP = (that.addr == null) &&
121 hostname.equalsIgnoreCase(that.hostname);
122 else
123 sameIP = (that.addr == null) && (that.hostname == null);
124 return sameIP && (port == that.port);
125 }
126
127 @Override
128 public final int hashCode() {
129 if (addr != null)
130 return addr.hashCode() + port;
131 if (hostname != null)
132 return hostname.toLowerCase().hashCode() + port;
133 return port;
134 }
135 }
136
137 private final transient InetSocketAddressHolder holder;
138
139 @java.io.Serial
140 private static final long serialVersionUID = 5076001401234631237L;
141
142 private static int checkPort(int port) {
143 if (port < 0 || port > 0xFFFF)
144 throw new IllegalArgumentException("port out of range:" + port);
145 return port;
146 }
147
148 private static String checkHost(String hostname) {
149 if (hostname == null)
150 throw new IllegalArgumentException("hostname can't be null");
151 return hostname;
152 }
153
154 /**
155 * Creates a socket address where the IP address is the wildcard address
156 * and the port number a specified value.
157 * <p>
158 * A valid port value is between 0 and 65535.
159 * A port number of {@code zero} will let the system pick up an
243 *
244 * @param host the Host name
245 * @param port The port number
246 * @throws IllegalArgumentException if the port parameter is outside
247 * the range of valid port values, or if the hostname
248 * parameter is {@code null}.
249 * @see #isUnresolved()
250 * @return an {@code InetSocketAddress} representing the unresolved
251 * socket address
252 * @since 1.5
253 */
254 public static InetSocketAddress createUnresolved(String host, int port) {
255 return new InetSocketAddress(checkPort(port), checkHost(host));
256 }
257
258 /**
259 * @serialField hostname String
260 * @serialField addr InetAddress
261 * @serialField port int
262 */
263 @java.io.Serial
264 private static final ObjectStreamField[] serialPersistentFields = {
265 new ObjectStreamField("hostname", String.class),
266 new ObjectStreamField("addr", InetAddress.class),
267 new ObjectStreamField("port", int.class)};
268
269 @java.io.Serial
270 private void writeObject(ObjectOutputStream out)
271 throws IOException
272 {
273 // Don't call defaultWriteObject()
274 ObjectOutputStream.PutField pfields = out.putFields();
275 pfields.put("hostname", holder.hostname);
276 pfields.put("addr", holder.addr);
277 pfields.put("port", holder.port);
278 out.writeFields();
279 }
280
281 @java.io.Serial
282 private void readObject(ObjectInputStream in)
283 throws IOException, ClassNotFoundException
284 {
285 // Don't call defaultReadObject()
286 ObjectInputStream.GetField oisFields = in.readFields();
287 final String oisHostname = (String)oisFields.get("hostname", null);
288 final InetAddress oisAddr = (InetAddress)oisFields.get("addr", null);
289 final int oisPort = oisFields.get("port", -1);
290
291 // Check that our invariants are satisfied
292 checkPort(oisPort);
293 if (oisHostname == null && oisAddr == null)
294 throw new InvalidObjectException("hostname and addr " +
295 "can't both be null");
296
297 InetSocketAddressHolder h = new InetSocketAddressHolder(oisHostname,
298 oisAddr,
299 oisPort);
300 UNSAFE.putReference(this, FIELDS_OFFSET, h);
301 }
302
303 @java.io.Serial
304 private void readObjectNoData()
305 throws ObjectStreamException
306 {
307 throw new InvalidObjectException("Stream data required");
308 }
309
310 private static final jdk.internal.misc.Unsafe UNSAFE
311 = jdk.internal.misc.Unsafe.getUnsafe();
312 private static final long FIELDS_OFFSET
313 = UNSAFE.objectFieldOffset(InetSocketAddress.class, "holder");
314
315 /**
316 * Gets the port number.
317 *
318 * @return the port number.
319 */
320 public final int getPort() {
321 return holder.getPort();
322 }
323
|