Skip to content

Commit 3217919

Browse files
authored
Allow nullable on Util.close (#5224)
Enforce null check in java with any mutable nullable closable
1 parent b16b63f commit 3217919

4 files changed

Lines changed: 24 additions & 10 deletions

File tree

mockwebserver/src/test/java/okhttp3/mockwebserver/internal/http2/Http2Server.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,14 @@ private void run() throws Exception {
7676
connection.start();
7777
} catch (IOException e) {
7878
logger.log(Level.INFO, "Http2Server connection failure: " + e);
79-
closeQuietly(socket);
79+
if (socket != null) {
80+
closeQuietly(socket);
81+
}
8082
} catch (Exception e) {
8183
logger.log(Level.WARNING, "Http2Server unexpected failure", e);
82-
closeQuietly(socket);
84+
if (socket != null) {
85+
closeQuietly(socket);
86+
}
8387
}
8488
}
8589
}

okhttp-tls/src/test/java/okhttp3/tls/HandshakeCertificatesTest.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,12 @@ private Future<Handshake> doServerHandshake(HandshakeCertificates server) {
164164
sslSocket.startHandshake();
165165
return Handshake.get(sslSocket.getSession());
166166
} finally {
167-
closeQuietly(rawSocket);
168-
closeQuietly(sslSocket);
167+
if (rawSocket != null) {
168+
closeQuietly(rawSocket);
169+
}
170+
if (sslSocket != null) {
171+
closeQuietly(sslSocket);
172+
}
169173
}
170174
});
171175
}
@@ -183,7 +187,9 @@ private Future<Handshake> doClientHandshake(
183187
return Handshake.get(sslSocket.getSession());
184188
} finally {
185189
closeQuietly(rawSocket);
186-
closeQuietly(sslSocket);
190+
if (sslSocket != null) {
191+
closeQuietly(sslSocket);
192+
}
187193
}
188194
});
189195
}

okhttp/src/main/java/okhttp3/internal/Util.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ fun <K, V> Map<K, V>.toImmutableMap(): Map<K, V> {
474474
}
475475
}
476476

477-
/** Closes this, ignoring any checked exceptions. Does nothing if this is null. */
477+
/** Closes this, ignoring any checked exceptions. */
478478
fun Closeable.closeQuietly() {
479479
try {
480480
close()
@@ -484,7 +484,7 @@ fun Closeable.closeQuietly() {
484484
}
485485
}
486486

487-
/** Closes this, ignoring any checked exceptions. Does nothing if this is null. */
487+
/** Closes this, ignoring any checked exceptions. */
488488
fun Socket.closeQuietly() {
489489
try {
490490
close()
@@ -496,7 +496,7 @@ fun Socket.closeQuietly() {
496496
}
497497
}
498498

499-
/** Closes this, ignoring any checked exceptions. Does nothing if this is null. */
499+
/** Closes this, ignoring any checked exceptions. */
500500
fun ServerSocket.closeQuietly() {
501501
try {
502502
close()

okhttp/src/test/java/okhttp3/internal/http2/MockHttp2Peer.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,12 @@ public Socket openSocket() throws IOException {
183183

184184
@Override public synchronized void close() throws IOException {
185185
executor.shutdown();
186-
closeQuietly(socket);
187-
closeQuietly(serverSocket);
186+
if (socket != null) {
187+
closeQuietly(socket);
188+
}
189+
if (serverSocket != null) {
190+
closeQuietly(serverSocket);
191+
}
188192
}
189193

190194
@Override public String toString() {

0 commit comments

Comments
 (0)