Skip to content

Commit

Permalink
Remove nullability of receiver for closeQuietly extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeWharton committed May 21, 2019
1 parent f465599 commit 28d517a
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ class MockWebServer : ExternalResource(), Closeable {
}

// Release all sockets and all threads, even if any close fails.
serverSocket.closeQuietly()
serverSocket?.closeQuietly()

val openClientSocket = openClientSockets.iterator()
while (openClientSocket.hasNext()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ public final class HandshakeCertificatesTest {

@After public void tearDown() {
executorService.shutdown();
closeQuietly(serverSocket);
if (serverSocket != null) {
closeQuietly(serverSocket);
}
}

@Test public void clientAndServer() throws Exception {
Expand Down
2 changes: 1 addition & 1 deletion okhttp/src/main/java/okhttp3/Cache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ class Cache internal constructor(

val response = entry.response(snapshot)
if (!entry.matches(request, response)) {
response.body().closeQuietly()
response.body()?.closeQuietly()
return null
}

Expand Down
12 changes: 6 additions & 6 deletions okhttp/src/main/java/okhttp3/internal/UtilKt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -208,19 +208,19 @@ fun <K, V> Map<K, V>.toImmutableMap(): Map<K, V> {
}

/** Closes this, ignoring any checked exceptions. Does nothing if this is null. */
fun Closeable?.closeQuietly() {
fun Closeable.closeQuietly() {
try {
this?.close()
close()
} catch (rethrown: RuntimeException) {
throw rethrown
} catch (_: Exception) {
}
}

/** Closes this, ignoring any checked exceptions. Does nothing if this is null. */
fun Socket?.closeQuietly() {
fun Socket.closeQuietly() {
try {
this?.close()
close()
} catch (e: AssertionError) {
throw e
} catch (rethrown: RuntimeException) {
Expand All @@ -230,9 +230,9 @@ fun Socket?.closeQuietly() {
}

/** Closes this, ignoring any checked exceptions. Does nothing if this is null. */
fun ServerSocket?.closeQuietly() {
fun ServerSocket.closeQuietly() {
try {
this?.close()
close()
} catch (rethrown: RuntimeException) {
throw rethrown
} catch (_: Exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class CacheInterceptor(internal val cache: InternalCache?) : Interceptor {

if (cacheCandidate != null && cacheResponse == null) {
// The cache candidate wasn't applicable. Close it.
cacheCandidate.body().closeQuietly()
cacheCandidate.body()?.closeQuietly()
}

// If we're forbidden from using the network and the cache is insufficient, fail.
Expand Down Expand Up @@ -83,7 +83,7 @@ class CacheInterceptor(internal val cache: InternalCache?) : Interceptor {
} finally {
// If we're crashing on I/O or otherwise, don't leak the cache body.
if (networkResponse == null && cacheCandidate != null) {
cacheCandidate.body().closeQuietly()
cacheCandidate.body()?.closeQuietly()
}
}

Expand All @@ -106,7 +106,7 @@ class CacheInterceptor(internal val cache: InternalCache?) : Interceptor {
cache.update(cacheResponse, response)
return response
} else {
cacheResponse.body().closeQuietly()
cacheResponse.body()?.closeQuietly()
}
}

Expand Down
6 changes: 2 additions & 4 deletions okhttp/src/main/java/okhttp3/internal/cache2/Relay.kt
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class Relay private constructor(
complete = true
}

upstream.closeQuietly()
upstream?.closeQuietly()
upstream = null
}

Expand Down Expand Up @@ -284,9 +284,7 @@ class Relay private constructor(
}
}

if (fileToClose != null) {
fileToClose.closeQuietly()
}
fileToClose?.closeQuietly()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class ExchangeFinder(
}
}
}
toClose.closeQuietly()
toClose?.closeQuietly()

if (releasedConnection != null) {
eventListener.connectionReleased(call, releasedConnection!!)
Expand Down Expand Up @@ -259,7 +259,7 @@ class ExchangeFinder(
transmitter.acquireConnectionNoEvents(result!!)
}
}
socket.closeQuietly()
socket?.closeQuietly()

eventListener.connectionAcquired(call, result!!)
return result!!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ class RealConnection(
eventListener.connectEnd(call, route.socketAddress(), route.proxy(), protocol)
break
} catch (e: IOException) {
socket.closeQuietly()
rawSocket.closeQuietly()
socket?.closeQuietly()
rawSocket?.closeQuietly()
socket = null
rawSocket = null
source = null
Expand Down Expand Up @@ -237,7 +237,7 @@ class RealConnection(

// The proxy decided to close the connection after an auth challenge. We need to create a new
// connection, but this time with the auth credentials.
rawSocket.closeQuietly()
rawSocket?.closeQuietly()
rawSocket = null
sink = null
source = null
Expand Down Expand Up @@ -391,7 +391,7 @@ class RealConnection(
Platform.get().afterHandshake(sslSocket)
}
if (!success) {
sslSocket.closeQuietly()
sslSocket?.closeQuietly()
}
}
}
Expand Down Expand Up @@ -592,7 +592,7 @@ class RealConnection(

fun cancel() {
// Close the raw socket so we don't end up doing synchronous I/O.
rawSocket.closeQuietly()
rawSocket?.closeQuietly()
}

override fun socket(): Socket = socket!!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ class Transmitter(
if (this.connection != null) releasedConnection = null
callEnd = noMoreExchanges && exchange == null
}
socket.closeQuietly()
socket?.closeQuietly()

if (releasedConnection != null) {
eventListener.connectionReleased(call, releasedConnection!!)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class RetryAndFollowUpInterceptor(private val client: OkHttpClient) : Intercepto
return response
}

response.body().closeQuietly()
response.body()?.closeQuietly()
if (transmitter.hasExchange()) {
exchange?.detachWithViolence()
}
Expand Down
6 changes: 3 additions & 3 deletions okhttp/src/main/java/okhttp3/internal/ws/RealWebSocket.kt
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ class RealWebSocket(
listener.onClosed(this, code, reason)
}
} finally {
toClose.closeQuietly()
toClose?.closeQuietly()
}
}

Expand Down Expand Up @@ -483,7 +483,7 @@ class RealWebSocket(

return true
} finally {
streamsToClose.closeQuietly()
streamsToClose?.closeQuietly()
}
}

Expand Down Expand Up @@ -531,7 +531,7 @@ class RealWebSocket(
try {
listener.onFailure(this, e, response)
} finally {
streamsToClose.closeQuietly()
streamsToClose?.closeQuietly()
}
}

Expand Down

0 comments on commit 28d517a

Please sign in to comment.