Skip to content

Commit 392ac70

Browse files
committed
8297211: Expensive fillInStackTrace operation in HttpURLConnection.getOutputStream0 when no content-length in response
Reviewed-by: simonis, dfuchs
1 parent 5a45c25 commit 392ac70

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

src/java.base/share/classes/java/net/URLConnection.java

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1995, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1995, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -619,10 +619,12 @@ public Map<String,List<String>> getHeaderFields() {
619619
* missing or malformed.
620620
*/
621621
public int getHeaderFieldInt(String name, int Default) {
622-
String value = getHeaderField(name);
623-
try {
624-
return Integer.parseInt(value);
625-
} catch (Exception e) { }
622+
final String value = getHeaderField(name);
623+
if (value != null) {
624+
try {
625+
return Integer.parseInt(value);
626+
} catch (NumberFormatException e) { }
627+
}
626628
return Default;
627629
}
628630

@@ -642,10 +644,12 @@ public int getHeaderFieldInt(String name, int Default) {
642644
* @since 1.7
643645
*/
644646
public long getHeaderFieldLong(String name, long Default) {
645-
String value = getHeaderField(name);
646-
try {
647-
return Long.parseLong(value);
648-
} catch (Exception e) { }
647+
final String value = getHeaderField(name);
648+
if (value != null) {
649+
try {
650+
return Long.parseLong(value);
651+
} catch (NumberFormatException e) { }
652+
}
649653
return Default;
650654
}
651655

@@ -667,10 +671,12 @@ public long getHeaderFieldLong(String name, long Default) {
667671
*/
668672
@SuppressWarnings("deprecation")
669673
public long getHeaderFieldDate(String name, long Default) {
670-
String value = getHeaderField(name);
671-
try {
672-
return Date.parse(value);
673-
} catch (Exception e) { }
674+
final String value = getHeaderField(name);
675+
if (value != null) {
676+
try {
677+
return Date.parse(value);
678+
} catch (Exception e) { }
679+
}
674680
return Default;
675681
}
676682

src/java.base/share/classes/sun/net/www/protocol/http/HttpURLConnection.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1932,9 +1932,12 @@ private InputStream getInputStream0() throws IOException {
19321932
continue;
19331933
}
19341934

1935-
try {
1936-
cl = Long.parseLong(responses.findValue("content-length"));
1937-
} catch (Exception exc) { };
1935+
final String contentLengthVal = responses.findValue("content-length");
1936+
if (contentLengthVal != null) {
1937+
try {
1938+
cl = Long.parseLong(contentLengthVal);
1939+
} catch (NumberFormatException nfe) { }
1940+
}
19381941

19391942
if (method.equals("HEAD") || cl == 0 ||
19401943
respCode == HTTP_NOT_MODIFIED ||

0 commit comments

Comments
 (0)