Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.

Commit 0df3e4d

Browse files
schlosnapavelbucek
authored andcommitted
3506: Avoid exception control flow in isUriInetAddress
Port of upstream Guava commit e6003692500352e1ba59eee5c93bc21acb3f46c0 which avoids using exceptions as control flow in Jersey's internal version of InetAddresses.isUriInetAddress(). This avoids the expense of exception creation that can impede request throughput, especially under high load, and addresses #3506 and previously JERSEY-3234.
1 parent 4a8f2b2 commit 0df3e4d

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

core-common/src/main/java/org/glassfish/jersey/internal/guava/InetAddresses.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,11 @@ private static InetAddress bytesToInetAddress(byte[] addr) {
281281
* however, it requires that IPv6 addresses are surrounded by square brackets.
282282
*
283283
* @param hostAddr A RFC 3986 section 3.2.2 encoded IPv4 or IPv6 address
284-
* @return an InetAddress representing the address in {@code hostAddr}
285-
* @throws IllegalArgumentException if {@code hostAddr} is not a valid
286-
* IPv4 address, or IPv6 address surrounded by square brackets
284+
* @return an InetAddress representing the address in {@code hostAddr};
285+
* otherwise null if {@code hostAddr} is not a valid IPv4 address,
286+
* or IPv6 address surrounded * by square brackets
287287
*/
288-
private static InetAddress forUriString(String hostAddr) {
288+
private static InetAddress forUriStringNoThrow(String hostAddr) {
289289
Preconditions.checkNotNull(hostAddr);
290290

291291
// Decide if this should be an IPv6 or IPv4 address.
@@ -302,8 +302,7 @@ private static InetAddress forUriString(String hostAddr) {
302302
// Parse the address, and make sure the length/version is correct.
303303
byte[] addr = ipStringToBytes(ipString);
304304
if (addr == null || addr.length != expectBytes) {
305-
throw new IllegalArgumentException(
306-
String.format("Not a valid URI IP literal: '%s'", hostAddr));
305+
return null;
307306
}
308307

309308
return bytesToInetAddress(addr);
@@ -317,12 +316,7 @@ private static InetAddress forUriString(String hostAddr) {
317316
* @return {@code true} if the argument is a valid IP URI host
318317
*/
319318
public static boolean isUriInetAddress(String ipString) {
320-
try {
321-
forUriString(ipString);
322-
return true;
323-
} catch (IllegalArgumentException e) {
324-
return false;
325-
}
319+
return forUriStringNoThrow(ipString) != null;
326320
}
327321

328322
/**

0 commit comments

Comments
 (0)