diff --git a/mockwebserver/src/main/java/okhttp3/mockwebserver/MockWebServer.kt b/mockwebserver/src/main/java/okhttp3/mockwebserver/MockWebServer.kt index e4220c232..f7e4cd509 100644 --- a/mockwebserver/src/main/java/okhttp3/mockwebserver/MockWebServer.kt +++ b/mockwebserver/src/main/java/okhttp3/mockwebserver/MockWebServer.kt @@ -513,9 +513,7 @@ class MockWebServer : ExternalResource(), Closeable { val sink = raw.sink().buffer() while (true) { val socketPolicy = dispatcher.peek().getSocketPolicy() - if (!processOneRequest(raw, source, sink)) { - throw IllegalStateException("Tunnel without any CONNECT!") - } + check(processOneRequest(raw, source, sink)) { "Tunnel without any CONNECT!" } if (socketPolicy === UPGRADE_TO_SSL_AT_END) return } } @@ -676,8 +674,8 @@ class MockWebServer : ExternalResource(), Closeable { } val method = request.substringBefore(' ') - if (hasBody && !HttpMethod.permitsRequestBody(method)) { - throw IllegalArgumentException("Request must not have a body: $request") + require(!hasBody || HttpMethod.permitsRequestBody(method)) { + "Request must not have a body: $request" } return RecordedRequest(request, headers.build(), chunkSizes, requestBody.receivedByteCount, diff --git a/okhttp-dnsoverhttps/src/main/java/okhttp3/dnsoverhttps/DnsRecordCodec.kt b/okhttp-dnsoverhttps/src/main/java/okhttp3/dnsoverhttps/DnsRecordCodec.kt index 3a5d0d55b..7e0b34c78 100644 --- a/okhttp-dnsoverhttps/src/main/java/okhttp3/dnsoverhttps/DnsRecordCodec.kt +++ b/okhttp-dnsoverhttps/src/main/java/okhttp3/dnsoverhttps/DnsRecordCodec.kt @@ -47,9 +47,7 @@ object DnsRecordCodec { val labels = host.split('.').dropLastWhile { it.isEmpty() } for (label in labels) { val utf8ByteCount = label.utf8Size() - if (utf8ByteCount != label.length.toLong()) { - throw IllegalArgumentException("non-ascii hostname: $host") - } + require(utf8ByteCount == label.length.toLong()) { "non-ascii hostname: $host" } nameBuf.writeByte(utf8ByteCount.toInt()) nameBuf.writeUtf8(label) } @@ -69,9 +67,7 @@ object DnsRecordCodec { buf.readShort() // query id val flags = buf.readShort().toInt() and 0xffff - if (flags shr 15 == 0) { - throw IllegalArgumentException("not a response") - } + require(flags shr 15 != 0) { "not a response" } val responseCode = flags and 0xf diff --git a/okhttp/src/main/java/okhttp3/Cache.kt b/okhttp/src/main/java/okhttp3/Cache.kt index 3a9a81b5a..5a4ba7682 100644 --- a/okhttp/src/main/java/okhttp3/Cache.kt +++ b/okhttp/src/main/java/okhttp3/Cache.kt @@ -352,7 +352,7 @@ class Cache internal constructor( } override fun remove() { - if (!canRemove) throw IllegalStateException("remove() before next()") + check(canRemove) { "remove() before next()" } delegate.remove() } } diff --git a/okhttp/src/main/java/okhttp3/CacheControl.kt b/okhttp/src/main/java/okhttp3/CacheControl.kt index 33ac9ec15..7a16baa33 100644 --- a/okhttp/src/main/java/okhttp3/CacheControl.kt +++ b/okhttp/src/main/java/okhttp3/CacheControl.kt @@ -193,7 +193,7 @@ class CacheControl private constructor( * precision; finer precision will be lost. */ fun maxAge(maxAge: Int, timeUnit: TimeUnit) = apply { - if (maxAge < 0) throw IllegalArgumentException("maxAge < 0: $maxAge") + require(maxAge >= 0) { "maxAge < 0: $maxAge" } val maxAgeSecondsLong = timeUnit.toSeconds(maxAge.toLong()) this.maxAgeSeconds = maxAgeSecondsLong.clampToInt() } @@ -206,7 +206,7 @@ class CacheControl private constructor( * [TimeUnit.SECONDS] precision; finer precision will be lost. */ fun maxStale(maxStale: Int, timeUnit: TimeUnit) = apply { - if (maxStale < 0) throw IllegalArgumentException("maxStale < 0: $maxStale") + require(maxStale >= 0) { "maxStale < 0: $maxStale" } val maxStaleSecondsLong = timeUnit.toSeconds(maxStale.toLong()) this.maxStaleSeconds = maxStaleSecondsLong.clampToInt() } @@ -220,7 +220,7 @@ class CacheControl private constructor( * [TimeUnit.SECONDS] precision; finer precision will be lost. */ fun minFresh(minFresh: Int, timeUnit: TimeUnit) = apply { - if (minFresh < 0) throw IllegalArgumentException("minFresh < 0: $minFresh") + require(minFresh >= 0) { "minFresh < 0: $minFresh" } val minFreshSecondsLong = timeUnit.toSeconds(minFresh.toLong()) this.minFreshSeconds = minFreshSecondsLong.clampToInt() } diff --git a/okhttp/src/main/java/okhttp3/HttpUrl.kt b/okhttp/src/main/java/okhttp3/HttpUrl.kt index 46c173362..b2647258a 100644 --- a/okhttp/src/main/java/okhttp3/HttpUrl.kt +++ b/okhttp/src/main/java/okhttp3/HttpUrl.kt @@ -877,7 +877,7 @@ class HttpUrl internal constructor( } fun port(port: Int): Builder { - if (port <= 0 || port > 65535) throw IllegalArgumentException("unexpected port: $port") + require(port in 1..65535) { "unexpected port: $port" } this.port = port return this } @@ -926,8 +926,8 @@ class HttpUrl internal constructor( fun setPathSegment(index: Int, pathSegment: String): Builder { val canonicalPathSegment = pathSegment.canonicalize(encodeSet = PATH_SEGMENT_ENCODE_SET) - if (isDot(canonicalPathSegment) || isDotDot(canonicalPathSegment)) { - throw IllegalArgumentException("unexpected path segment: $pathSegment") + require(!isDot(canonicalPathSegment) && !isDotDot(canonicalPathSegment)) { + "unexpected path segment: $pathSegment" } encodedPathSegments[index] = canonicalPathSegment return this @@ -939,8 +939,8 @@ class HttpUrl internal constructor( alreadyEncoded = true ) encodedPathSegments[index] = canonicalPathSegment - if (isDot(canonicalPathSegment) || isDotDot(canonicalPathSegment)) { - throw IllegalArgumentException("unexpected path segment: $encodedPathSegment") + require(!isDot(canonicalPathSegment) && !isDotDot(canonicalPathSegment)) { + "unexpected path segment: $encodedPathSegment" } return this } @@ -954,9 +954,7 @@ class HttpUrl internal constructor( } fun encodedPath(encodedPath: String): Builder { - if (!encodedPath.startsWith("/")) { - throw IllegalArgumentException("unexpected encodedPath: $encodedPath") - } + require(encodedPath.startsWith("/")) { "unexpected encodedPath: $encodedPath" } resolvePath(encodedPath, 0, encodedPath.length) return this } diff --git a/okhttp/src/main/java/okhttp3/OkHttpClient.kt b/okhttp/src/main/java/okhttp3/OkHttpClient.kt index c152a9d07..e23e8fe98 100644 --- a/okhttp/src/main/java/okhttp3/OkHttpClient.kt +++ b/okhttp/src/main/java/okhttp3/OkHttpClient.kt @@ -17,16 +17,16 @@ package okhttp3 import okhttp3.Protocol.HTTP_1_1 import okhttp3.Protocol.HTTP_2 -import okhttp3.internal.checkDuration import okhttp3.internal.cache.InternalCache +import okhttp3.internal.checkDuration import okhttp3.internal.eventListenerFactory -import okhttp3.internal.toImmutableList import okhttp3.internal.immutableListOf import okhttp3.internal.platform.Platform import okhttp3.internal.platformTrustManager import okhttp3.internal.proxy.NullProxySelector import okhttp3.internal.tls.CertificateChainCleaner import okhttp3.internal.tls.OkHostnameVerifier +import okhttp3.internal.toImmutableList import okhttp3.internal.ws.RealWebSocket import okio.Sink import okio.Source @@ -531,9 +531,7 @@ open class OkHttpClient internal constructor( * If unset, the [system-wide default][SocketFactory.getDefault] socket factory will be used. */ fun socketFactory(socketFactory: SocketFactory) = apply { - if (socketFactory is SSLSocketFactory) { - throw IllegalArgumentException("socketFactory instanceof SSLSocketFactory") - } + require(socketFactory !is SSLSocketFactory) { "socketFactory instanceof SSLSocketFactory" } this.socketFactory = socketFactory } diff --git a/okhttp/src/main/java/okhttp3/internal/http/RealInterceptorChain.kt b/okhttp/src/main/java/okhttp3/internal/http/RealInterceptorChain.kt index ce7b21a56..7b27d0f3e 100644 --- a/okhttp/src/main/java/okhttp3/internal/http/RealInterceptorChain.kt +++ b/okhttp/src/main/java/okhttp3/internal/http/RealInterceptorChain.kt @@ -77,10 +77,7 @@ class RealInterceptorChain( fun transmitter(): Transmitter = transmitter - fun exchange(): Exchange { - if (exchange == null) throw IllegalStateException() - return exchange - } + fun exchange(): Exchange = exchange!! override fun call(): Call = call @@ -97,15 +94,13 @@ class RealInterceptorChain( calls++ // If we already have a stream, confirm that the incoming request will use it. - if (this.exchange != null && !this.exchange.connection()!!.supportsUrl(request.url())) { - throw IllegalStateException("network interceptor " + interceptors[index - 1] + - " must retain the same host and port") + check(this.exchange == null || this.exchange.connection()!!.supportsUrl(request.url())) { + "network interceptor ${interceptors[index - 1]} must retain the same host and port" } // If we already have a stream, confirm that this is the only call to chain.proceed(). - if (this.exchange != null && calls > 1) { - throw IllegalStateException("network interceptor " + interceptors[index - 1] + - " must call proceed() exactly once") + check(this.exchange == null || calls <= 1) { + "network interceptor ${interceptors[index - 1]} must call proceed() exactly once" } // Call the next interceptor in the chain. @@ -118,15 +113,11 @@ class RealInterceptorChain( "interceptor $interceptor returned null") // Confirm that the next interceptor made its required call to chain.proceed(). - if (exchange != null && index + 1 < interceptors.size && next.calls != 1) { - throw IllegalStateException("network interceptor " + interceptor + - " must call proceed() exactly once") + check(exchange == null || index + 1 >= interceptors.size || next.calls == 1) { + "network interceptor $interceptor must call proceed() exactly once" } - if (response.body() == null) { - throw IllegalStateException( - "interceptor $interceptor returned a response with no body") - } + check(response.body() != null) { "interceptor $interceptor returned a response with no body" } return response } diff --git a/okhttp/src/main/java/okhttp3/internal/http1/Http1ExchangeCodec.kt b/okhttp/src/main/java/okhttp3/internal/http1/Http1ExchangeCodec.kt index 12fc5139e..22b4ff279 100644 --- a/okhttp/src/main/java/okhttp3/internal/http1/Http1ExchangeCodec.kt +++ b/okhttp/src/main/java/okhttp3/internal/http1/Http1ExchangeCodec.kt @@ -383,8 +383,8 @@ class Http1ExchangeCodec( } override fun read(sink: Buffer, byteCount: Long): Long { - if (byteCount < 0L) throw IllegalArgumentException("byteCount < 0: $byteCount") - if (closed) throw IllegalStateException("closed") + require(byteCount >= 0L) { "byteCount < 0: $byteCount" } + check(!closed) { "closed" } if (bytesRemaining == 0L) return -1 val read = super.read(sink, minOf(bytesRemaining, byteCount)) diff --git a/okhttp/src/main/java/okhttp3/internal/platform/AndroidPlatform.kt b/okhttp/src/main/java/okhttp3/internal/platform/AndroidPlatform.kt index f167da7c3..38184aef8 100644 --- a/okhttp/src/main/java/okhttp3/internal/platform/AndroidPlatform.kt +++ b/okhttp/src/main/java/okhttp3/internal/platform/AndroidPlatform.kt @@ -393,8 +393,7 @@ class AndroidPlatform( } catch (ignored: NoSuchMethodException) { } } - throw IllegalStateException( - "Expected Android API level 21+ but was " + Build.VERSION.SDK_INT) + throw IllegalStateException("Expected Android API level 21+ but was ${Build.VERSION.SDK_INT}") } } } diff --git a/okhttp/src/main/java/okhttp3/internal/ws/WebSocketProtocol.kt b/okhttp/src/main/java/okhttp3/internal/ws/WebSocketProtocol.kt index 678a93159..171c44e44 100644 --- a/okhttp/src/main/java/okhttp3/internal/ws/WebSocketProtocol.kt +++ b/okhttp/src/main/java/okhttp3/internal/ws/WebSocketProtocol.kt @@ -131,9 +131,7 @@ object WebSocketProtocol { fun validateCloseCode(code: Int) { val message = closeCodeExceptionMessage(code) - if (message != null) { - throw IllegalArgumentException(message) - } + require(message == null) { message!! } } fun acceptHeader(key: String): String {