Repository: square/okhttp
File: okhttp/src/main/kotlin/okhttp3/Cache.kt (Kotlin)

Code before:
     * base64-encoded peerCertificate[0]
     * base64-encoded peerCertificate[1]
     * -1
     * TLSv1.2
     * ```
     *
     * The file is newline separated. The first two lines are the URL and the request method. Next
     * is the number of HTTP Vary request header lines, followed by those lines.
     *
     * Next is the response status line, followed by the number of HTTP response header lines,
     * followed by those lines.
     *
     * HTTPS responses also contain SSL session information. This begins with a blank line, and then
     * a line containing the cipher suite. Next is the length of the peer certificate chain. These
     * certificates are base64-encoded and appear each on their own line. The next line contains the
     * length of the local certificate chain. These certificates are also base64-encoded and appear
     * each on their own line. A length of -1 is used to encode a null array. The last line is
     * optional. If present, it contains the TLS version.
     */
    @Throws(IOException::class) constructor(rawSource: Source) {
      rawSource.use {
        val source = rawSource.buffer()
        val urlLine = source.readUtf8LineStrict()
        // Choice here is between failing with a correct RuntimeException
        // or mostly silently with an IOException
        url = urlLine.toHttpUrlOrNull() ?: throw IOException("Cache Failure for $urlLine").also {
          Platform.get().log("cache corruption", WARN, it)
        }
        requestMethod = source.readUtf8LineStrict()
        val varyHeadersBuilder = Headers.Builder()
        val varyRequestHeaderLineCount = readInt(source)
        for (i in 0 until varyRequestHeaderLineCount) {
          varyHeadersBuilder.addLenient(source.readUtf8LineStrict())
        }
        varyHeaders = varyHeadersBuilder.build()

        val statusLine = StatusLine.parse(source.readUtf8LineStrict())
        protocol = statusLine.protocol
        code = statusLine.code
        message = statusLine.message
        val responseHeadersBuilder = Headers.Builder()
        val responseHeaderLineCount = readInt(source)
        for (i in 0 until responseHeaderLineCount) {
          responseHeadersBuilder.addLenient(source.readUtf8LineStrict())
        }
        val sendRequestMillisString = responseHeadersBuilder[SENT_MILLIS]
        val receivedResponseMillisString = responseHeadersBuilder[RECEIVED_MILLIS]
        responseHeadersBuilder.removeAll(SENT_MILLIS)
        responseHeadersBuilder.removeAll(RECEIVED_MILLIS)
        sentRequestMillis = sendRequestMillisString?.toLong() ?: 0L
        receivedResponseMillis = receivedResponseMillisString?.toLong() ?: 0L

Review comment (suggestion):
3 options here

1) runtime exception - fatal...
2) io exception only
3) io exception and log a warning

Code after:
     * base64-encoded peerCertificate[0]
     * base64-encoded peerCertificate[1]
     * -1
     * TLSv1.2
     * ```
     *
     * The file is newline separated. The first two lines are the URL and the request method. Next
     * is the number of HTTP Vary request header lines, followed by those lines.
     *
     * Next is the response status line, followed by the number of HTTP response header lines,
     * followed by those lines.
     *
     * HTTPS responses also contain SSL session information. This begins with a blank line, and then
     * a line containing the cipher suite. Next is the length of the peer certificate chain. These
     * certificates are base64-encoded and appear each on their own line. The next line contains the
     * length of the local certificate chain. These certificates are also base64-encoded and appear
     * each on their own line. A length of -1 is used to encode a null array. The last line is
     * optional. If present, it contains the TLS version.
     */
    @Throws(IOException::class) constructor(rawSource: Source) {
      rawSource.use {
        val source = rawSource.buffer()
        val urlLine = source.readUtf8LineStrict()
        // Choice here is between failing with a correct RuntimeException
        // or mostly silently with an IOException
        url = urlLine.toHttpUrlOrNull() ?: throw IOException("Cache corruption for $urlLine").also {
          Platform.get().log("cache corruption", WARN, it)
        }
        requestMethod = source.readUtf8LineStrict()
        val varyHeadersBuilder = Headers.Builder()
        val varyRequestHeaderLineCount = readInt(source)
        for (i in 0 until varyRequestHeaderLineCount) {
          varyHeadersBuilder.addLenient(source.readUtf8LineStrict())
        }
        varyHeaders = varyHeadersBuilder.build()

        val statusLine = StatusLine.parse(source.readUtf8LineStrict())
        protocol = statusLine.protocol
        code = statusLine.code
        message = statusLine.message
        val responseHeadersBuilder = Headers.Builder()
        val responseHeaderLineCount = readInt(source)
        for (i in 0 until responseHeaderLineCount) {
          responseHeadersBuilder.addLenient(source.readUtf8LineStrict())
        }
        val sendRequestMillisString = responseHeadersBuilder[SENT_MILLIS]
        val receivedResponseMillisString = responseHeadersBuilder[RECEIVED_MILLIS]
        responseHeadersBuilder.removeAll(SENT_MILLIS)
        responseHeadersBuilder.removeAll(RECEIVED_MILLIS)
        sentRequestMillis = sendRequestMillisString?.toLong() ?: 0L
        receivedResponseMillis = receivedResponseMillisString?.toLong() ?: 0L