@@ -233,11 +233,9 @@ class Cache internal constructor(
233233 val snapshot = (cached.body as CacheResponseBody ).snapshot
234234 var editor: DiskLruCache .Editor ? = null
235235 try {
236- editor = snapshot.edit() // Returns null if snapshot is not current.
237- if (editor != null ) {
238- entry.writeTo(editor)
239- editor.commit()
240- }
236+ editor = snapshot.edit() ? : return // edit() returns null if snapshot is not current.
237+ entry.writeTo(editor)
238+ editor.commit()
241239 } catch (_: IOException ) {
242240 abortQuietly(editor)
243241 }
@@ -299,7 +297,7 @@ class Cache internal constructor(
299297 return object : MutableIterator <String > {
300298 val delegate: MutableIterator <DiskLruCache .Snapshot > = cache.snapshots()
301299 var nextUrl: String? = null
302- var canRemove: Boolean = false
300+ var canRemove = false
303301
304302 override fun hasNext (): Boolean {
305303 if (nextUrl != null ) return true
@@ -393,7 +391,7 @@ class Cache internal constructor(
393391 ) : CacheRequest {
394392 private val cacheOut: Sink = editor.newSink(ENTRY_BODY )
395393 private val body: Sink
396- internal var done: Boolean = false
394+ internal var done = false
397395
398396 init {
399397 this .body = object : ForwardingSink (cacheOut) {
@@ -559,42 +557,42 @@ class Cache internal constructor(
559557
560558 @Throws(IOException ::class )
561559 fun writeTo (editor : DiskLruCache .Editor ) {
562- val sink = editor.newSink(ENTRY_METADATA ).buffer()
563- sink.writeUtf8(url).writeByte(' \n ' .toInt())
564- sink.writeUtf8(requestMethod).writeByte(' \n ' .toInt())
565- sink.writeDecimalLong(varyHeaders.size.toLong()).writeByte(' \n ' .toInt())
566- for (i in 0 until varyHeaders.size) {
567- sink.writeUtf8(varyHeaders.name(i))
560+ editor.newSink(ENTRY_METADATA ).buffer().use { sink ->
561+ sink.writeUtf8(url).writeByte(' \n ' .toInt())
562+ sink.writeUtf8(requestMethod).writeByte(' \n ' .toInt())
563+ sink.writeDecimalLong(varyHeaders.size.toLong()).writeByte(' \n ' .toInt())
564+ for (i in 0 until varyHeaders.size) {
565+ sink.writeUtf8(varyHeaders.name(i))
566+ .writeUtf8(" : " )
567+ .writeUtf8(varyHeaders.value(i))
568+ .writeByte(' \n ' .toInt())
569+ }
570+
571+ sink.writeUtf8(StatusLine (protocol, code, message).toString()).writeByte(' \n ' .toInt())
572+ sink.writeDecimalLong((responseHeaders.size + 2 ).toLong()).writeByte(' \n ' .toInt())
573+ for (i in 0 until responseHeaders.size) {
574+ sink.writeUtf8(responseHeaders.name(i))
575+ .writeUtf8(" : " )
576+ .writeUtf8(responseHeaders.value(i))
577+ .writeByte(' \n ' .toInt())
578+ }
579+ sink.writeUtf8(SENT_MILLIS )
568580 .writeUtf8(" : " )
569- .writeUtf8(varyHeaders.value(i) )
581+ .writeDecimalLong(sentRequestMillis )
570582 .writeByte(' \n ' .toInt())
571- }
572-
573- sink.writeUtf8(StatusLine (protocol, code, message).toString()).writeByte(' \n ' .toInt())
574- sink.writeDecimalLong((responseHeaders.size + 2 ).toLong()).writeByte(' \n ' .toInt())
575- for (i in 0 until responseHeaders.size) {
576- sink.writeUtf8(responseHeaders.name(i))
583+ sink.writeUtf8(RECEIVED_MILLIS )
577584 .writeUtf8(" : " )
578- .writeUtf8(responseHeaders.value(i) )
585+ .writeDecimalLong(receivedResponseMillis )
579586 .writeByte(' \n ' .toInt())
587+
588+ if (isHttps) {
589+ sink.writeByte(' \n ' .toInt())
590+ sink.writeUtf8(handshake!! .cipherSuite.javaName).writeByte(' \n ' .toInt())
591+ writeCertList(sink, handshake.peerCertificates)
592+ writeCertList(sink, handshake.localCertificates)
593+ sink.writeUtf8(handshake.tlsVersion.javaName).writeByte(' \n ' .toInt())
594+ }
580595 }
581- sink.writeUtf8(SENT_MILLIS )
582- .writeUtf8(" : " )
583- .writeDecimalLong(sentRequestMillis)
584- .writeByte(' \n ' .toInt())
585- sink.writeUtf8(RECEIVED_MILLIS )
586- .writeUtf8(" : " )
587- .writeDecimalLong(receivedResponseMillis)
588- .writeByte(' \n ' .toInt())
589-
590- if (isHttps) {
591- sink.writeByte(' \n ' .toInt())
592- sink.writeUtf8(handshake!! .cipherSuite.javaName).writeByte(' \n ' .toInt())
593- writeCertList(sink, handshake.peerCertificates)
594- writeCertList(sink, handshake.localCertificates)
595- sink.writeUtf8(handshake.tlsVersion.javaName).writeByte(' \n ' .toInt())
596- }
597- sink.close()
598596 }
599597
600598 @Throws(IOException ::class )
@@ -660,10 +658,10 @@ class Cache internal constructor(
660658
661659 companion object {
662660 /* * Synthetic response header: the local time when the request was sent. */
663- private val SENT_MILLIS = Platform .get().getPrefix() + " -Sent-Millis"
661+ private val SENT_MILLIS = " ${ Platform .get().getPrefix()} -Sent-Millis"
664662
665663 /* * Synthetic response header: the local time when the response was received. */
666- private val RECEIVED_MILLIS = Platform .get().getPrefix() + " -Received-Millis"
664+ private val RECEIVED_MILLIS = " ${ Platform .get().getPrefix()} -Received-Millis"
667665 }
668666 }
669667
0 commit comments