Skip to content

Commit ecdb83c

Browse files
committed
Minor review updates
- don't override hashcode() and equals() (the overrides break the contracts) - catch Throwable (else ExceptionUtils.handleThrowable() is a NO-OP) - comment typo - ensure the trackedStream is returned for multiple getInputStream() calls - NO-OP known safe case of FileURLConnection
1 parent dbd94fb commit ecdb83c

1 file changed

Lines changed: 18 additions & 24 deletions

File tree

java/org/apache/tomcat/util/buf/CloseableURLConnection.java

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -135,31 +135,36 @@ public void close() {
135135
if (trackedStream != null) {
136136
try {
137137
trackedStream.close();
138-
} catch (Exception e) {
139-
ExceptionUtils.handleThrowable(e);
138+
} catch (Throwable t) {
139+
ExceptionUtils.handleThrowable(t);
140140
}
141141
} else if (connection instanceof JarURLConnection) {
142142
try (@SuppressWarnings("unused")
143143
java.util.jar.JarFile jarFile = ((JarURLConnection) connection).getJarFile()) {
144144
// Explicitly close the JarFile to release its native resources.
145145
// As setUseCaches(false) is set, this should not cause side effects on other streams.
146-
} catch (Exception e) {
147-
ExceptionUtils.handleThrowable(e);
146+
} catch (Throwable t) {
147+
ExceptionUtils.handleThrowable(t);
148148
}
149+
} else if (connection.getClass().getName().equals("sun.net.www.protocol.file.FileURLConnection")) {
150+
/*
151+
* Internal JDK class so have to check by default name. If a JDK uses another name it will be handled by the
152+
* final block.
153+
*
154+
* NO-OP - known not to open a stream to read metadata
155+
*/
149156
} else if (!(connection instanceof HttpURLConnection)) {
150-
// Other cases like FileURLConnection could have used a stream as a side effect,
151-
// possibly causing file locking.
157+
// Other cases could have used a stream as a side effect, possibly causing file locking.
152158
try (@SuppressWarnings("unused") InputStream is = connection.getInputStream()) {
153159
// Explicitly close the InputStream to release its native resources.
154-
} catch (Exception e) {
155-
ExceptionUtils.handleThrowable(e);
160+
} catch (Throwable t) {
161+
ExceptionUtils.handleThrowable(t);
156162
}
157163
}
158164

159165
if (connection instanceof HttpURLConnection) {
160166
((HttpURLConnection) connection).disconnect();
161167
}
162-
163168
}
164169

165170

@@ -172,7 +177,9 @@ public void close() {
172177
*/
173178
@Override
174179
public InputStream getInputStream() throws IOException {
175-
trackedStream = connection.getInputStream();
180+
if (trackedStream == null) {
181+
trackedStream = connection.getInputStream();
182+
}
176183
return trackedStream;
177184
}
178185

@@ -284,7 +291,7 @@ public Object getContent(Class<?>[] classes) throws IOException {
284291
public Permission getPermission() throws IOException {
285292
/*
286293
* This method is deprecated for removal in Java 25. If it isn't overridden the superclass will return {@code
287-
* java.security.AllPermission} which would be acceptable but, for consistency, it is better to oveerride the
294+
* java.security.AllPermission} which would be acceptable but, for consistency, it is better to override the
288295
* method. Calling {@code getPermission()} on the wrapped connection would work until the method is removed.
289296
* Throwing {@code UnsupportedOperationException} works now since Tomcat never calls the method and will
290297
* continue to work once the method is removed.
@@ -393,17 +400,4 @@ public String getRequestProperty(String key) {
393400
public Map<String, List<String>> getRequestProperties() {
394401
return connection.getRequestProperties();
395402
}
396-
397-
398-
@Override
399-
public int hashCode() {
400-
return connection.hashCode();
401-
}
402-
403-
404-
@Override
405-
public boolean equals(Object obj) {
406-
return connection.equals(obj);
407-
}
408-
409403
}

0 commit comments

Comments
 (0)