MCPcopy Create free account
hub / github.com/antlr/codebuff / InetAddresses

Class InetAddresses

corpus/java/training/guava/net/InetAddresses.java:101–995  ·  view source on GitHub ↗

Static utility methods pertaining to InetAddress instances. Important note: Unlike 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

99 * @since 5.0
100 */
101@Beta
102@GwtIncompatible
103public final class InetAddresses {
104 private static final int IPV4_PART_COUNT = 4;
105 private static final int IPV6_PART_COUNT = 8;
106 private static final Splitter IPV4_SPLITTER = Splitter.on('.').limit(IPV4_PART_COUNT);
107 private static final Inet4Address LOOPBACK4 = (Inet4Address) forString("127.0.0.1");
108 private static final Inet4Address ANY4 = (Inet4Address) forString("0.0.0.0");
109
110 private InetAddresses() {}
111
112 /**
113 * Returns an {@link Inet4Address}, given a byte array representation of the IPv4 address.
114 *
115 * @param bytes byte array representing an IPv4 address (should be of length 4)
116 * @return {@link Inet4Address} corresponding to the supplied byte array
117 * @throws IllegalArgumentException if a valid {@link Inet4Address} can not be created
118 */
119 private static Inet4Address getInet4Address(byte[] bytes) {
120 Preconditions.checkArgument(
121 bytes.length == 4,
122 "Byte array has invalid length for an IPv4 address: %s != 4.",
123 bytes.length);
124
125 // Given a 4-byte array, this cast should always succeed.
126 return (Inet4Address) bytesToInetAddress(bytes);
127 }
128
129 /**
130 * Returns the {@link InetAddress} having the given string representation.
131 *
132 * <p>This deliberately avoids all nameservice lookups (e.g. no DNS).
133 *
134 * @param ipString {@code String} containing an IPv4 or IPv6 string literal, e.g.
135 * {@code "192.168.0.1"} or {@code "2001:db8::1"}
136 * @return {@link InetAddress} representing the argument
137 * @throws IllegalArgumentException if the argument is not a valid IP string literal
138 */
139 public static InetAddress forString(String ipString) {
140 byte[] addr = ipStringToBytes(ipString);
141
142 // The argument was malformed, i.e. not an IP string literal.
143 if (addr == null) {
144 throw formatIllegalArgumentException("'%s' is not an IP string literal.", ipString);
145 }
146
147 return bytesToInetAddress(addr);
148 }
149
150 /**
151 * Returns {@code true} if the supplied string is a valid IP string literal, {@code false}
152 * otherwise.
153 *
154 * @param ipString {@code String} to evaluated as an IP string literal
155 * @return {@code true} if the argument is a valid IP string literal
156 */
157 public static boolean isInetAddress(String ipString) {
158 return ipStringToBytes(ipString) != null;

Callers

nothing calls this directly

Calls 3

onMethod · 0.95
forStringMethod · 0.95
limitMethod · 0.45

Tested by

no test coverage detected