*
* @since 1.1
*/
- public boolean isMulticastAddress ()
+ public boolean isMulticastAddress()
{
int len = addr.length;
*
* @since 1.4
*/
- public boolean isAnyLocalAddress ()
+ public boolean isAnyLocalAddress()
{
// This is the IPv4 implementation.
// Any class derived from InetAddress should override this.
*
* @since 1.4
*/
- public boolean isLoopbackAddress ()
+ public boolean isLoopbackAddress()
{
// This is the IPv4 implementation.
// Any class derived from InetAddress should override this.
*
* @since 1.4
*/
- public boolean isLinkLocalAddress ()
+ public boolean isLinkLocalAddress()
{
// This is the IPv4 implementation.
// Any class derived from InetAddress should override this.
*
* @since 1.4
*/
- public boolean isSiteLocalAddress ()
+ public boolean isSiteLocalAddress()
{
// This is the IPv4 implementation.
// Any class derived from InetAddress should override this.
*
* @since 1.4
*/
- public boolean isMCGlobal ()
+ public boolean isMCGlobal()
{
// This is the IPv4 implementation.
// Any class derived from InetAddress should override this.
*
* @since 1.4
*/
- public boolean isMCNodeLocal ()
+ public boolean isMCNodeLocal()
{
// This is the IPv4 implementation.
// Any class derived from InetAddress should override this.
*
* @since 1.4
*/
- public boolean isMCLinkLocal ()
+ public boolean isMCLinkLocal()
{
// This is the IPv4 implementation.
// Any class derived from InetAddress should override this.
- if (!isMulticastAddress ())
+ if (!isMulticastAddress())
return false;
return (addr [0] == 0xE0
*
* @since 1.4
*/
- public boolean isMCSiteLocal ()
+ public boolean isMCSiteLocal()
{
// This is the IPv4 implementation.
// Any class derived from InetAddress should override this.
*
* @since 1.4
*/
- public boolean isMCOrgLocal ()
+ public boolean isMCOrgLocal()
{
// This is the IPv4 implementation.
// Any class derived from InetAddress should override this.
/**
* Returns the hostname represented by this InetAddress
*/
- public String getHostName ()
+ public String getHostName()
{
if (hostName == null)
lookup (null, this, false);
*
* @since 1.4
*/
- public String getCanonicalHostName ()
+ public String getCanonicalHostName()
{
- SecurityManager sm = System.getSecurityManager ();
+ SecurityManager sm = System.getSecurityManager();
if (sm != null)
{
try
}
catch (SecurityException e)
{
- return getHostAddress ();
+ return getHostAddress();
}
}
// Try to find the FDQN now
- InetAddress address = new InetAddress (getAddress (), null);
- return address.getHostName ();
+ InetAddress address = new InetAddress (getAddress(), null);
+ return address.getHostName();
}
/**
* Returns the IP address of this InetAddress as array of bytes
*/
- public byte[] getAddress ()
+ public byte[] getAddress()
{
// An experiment shows that JDK1.2 returns a different byte array each
// time. This makes sense, in terms of security.
- return (byte[]) addr.clone ();
+ return (byte[]) addr.clone();
}
/* Helper function due to a CNI limitation. */
/* Helper function due to a CNI limitation. */
private static SecurityException checkConnect (String hostname)
{
- SecurityManager s = System.getSecurityManager ();
+ SecurityManager s = System.getSecurityManager();
if (s == null)
return null;
*
* @since 1.0.2
*/
- public String getHostAddress ()
+ public String getHostAddress()
{
- StringBuffer sbuf = new StringBuffer (40);
+ StringBuffer sb = new StringBuffer (40);
int len = addr.length;
int i = 0;
+
if (len == 16)
{ // An IPv6 address.
- for (; ; i += 2)
+ for ( ; ; i += 2)
{
if (i >= 16)
- return sbuf.toString ();
+ return sb.toString();
+
int x = ((addr [i] & 0xFF) << 8) | (addr [i + 1] & 0xFF);
- boolean empty = sbuf.length () == 0;
+ boolean empty = sb.length() == 0;
+
if (empty)
{
if (i == 10 && x == 0xFFFF)
{ // IPv4-mapped IPv6 address.
- sbuf.append (":FFFF:");
+ sb.append (":FFFF:");
break; // Continue as IPv4 address;
}
else if (i == 12)
{ // IPv4-compatible IPv6 address.
- sbuf.append (':');
+ sb.append (':');
break; // Continue as IPv4 address.
}
else if (i > 0)
- sbuf.append ("::");
+ sb.append ("::");
}
else
- sbuf.append (':');
+ sb.append (':');
+
if (x != 0 || i >= 14)
- sbuf.append (Integer.toHexString (x).toUpperCase ());
+ sb.append (Integer.toHexString (x).toUpperCase());
}
}
- for ( ; ; )
+
+ for ( ; ; )
{
- sbuf.append (addr[i] & 0xFF);
+ sb.append (addr [i] & 0xff);
i++;
+
if (i == len)
break;
- sbuf.append ('.');
+
+ sb.append ('.');
}
- return sbuf.toString();
+ return sb.toString();
}
/**
* Returns a hashcode of the InetAddress
*/
- public int hashCode ()
+ public int hashCode()
{
// There hashing algorithm is not specified, but a simple experiment
// shows that it is equal to the address, as a 32-bit big-endian integer.
/**
* Returns then <code>InetAddress</code> as string
*/
- public String toString ()
+ public String toString()
{
String result;
- String address = getHostAddress ();
+ String address = getHostAddress();
if (hostName != null)
result = hostName + "/" + address;
public static InetAddress getByName (String hostname)
throws UnknownHostException
{
- SecurityManager s = System.getSecurityManager ();
+ SecurityManager s = System.getSecurityManager();
if (s != null)
s.checkConnect (hostname, -1);
// Default to current host if necessary
if (hostname == null)
- return getLocalHost ();
+ return getLocalHost();
// Assume that the host string is an IP address
byte[] address = aton (hostname);
public static InetAddress[] getAllByName (String hostname)
throws UnknownHostException
{
- SecurityManager s = System.getSecurityManager ();
+ SecurityManager s = System.getSecurityManager();
if (s != null)
s.checkConnect (hostname, -1);
return lookup (hostname, null, true);
}
- private static native String getLocalHostname ();
+ private static native String getLocalHostname();
/**
- * Returns the local host
+ * Returns the local host address.
*
* @exception UnknownHostException If no IP address for the host could
* be found
*/
- public static InetAddress getLocalHost () throws UnknownHostException
+ public static InetAddress getLocalHost() throws UnknownHostException
{
- SecurityManager s = System.getSecurityManager ();
+ SecurityManager s = System.getSecurityManager();
// Experimentation shows that JDK1.2 does cache the result.
// However, if there is a security manager, and the cached result
if (s == null && localhost != null)
return;
- String hostname = getLocalHostname ();
+ String hostname = getLocalHostname();
if (s != null)
{
/**
* Needed for serialization
*/
- private void readResolve () throws ObjectStreamException
+ private void readResolve() throws ObjectStreamException
{
// FIXME: implement this
}
private void readObject (ObjectInputStream ois)
throws IOException, ClassNotFoundException
{
- ois.defaultReadObject ();
+ ois.defaultReadObject();
addr = new byte [4];
addr [3] = (byte) address;
for (; i < len; i++)
address = address << 8 | (((int) addr [i]) & 0xFF);
- oos.defaultWriteObject ();
+ oos.defaultWriteObject();
}
}