< prev index next >

src/java.base/share/classes/sun/net/util/IPAddressUtil.java

Print this page

264     public static boolean isIPv6LiteralAddress(String src) {
265         return textToNumericFormatV6(src) != null;
266     }
267 
268     /*
269      * Convert IPv4-Mapped address to IPv4 address. Both input and
270      * returned value are in network order binary form.
271      *
272      * @param src a String representing an IPv4-Mapped address in textual format
273      * @return a byte array representing the IPv4 numeric address
274      */
275     public static byte[] convertFromIPv4MappedAddress(byte[] addr) {
276         if (isIPv4MappedAddress(addr)) {
277             byte[] newAddr = new byte[INADDR4SZ];
278             System.arraycopy(addr, 12, newAddr, 0, INADDR4SZ);
279             return newAddr;
280         }
281         return null;
282     }
283 












284     /**
285      * Utility routine to check if the InetAddress is an
286      * IPv4 mapped IPv6 address.
287      *
288      * @return a <code>boolean</code> indicating if the InetAddress is
289      * an IPv4 mapped IPv6 address; or false if address is IPv4 address.
290      */
291     private static boolean isIPv4MappedAddress(byte[] addr) {
292         if (addr.length < INADDR16SZ) {
293             return false;
294         }
295         if ((addr[0] == 0x00) && (addr[1] == 0x00) &&
296             (addr[2] == 0x00) && (addr[3] == 0x00) &&
297             (addr[4] == 0x00) && (addr[5] == 0x00) &&
298             (addr[6] == 0x00) && (addr[7] == 0x00) &&
299             (addr[8] == 0x00) && (addr[9] == 0x00) &&
300             (addr[10] == (byte)0xff) &&
301             (addr[11] == (byte)0xff))  {
302             return true;
303         }
304         return false;
305     }
306     /**
307      * Mapping from unscoped local Inet(6)Address to the same address
308      * including the correct scope-id, determined from NetworkInterface.
309      */
310     private final static ConcurrentHashMap<InetAddress,InetAddress>
311         cache = new ConcurrentHashMap<>();

264     public static boolean isIPv6LiteralAddress(String src) {
265         return textToNumericFormatV6(src) != null;
266     }
267 
268     /*
269      * Convert IPv4-Mapped address to IPv4 address. Both input and
270      * returned value are in network order binary form.
271      *
272      * @param src a String representing an IPv4-Mapped address in textual format
273      * @return a byte array representing the IPv4 numeric address
274      */
275     public static byte[] convertFromIPv4MappedAddress(byte[] addr) {
276         if (isIPv4MappedAddress(addr)) {
277             byte[] newAddr = new byte[INADDR4SZ];
278             System.arraycopy(addr, 12, newAddr, 0, INADDR4SZ);
279             return newAddr;
280         }
281         return null;
282     }
283 
284 
285     /**
286      * Returns the address value, as an int, of an IPv4-mapped IPv6 address.
287      */
288     public static int intFromIPv4MappedAddress(byte[] addr) {
289         assert isIPv4MappedAddress(addr);
290         return ((addr[12] & 0xFF) << 24) |
291                ((addr[13] & 0xFF) << 16) |
292                ((addr[14] & 0xFF) << 8)  |
293                 (addr[15] & 0xFF);
294     }
295 
296     /**
297      * Utility routine to check if the InetAddress is an
298      * IPv4 mapped IPv6 address.
299      *
300      * @return a <code>boolean</code> indicating if the InetAddress is
301      * an IPv4 mapped IPv6 address; or false if address is IPv4 address.
302      */
303     public static boolean isIPv4MappedAddress(byte[] addr) {
304         if (addr.length < INADDR16SZ) {
305             return false;
306         }
307         if ((addr[0] == 0x00) && (addr[1] == 0x00) &&
308             (addr[2] == 0x00) && (addr[3] == 0x00) &&
309             (addr[4] == 0x00) && (addr[5] == 0x00) &&
310             (addr[6] == 0x00) && (addr[7] == 0x00) &&
311             (addr[8] == 0x00) && (addr[9] == 0x00) &&
312             (addr[10] == (byte)0xff) &&
313             (addr[11] == (byte)0xff))  {
314             return true;
315         }
316         return false;
317     }
318     /**
319      * Mapping from unscoped local Inet(6)Address to the same address
320      * including the correct scope-id, determined from NetworkInterface.
321      */
322     private final static ConcurrentHashMap<InetAddress,InetAddress>
323         cache = new ConcurrentHashMap<>();
< prev index next >