MCPcopy Index your code
hub / github.com/antlr/codebuff / InetAddresses

Class InetAddresses

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

Callers

nothing calls this directly

Calls 3

onMethod · 0.95
forStringMethod · 0.95
limitMethod · 0.45

Tested by

no test coverage detected