MCPcopy
hub / github.com/google/guava / InetAddresses

Class InetAddresses

guava/src/com/google/common/net/InetAddresses.java:104–1244  ·  view source on GitHub ↗

Static utility methods pertaining to {@link InetAddress} instances. <p><b>Important note:</b> Unlike {@code InetAddress.getByName()}, the methods of this class never cause DNS services to be accessed. For this reason, you should prefer these methods as much as possible over their JDK equivalents wh

Source from the content-addressed store, hash-verified

102 * @since 5.0
103 */
104@J2ktIncompatible
105@GwtIncompatible
106public final class InetAddresses {
107 private static final int IPV4_PART_COUNT = 4;
108 private static final int IPV6_PART_COUNT = 8;
109 private static final char IPV4_DELIMITER = '.';
110 private static final char IPV6_DELIMITER = ':';
111 private static final CharMatcher IPV4_DELIMITER_MATCHER = CharMatcher.is(IPV4_DELIMITER);
112 private static final CharMatcher IPV6_DELIMITER_MATCHER = CharMatcher.is(IPV6_DELIMITER);
113 private static final Inet4Address LOOPBACK4 = (Inet4Address) forString("127.0.0.1");
114 private static final Inet4Address ANY4 = (Inet4Address) forString("0.0.0.0");
115
116 private InetAddresses() {}
117
118 /**
119 * Returns an {@link Inet4Address}, given a byte array representation of the IPv4 address.
120 *
121 * @param bytes byte array representing an IPv4 address (should be of length 4)
122 * @return {@link Inet4Address} corresponding to the supplied byte array
123 * @throws IllegalArgumentException if a valid {@link Inet4Address} can not be created
124 */
125 private static Inet4Address getInet4Address(byte[] bytes) {
126 checkArgument(
127 bytes.length == 4,
128 "Byte array has invalid length for an IPv4 address: %s != 4.",
129 bytes.length);
130
131 // Given a 4-byte array, this cast should always succeed.
132 return (Inet4Address) bytesToInetAddress(bytes, null);
133 }
134
135 /**
136 * Returns the {@link InetAddress} having the given string representation.
137 *
138 * <p>This deliberately avoids all nameservice lookups (e.g. no DNS).
139 *
140 * <p>IPv4-mapped IPv6 addresses (e.g. {@code "::ffff:192.168.0.1"}) are returned as {@link
141 * Inet4Address} objects, not {@link Inet6Address}. This is consistent with the behavior of {@link
142 * InetAddress}.
143 *
144 * <p>This method accepts non-ASCII digits, for example {@code "192.168.0.1"} (those are fullwidth
145 * characters). That is consistent with {@link InetAddress}, but not with various RFCs. If you
146 * want to accept ASCII digits only, you can use something like {@code
147 * CharMatcher.ascii().matchesAllOf(ipString)}.
148 *
149 * <p>The scope ID is validated against the interfaces on the machine, which requires permissions
150 * under Android.
151 *
152 * <p><b>Android users on API >= 29:</b> Prefer {@code InetAddresses.parseNumericAddress}.
153 *
154 * @param ipString {@code String} containing an IPv4 or IPv6 string literal, e.g. {@code
155 * "192.168.0.1"} or {@code "2001:db8::1"} or with a scope ID, e.g. {@code "2001:db8::1%eth0"}
156 * @return {@link InetAddress} representing the argument
157 * @throws IllegalArgumentException if the argument is not a valid IP string literal or if the
158 * address has a scope ID that fails validation against the interfaces on the machine (as
159 * required by Java's {@link InetAddress})
160 */
161 @CanIgnoreReturnValue // TODO(b/219820829): consider removing

Callers

nothing calls this directly

Calls 2

isMethod · 0.95
forStringMethod · 0.95

Tested by

no test coverage detected