< prev index next >

src/java.base/share/classes/java/net/InetAddress.java

Print this page




 282         }
 283     }
 284 
 285     /* Used to store the serializable fields of InetAddress */
 286     final transient InetAddressHolder holder;
 287 
 288     InetAddressHolder holder() {
 289         return holder;
 290     }
 291 
 292     /* Used to store the name service provider */
 293     private static transient NameService nameService;
 294 
 295     /**
 296      * Used to store the best available hostname.
 297      * Lazily initialized via a data race; safe because Strings are immutable.
 298      */
 299     private transient String canonicalHostName = null;
 300 
 301     /** use serialVersionUID from JDK 1.0.2 for interoperability */

 302     private static final long serialVersionUID = 3286316764910316507L;
 303 
 304     /*
 305      * Load net library into runtime, and perform initializations.
 306      */
 307     static {
 308         String str = GetPropertyAction.privilegedGetProperty("java.net.preferIPv6Addresses");
 309         if (str == null) {
 310             preferIPv6Address = PREFER_IPV4_VALUE;
 311         } else if (str.equalsIgnoreCase("true")) {
 312             preferIPv6Address = PREFER_IPV6_VALUE;
 313         } else if (str.equalsIgnoreCase("false")) {
 314             preferIPv6Address = PREFER_IPV4_VALUE;
 315         } else if (str.equalsIgnoreCase("system")) {
 316             preferIPv6Address = PREFER_SYSTEM_VALUE;
 317         } else {
 318             preferIPv6Address = PREFER_IPV4_VALUE;
 319         }
 320         jdk.internal.loader.BootLoader.loadLibrary("net");
 321         SharedSecrets.setJavaNetInetAddressAccess(


 336     }
 337 
 338     /**
 339      * Constructor for the Socket.accept() method.
 340      * This creates an empty InetAddress, which is filled in by
 341      * the accept() method.  This InetAddress, however, is not
 342      * put in the address cache, since it is not created by name.
 343      */
 344     InetAddress() {
 345         holder = new InetAddressHolder();
 346     }
 347 
 348     /**
 349      * Replaces the de-serialized object with an Inet4Address object.
 350      *
 351      * @return the alternate object to the de-serialized object.
 352      *
 353      * @throws ObjectStreamException if a new object replacing this
 354      * object could not be created
 355      */

 356     private Object readResolve() throws ObjectStreamException {
 357         // will replace the deserialized 'this' object
 358         return new Inet4Address(holder().getHostName(), holder().getAddress());
 359     }
 360 
 361     /**
 362      * Utility routine to check if the InetAddress is an
 363      * IP multicast address.
 364      * @return a {@code boolean} indicating if the InetAddress is
 365      * an IP multicast address
 366      * @since   1.1
 367      */
 368     public boolean isMulticastAddress() {
 369         return false;
 370     }
 371 
 372     /**
 373      * Utility routine to check if the InetAddress is a wildcard address.
 374      * @return a {@code boolean} indicating if the InetAddress is
 375      *         a wildcard address.


1670                                "in your properties file.");
1671         } catch (IllegalAccessException e) {
1672             System.err.println("Cannot access class: java.net." + prefix +
1673                                implName + ":\ncheck impl.prefix property " +
1674                                "in your properties file.");
1675         }
1676 
1677         if (impl == null) {
1678             try {
1679                 @SuppressWarnings("deprecation")
1680                 Object tmp = Class.forName(implName).newInstance();
1681                 impl = tmp;
1682             } catch (Exception e) {
1683                 throw new Error("System property impl.prefix incorrect");
1684             }
1685         }
1686 
1687         return (InetAddressImpl) impl;
1688     }
1689 

1690     private void readObjectNoData () {
1691         if (getClass().getClassLoader() != null) {
1692             throw new SecurityException ("invalid address type");
1693         }
1694     }
1695 
1696     private static final jdk.internal.misc.Unsafe UNSAFE
1697             = jdk.internal.misc.Unsafe.getUnsafe();
1698     private static final long FIELDS_OFFSET
1699             = UNSAFE.objectFieldOffset(InetAddress.class, "holder");
1700 

1701     private void readObject (ObjectInputStream s) throws
1702                          IOException, ClassNotFoundException {
1703         if (getClass().getClassLoader() != null) {
1704             throw new SecurityException ("invalid address type");
1705         }
1706         GetField gf = s.readFields();
1707         String host = (String)gf.get("hostName", null);
1708         int address = gf.get("address", 0);
1709         int family = gf.get("family", 0);
1710         if (family != IPv4 && family != IPv6) {
1711             throw new InvalidObjectException("invalid address family type: " + family);
1712         }
1713         InetAddressHolder h = new InetAddressHolder(host, address, family);
1714         UNSAFE.putReference(this, FIELDS_OFFSET, h);
1715     }
1716 
1717     /* needed because the serializable fields no longer exist */
1718 
1719     /**
1720      * @serialField hostName String
1721      * @serialField address int
1722      * @serialField family int
1723      */

1724     private static final ObjectStreamField[] serialPersistentFields = {
1725         new ObjectStreamField("hostName", String.class),
1726         new ObjectStreamField("address", int.class),
1727         new ObjectStreamField("family", int.class),
1728     };
1729 

1730     private void writeObject (ObjectOutputStream s) throws
1731                         IOException {
1732         if (getClass().getClassLoader() != null) {
1733             throw new SecurityException ("invalid address type");
1734         }
1735         PutField pf = s.putFields();
1736         pf.put("hostName", holder().getHostName());
1737         pf.put("address", holder().getAddress());
1738         pf.put("family", holder().getFamily());
1739         s.writeFields();
1740     }
1741 }
1742 
1743 /*
1744  * Simple factory to create the impl
1745  */
1746 class InetAddressImplFactory {
1747 
1748     static InetAddressImpl create() {
1749         return InetAddress.loadImpl(isIPv6Supported() ?


 282         }
 283     }
 284 
 285     /* Used to store the serializable fields of InetAddress */
 286     final transient InetAddressHolder holder;
 287 
 288     InetAddressHolder holder() {
 289         return holder;
 290     }
 291 
 292     /* Used to store the name service provider */
 293     private static transient NameService nameService;
 294 
 295     /**
 296      * Used to store the best available hostname.
 297      * Lazily initialized via a data race; safe because Strings are immutable.
 298      */
 299     private transient String canonicalHostName = null;
 300 
 301     /** use serialVersionUID from JDK 1.0.2 for interoperability */
 302     @java.io.Serial
 303     private static final long serialVersionUID = 3286316764910316507L;
 304 
 305     /*
 306      * Load net library into runtime, and perform initializations.
 307      */
 308     static {
 309         String str = GetPropertyAction.privilegedGetProperty("java.net.preferIPv6Addresses");
 310         if (str == null) {
 311             preferIPv6Address = PREFER_IPV4_VALUE;
 312         } else if (str.equalsIgnoreCase("true")) {
 313             preferIPv6Address = PREFER_IPV6_VALUE;
 314         } else if (str.equalsIgnoreCase("false")) {
 315             preferIPv6Address = PREFER_IPV4_VALUE;
 316         } else if (str.equalsIgnoreCase("system")) {
 317             preferIPv6Address = PREFER_SYSTEM_VALUE;
 318         } else {
 319             preferIPv6Address = PREFER_IPV4_VALUE;
 320         }
 321         jdk.internal.loader.BootLoader.loadLibrary("net");
 322         SharedSecrets.setJavaNetInetAddressAccess(


 337     }
 338 
 339     /**
 340      * Constructor for the Socket.accept() method.
 341      * This creates an empty InetAddress, which is filled in by
 342      * the accept() method.  This InetAddress, however, is not
 343      * put in the address cache, since it is not created by name.
 344      */
 345     InetAddress() {
 346         holder = new InetAddressHolder();
 347     }
 348 
 349     /**
 350      * Replaces the de-serialized object with an Inet4Address object.
 351      *
 352      * @return the alternate object to the de-serialized object.
 353      *
 354      * @throws ObjectStreamException if a new object replacing this
 355      * object could not be created
 356      */
 357     @java.io.Serial
 358     private Object readResolve() throws ObjectStreamException {
 359         // will replace the deserialized 'this' object
 360         return new Inet4Address(holder().getHostName(), holder().getAddress());
 361     }
 362 
 363     /**
 364      * Utility routine to check if the InetAddress is an
 365      * IP multicast address.
 366      * @return a {@code boolean} indicating if the InetAddress is
 367      * an IP multicast address
 368      * @since   1.1
 369      */
 370     public boolean isMulticastAddress() {
 371         return false;
 372     }
 373 
 374     /**
 375      * Utility routine to check if the InetAddress is a wildcard address.
 376      * @return a {@code boolean} indicating if the InetAddress is
 377      *         a wildcard address.


1672                                "in your properties file.");
1673         } catch (IllegalAccessException e) {
1674             System.err.println("Cannot access class: java.net." + prefix +
1675                                implName + ":\ncheck impl.prefix property " +
1676                                "in your properties file.");
1677         }
1678 
1679         if (impl == null) {
1680             try {
1681                 @SuppressWarnings("deprecation")
1682                 Object tmp = Class.forName(implName).newInstance();
1683                 impl = tmp;
1684             } catch (Exception e) {
1685                 throw new Error("System property impl.prefix incorrect");
1686             }
1687         }
1688 
1689         return (InetAddressImpl) impl;
1690     }
1691 
1692     @java.io.Serial
1693     private void readObjectNoData () {
1694         if (getClass().getClassLoader() != null) {
1695             throw new SecurityException ("invalid address type");
1696         }
1697     }
1698 
1699     private static final jdk.internal.misc.Unsafe UNSAFE
1700             = jdk.internal.misc.Unsafe.getUnsafe();
1701     private static final long FIELDS_OFFSET
1702             = UNSAFE.objectFieldOffset(InetAddress.class, "holder");
1703 
1704     @java.io.Serial
1705     private void readObject (ObjectInputStream s) throws
1706                          IOException, ClassNotFoundException {
1707         if (getClass().getClassLoader() != null) {
1708             throw new SecurityException ("invalid address type");
1709         }
1710         GetField gf = s.readFields();
1711         String host = (String)gf.get("hostName", null);
1712         int address = gf.get("address", 0);
1713         int family = gf.get("family", 0);
1714         if (family != IPv4 && family != IPv6) {
1715             throw new InvalidObjectException("invalid address family type: " + family);
1716         }
1717         InetAddressHolder h = new InetAddressHolder(host, address, family);
1718         UNSAFE.putReference(this, FIELDS_OFFSET, h);
1719     }
1720 
1721     /* needed because the serializable fields no longer exist */
1722 
1723     /**
1724      * @serialField hostName String
1725      * @serialField address int
1726      * @serialField family int
1727      */
1728     @java.io.Serial
1729     private static final ObjectStreamField[] serialPersistentFields = {
1730         new ObjectStreamField("hostName", String.class),
1731         new ObjectStreamField("address", int.class),
1732         new ObjectStreamField("family", int.class),
1733     };
1734 
1735     @java.io.Serial
1736     private void writeObject (ObjectOutputStream s) throws
1737                         IOException {
1738         if (getClass().getClassLoader() != null) {
1739             throw new SecurityException ("invalid address type");
1740         }
1741         PutField pf = s.putFields();
1742         pf.put("hostName", holder().getHostName());
1743         pf.put("address", holder().getAddress());
1744         pf.put("family", holder().getFamily());
1745         s.writeFields();
1746     }
1747 }
1748 
1749 /*
1750  * Simple factory to create the impl
1751  */
1752 class InetAddressImplFactory {
1753 
1754     static InetAddressImpl create() {
1755         return InetAddress.loadImpl(isIPv6Supported() ?
< prev index next >