0N/A/*
5697N/A * Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage java.net;
0N/A
0N/Aimport java.io.IOException;
0N/Aimport java.io.InvalidObjectException;
5697N/Aimport java.io.ObjectInputStream;
5697N/Aimport java.io.ObjectOutputStream;
5697N/Aimport java.io.ObjectStreamException;
5697N/Aimport java.io.ObjectStreamField;
0N/A
0N/A/**
0N/A *
0N/A * This class implements an IP Socket Address (IP address + port number)
0N/A * It can also be a pair (hostname + port number), in which case an attempt
0N/A * will be made to resolve the hostname. If resolution fails then the address
0N/A * is said to be <I>unresolved</I> but can still be used on some circumstances
0N/A * like connecting through a proxy.
0N/A * <p>
0N/A * It provides an immutable object used by sockets for binding, connecting, or
0N/A * as returned values.
0N/A * <p>
0N/A * The <i>wildcard</i> is a special local IP address. It usually means "any"
0N/A * and can only be used for <code>bind</code> operations.
0N/A *
0N/A * @see java.net.Socket
0N/A * @see java.net.ServerSocket
0N/A * @since 1.4
0N/A */
5697N/Apublic class InetSocketAddress
5697N/A extends SocketAddress
5697N/A{
5697N/A // Private implementation class pointed to by all public methods.
5697N/A private static class InetSocketAddressHolder {
5697N/A // The hostname of the Socket Address
5697N/A private String hostname;
5697N/A // The IP address of the Socket Address
5697N/A private InetAddress addr;
5697N/A // The port number of the Socket Address
5697N/A private int port;
5697N/A
5697N/A private InetSocketAddressHolder(String hostname, InetAddress addr, int port) {
5697N/A this.hostname = hostname;
5697N/A this.addr = addr;
5697N/A this.port = port;
5697N/A }
5697N/A
5697N/A private int getPort() {
5697N/A return port;
5697N/A }
5697N/A
5697N/A private InetAddress getAddress() {
5697N/A return addr;
5697N/A }
5697N/A
5697N/A private String getHostName() {
5697N/A if (hostname != null)
5697N/A return hostname;
5697N/A if (addr != null)
5697N/A return addr.getHostName();
5697N/A return null;
5697N/A }
5697N/A
5697N/A private String getHostString() {
5697N/A if (hostname != null)
5697N/A return hostname;
5697N/A if (addr != null) {
5888N/A if (addr.holder().getHostName() != null)
5888N/A return addr.holder().getHostName();
5697N/A else
5697N/A return addr.getHostAddress();
5697N/A }
5697N/A return null;
5697N/A }
5697N/A
5697N/A private boolean isUnresolved() {
5697N/A return addr == null;
5697N/A }
5697N/A
5697N/A @Override
5697N/A public String toString() {
5697N/A if (isUnresolved()) {
5697N/A return hostname + ":" + port;
5697N/A } else {
5697N/A return addr.toString() + ":" + port;
5697N/A }
5697N/A }
5697N/A
5697N/A @Override
5697N/A public final boolean equals(Object obj) {
5697N/A if (obj == null || !(obj instanceof InetSocketAddressHolder))
5697N/A return false;
5697N/A InetSocketAddressHolder that = (InetSocketAddressHolder)obj;
5697N/A boolean sameIP;
5697N/A if (addr != null)
5697N/A sameIP = addr.equals(that.addr);
5697N/A else if (hostname != null)
5697N/A sameIP = (that.addr == null) &&
5697N/A hostname.equalsIgnoreCase(that.hostname);
5697N/A else
5697N/A sameIP = (that.addr == null) && (that.hostname == null);
5697N/A return sameIP && (port == that.port);
5697N/A }
5697N/A
5697N/A @Override
5697N/A public final int hashCode() {
5697N/A if (addr != null)
5697N/A return addr.hashCode() + port;
5697N/A if (hostname != null)
5697N/A return hostname.toLowerCase().hashCode() + port;
5697N/A return port;
5697N/A }
5697N/A }
5697N/A
5697N/A private final transient InetSocketAddressHolder holder;
0N/A
0N/A private static final long serialVersionUID = 5076001401234631237L;
0N/A
5697N/A private static int checkPort(int port) {
5697N/A if (port < 0 || port > 0xFFFF)
5697N/A throw new IllegalArgumentException("port out of range:" + port);
5697N/A return port;
5697N/A }
5697N/A
5697N/A private static String checkHost(String hostname) {
5697N/A if (hostname == null)
5697N/A throw new IllegalArgumentException("hostname can't be null");
5697N/A return hostname;
0N/A }
0N/A
0N/A /**
0N/A * Creates a socket address where the IP address is the wildcard address
0N/A * and the port number a specified value.
0N/A * <p>
0N/A * A valid port value is between 0 and 65535.
0N/A * A port number of <code>zero</code> will let the system pick up an
0N/A * ephemeral port in a <code>bind</code> operation.
0N/A * <p>
0N/A * @param port The port number
0N/A * @throws IllegalArgumentException if the port parameter is outside the specified
0N/A * range of valid port values.
0N/A */
0N/A public InetSocketAddress(int port) {
0N/A this(InetAddress.anyLocalAddress(), port);
0N/A }
0N/A
0N/A /**
0N/A *
0N/A * Creates a socket address from an IP address and a port number.
0N/A * <p>
0N/A * A valid port value is between 0 and 65535.
0N/A * A port number of <code>zero</code> will let the system pick up an
0N/A * ephemeral port in a <code>bind</code> operation.
0N/A * <P>
0N/A * A <code>null</code> address will assign the <i>wildcard</i> address.
0N/A * <p>
0N/A * @param addr The IP address
0N/A * @param port The port number
0N/A * @throws IllegalArgumentException if the port parameter is outside the specified
0N/A * range of valid port values.
0N/A */
0N/A public InetSocketAddress(InetAddress addr, int port) {
5697N/A holder = new InetSocketAddressHolder(
5697N/A null,
5697N/A addr == null ? InetAddress.anyLocalAddress() : addr,
5697N/A checkPort(port));
0N/A }
0N/A
0N/A /**
0N/A *
0N/A * Creates a socket address from a hostname and a port number.
0N/A * <p>
0N/A * An attempt will be made to resolve the hostname into an InetAddress.
0N/A * If that attempt fails, the address will be flagged as <I>unresolved</I>.
0N/A * <p>
0N/A * If there is a security manager, its <code>checkConnect</code> method
0N/A * is called with the host name as its argument to check the permissiom
0N/A * to resolve it. This could result in a SecurityException.
0N/A * <P>
0N/A * A valid port value is between 0 and 65535.
0N/A * A port number of <code>zero</code> will let the system pick up an
0N/A * ephemeral port in a <code>bind</code> operation.
0N/A * <P>
0N/A * @param hostname the Host name
0N/A * @param port The port number
0N/A * @throws IllegalArgumentException if the port parameter is outside the range
0N/A * of valid port values, or if the hostname parameter is <TT>null</TT>.
0N/A * @throws SecurityException if a security manager is present and
0N/A * permission to resolve the host name is
0N/A * denied.
0N/A * @see #isUnresolved()
0N/A */
0N/A public InetSocketAddress(String hostname, int port) {
5697N/A checkHost(hostname);
5697N/A InetAddress addr = null;
5697N/A String host = null;
0N/A try {
0N/A addr = InetAddress.getByName(hostname);
0N/A } catch(UnknownHostException e) {
5697N/A host = hostname;
0N/A }
5697N/A holder = new InetSocketAddressHolder(host, addr, checkPort(port));
5697N/A }
5697N/A
5697N/A // private constructor for creating unresolved instances
5697N/A private InetSocketAddress(int port, String hostname) {
5697N/A holder = new InetSocketAddressHolder(hostname, null, port);
0N/A }
0N/A
0N/A /**
0N/A *
0N/A * Creates an unresolved socket address from a hostname and a port number.
0N/A * <p>
0N/A * No attempt will be made to resolve the hostname into an InetAddress.
0N/A * The address will be flagged as <I>unresolved</I>.
0N/A * <p>
0N/A * A valid port value is between 0 and 65535.
0N/A * A port number of <code>zero</code> will let the system pick up an
0N/A * ephemeral port in a <code>bind</code> operation.
0N/A * <P>
0N/A * @param host the Host name
0N/A * @param port The port number
0N/A * @throws IllegalArgumentException if the port parameter is outside
0N/A * the range of valid port values, or if the hostname
0N/A * parameter is <TT>null</TT>.
0N/A * @see #isUnresolved()
0N/A * @return a <code>InetSocketAddress</code> representing the unresolved
0N/A * socket address
0N/A * @since 1.5
0N/A */
0N/A public static InetSocketAddress createUnresolved(String host, int port) {
5697N/A return new InetSocketAddress(checkPort(port), checkHost(host));
0N/A }
0N/A
5697N/A /**
5697N/A * @serialField hostname String
5697N/A * @serialField addr InetAddress
5697N/A * @serialField port int
5697N/A */
5697N/A private static final ObjectStreamField[] serialPersistentFields = {
5697N/A new ObjectStreamField("hostname", String.class),
5697N/A new ObjectStreamField("addr", InetAddress.class),
5697N/A new ObjectStreamField("port", int.class)};
5697N/A
5697N/A private void writeObject(ObjectOutputStream out)
5697N/A throws IOException
5697N/A {
5697N/A // Don't call defaultWriteObject()
5697N/A ObjectOutputStream.PutField pfields = out.putFields();
5697N/A pfields.put("hostname", holder.hostname);
5697N/A pfields.put("addr", holder.addr);
5697N/A pfields.put("port", holder.port);
5697N/A out.writeFields();
5697N/A }
5697N/A
5697N/A private void readObject(ObjectInputStream in)
5697N/A throws IOException, ClassNotFoundException
5697N/A {
5697N/A // Don't call defaultReadObject()
5697N/A ObjectInputStream.GetField oisFields = in.readFields();
5697N/A final String oisHostname = (String)oisFields.get("hostname", null);
5697N/A final InetAddress oisAddr = (InetAddress)oisFields.get("addr", null);
5697N/A final int oisPort = oisFields.get("port", -1);
0N/A
0N/A // Check that our invariants are satisfied
5697N/A checkPort(oisPort);
5697N/A if (oisHostname == null && oisAddr == null)
0N/A throw new InvalidObjectException("hostname and addr " +
0N/A "can't both be null");
5697N/A
5697N/A InetSocketAddressHolder h = new InetSocketAddressHolder(oisHostname,
5697N/A oisAddr,
5697N/A oisPort);
5697N/A UNSAFE.putObject(this, FIELDS_OFFSET, h);
5697N/A }
5697N/A
5697N/A private void readObjectNoData()
5697N/A throws ObjectStreamException
5697N/A {
5697N/A throw new InvalidObjectException("Stream data required");
5697N/A }
5697N/A
5697N/A private static final long FIELDS_OFFSET;
5697N/A private static final sun.misc.Unsafe UNSAFE;
5697N/A static {
5697N/A try {
5697N/A sun.misc.Unsafe unsafe = sun.misc.Unsafe.getUnsafe();
5697N/A FIELDS_OFFSET = unsafe.objectFieldOffset(
5697N/A InetSocketAddress.class.getDeclaredField("holder"));
5697N/A UNSAFE = unsafe;
5697N/A } catch (ReflectiveOperationException e) {
5697N/A throw new Error(e);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Gets the port number.
0N/A *
0N/A * @return the port number.
0N/A */
0N/A public final int getPort() {
5697N/A return holder.getPort();
0N/A }
0N/A
0N/A /**
0N/A *
0N/A * Gets the <code>InetAddress</code>.
0N/A *
0N/A * @return the InetAdress or <code>null</code> if it is unresolved.
0N/A */
0N/A public final InetAddress getAddress() {
5697N/A return holder.getAddress();
0N/A }
0N/A
0N/A /**
0N/A * Gets the <code>hostname</code>.
0N/A * Note: This method may trigger a name service reverse lookup if the
0N/A * address was created with a literal IP address.
0N/A *
0N/A * @return the hostname part of the address.
0N/A */
0N/A public final String getHostName() {
5697N/A return holder.getHostName();
0N/A }
0N/A
0N/A /**
0N/A * Returns the hostname, or the String form of the address if it
0N/A * doesn't have a hostname (it was created using a literal).
5697N/A * This has the benefit of <b>not</b> attempting a reverse lookup.
0N/A *
0N/A * @return the hostname, or String representation of the address.
0N/A * @since 1.7
0N/A */
0N/A public final String getHostString() {
5697N/A return holder.getHostString();
0N/A }
0N/A
0N/A /**
0N/A * Checks whether the address has been resolved or not.
0N/A *
0N/A * @return <code>true</code> if the hostname couldn't be resolved into
0N/A * an <code>InetAddress</code>.
0N/A */
0N/A public final boolean isUnresolved() {
5697N/A return holder.isUnresolved();
0N/A }
0N/A
0N/A /**
0N/A * Constructs a string representation of this InetSocketAddress.
0N/A * This String is constructed by calling toString() on the InetAddress
0N/A * and concatenating the port number (with a colon). If the address
0N/A * is unresolved then the part before the colon will only contain the hostname.
0N/A *
0N/A * @return a string representation of this object.
0N/A */
5697N/A @Override
0N/A public String toString() {
5697N/A return holder.toString();
0N/A }
0N/A
0N/A /**
0N/A * Compares this object against the specified object.
0N/A * The result is <code>true</code> if and only if the argument is
0N/A * not <code>null</code> and it represents the same address as
0N/A * this object.
0N/A * <p>
0N/A * Two instances of <code>InetSocketAddress</code> represent the same
0N/A * address if both the InetAddresses (or hostnames if it is unresolved) and port
0N/A * numbers are equal.
0N/A * If both addresses are unresolved, then the hostname & the port number
0N/A * are compared.
0N/A *
0N/A * Note: Hostnames are case insensitive. e.g. "FooBar" and "foobar" are
0N/A * considered equal.
0N/A *
0N/A * @param obj the object to compare against.
0N/A * @return <code>true</code> if the objects are the same;
0N/A * <code>false</code> otherwise.
0N/A * @see java.net.InetAddress#equals(java.lang.Object)
0N/A */
5697N/A @Override
0N/A public final boolean equals(Object obj) {
0N/A if (obj == null || !(obj instanceof InetSocketAddress))
0N/A return false;
5697N/A return holder.equals(((InetSocketAddress) obj).holder);
0N/A }
0N/A
0N/A /**
0N/A * Returns a hashcode for this socket address.
0N/A *
0N/A * @return a hash code value for this socket address.
0N/A */
5697N/A @Override
0N/A public final int hashCode() {
5697N/A return holder.hashCode();
0N/A }
0N/A}