Home | History | Annotate | Download | only in net

Lines Matching refs:port

34  * An immutable representation of a host and port.
58 * concerned with brackets, colons, and port numbers. Full validation of the
68 /** Magic value indicating the absence of a port number. */
74 /** Validated port number in the range [0..65535], or NO_PORT */
75 private final int port;
80 private HostAndPort(String host, int port, boolean hasBracketlessColons) {
82 this.port = port;
97 /** Return true if this instance has a defined port. */
99 return port >= 0;
103 * Get the current port number, failing if no port is defined.
105 * @return a validated port number, in the range [0..65535]
106 * @throws IllegalStateException if no port is defined. You can use
111 return port;
115 * Returns the current port number, with a default if no port is defined.
118 return hasPort() ? port : defaultPort;
122 * Build a HostAndPort instance from separate host and port values.
127 * @param host the host string to parse. Must not contain a port number.
128 * @param port a port number from [0..65535]
130 * @throws IllegalArgumentException if {@code host} contains a port number,
131 * or {@code port} is out of range.
133 public static HostAndPort fromParts(String host, int port) {
134 checkArgument(isValidPort(port), "Port out of range: %s", port);
136 checkArgument(!parsedHost.hasPort(), "Host has a port: %s", host);
137 return new HostAndPort(parsedHost.host, port, parsedHost.hasBracketlessColons);
146 * @param host the host-only string to parse. Must not contain a port number.
148 * @throws IllegalArgumentException if {@code host} contains a port number.
153 checkArgument(!parsedHost.hasPort(), "Host has a port: %s", host);
158 * Split a freeform string into a host and port, without strict validation.
160 * Note that the host-only formats will leave the port field undefined. You
180 // Exactly 1 colon. Split into host:port.
190 int port = NO_PORT;
192 // Try to parse the whole port string as a number.
194 checkArgument(!portString.startsWith("+"), "Unparseable port number: %s", hostPortString);
196 port = Integer.parseInt(portString);
198 throw new IllegalArgumentException("Unparseable port number: " + hostPortString);
200 checkArgument(isValidPort(port), "Port number out of range: %s", hostPortString);
203 return new HostAndPort(host, port, hasBracketlessColons);
207 * Parses a bracketed host-port string, throwing IllegalArgumentException if parsing fails.
209 * @param hostPortString the full bracketed host-port specification. Post might not be specified.
210 * @return an array with 2 strings: host and port, in that order.
211 * @throws IllegalArgumentException if parsing the bracketed host-port string fails.
217 "Bracketed host-port string must start with a bracket: %s", hostPortString);
221 "Invalid bracketed host/port: %s", hostPortString);
231 "Port must be numeric: %s", hostPortString);
238 * Provide a default port if the parsed string contained only a host.
240 * You can chain this after {@link #fromString(String)} to include a port in
241 * case the port was omitted from the input string. If a port was already
244 * @param defaultPort a port number, from [0..65535]
245 * @return a HostAndPort instance, guaranteed to have a defined port.
249 if (hasPort() || port == defaultPort) {
283 && this.port == that.port
291 return Objects.hashCode(host, port, hasBracketlessColons);
294 /** Rebuild the host:port string, including brackets if necessary. */
305 builder.append(':').append(port);
310 /** Return true for valid port numbers. */
311 private static boolean isValidPort(int port) {
312 return port >= 0 && port <= 65535;