Skip to content

Commit a2fb9ae

Browse files
Ben TaylorPaul Hohensee
authored andcommitted
8297211: Expensive fillInStackTrace operation in HttpURLConnection.getOutputStream0 when no content-length in response
Backport-of: 392ac7055d4697c56fa85ac5572f5bc4dc431f1d
1 parent 26b03b5 commit a2fb9ae

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
@@ -611,10 +611,12 @@ public Map<String,List<String>> getHeaderFields() {
611611
* missing or malformed.
612612
*/
613613
public int getHeaderFieldInt(String name, int Default) {
614-
String value = getHeaderField(name);
615-
try {
616-
return Integer.parseInt(value);
617-
} catch (Exception e) { }
614+
final String value = getHeaderField(name);
615+
if (value != null) {
616+
try {
617+
return Integer.parseInt(value);
618+
} catch (NumberFormatException e) { }
619+
}
618620
return Default;
619621
}
620622

@@ -634,10 +636,12 @@ public int getHeaderFieldInt(String name, int Default) {
634636
* @since 1.7
635637
*/
636638
public long getHeaderFieldLong(String name, long Default) {
637-
String value = getHeaderField(name);
638-
try {
639-
return Long.parseLong(value);
640-
} catch (Exception e) { }
639+
final String value = getHeaderField(name);
640+
if (value != null) {
641+
try {
642+
return Long.parseLong(value);
643+
} catch (NumberFormatException e) { }
644+
}
641645
return Default;
642646
}
643647

@@ -659,10 +663,12 @@ public long getHeaderFieldLong(String name, long Default) {
659663
*/
660664
@SuppressWarnings("deprecation")
661665
public long getHeaderFieldDate(String name, long Default) {
662-
String value = getHeaderField(name);
663-
try {
664-
return Date.parse(value);
665-
} catch (Exception e) { }
666+
final String value = getHeaderField(name);
667+
if (value != null) {
668+
try {
669+
return Date.parse(value);
670+
} catch (Exception e) { }
671+
}
666672
return Default;
667673
}
668674

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
@@ -1938,9 +1938,12 @@ private InputStream getInputStream0() throws IOException {
19381938
continue;
19391939
}
19401940

1941-
try {
1942-
cl = Long.parseLong(responses.findValue("content-length"));
1943-
} catch (Exception exc) { };
1941+
final String contentLengthVal = responses.findValue("content-length");
1942+
if (contentLengthVal != null) {
1943+
try {
1944+
cl = Long.parseLong(contentLengthVal);
1945+
} catch (NumberFormatException nfe) { }
1946+
}
19441947

19451948
if (method.equals("HEAD") || cl == 0 ||
19461949
respCode == HTTP_NOT_MODIFIED ||

0 commit comments

Comments
 (0)