Skip to content

Commit

Permalink
Make UnsafeCursor implement Closable interface in multiplatform (Fixes
Browse files Browse the repository at this point in the history
…#1238) (#1239)

* Promote UnsafeCursor Closable interface to multiplatform (#1238)

* Make UnsafeCursor implement Closable interface in multiplatform (#1238)
  • Loading branch information
inklesspen1rus committed Jun 17, 2023
1 parent 297db32 commit e817480
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
4 changes: 2 additions & 2 deletions okio/src/commonMain/kotlin/okio/Buffer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ expect class Buffer() : BufferedSource, BufferedSink {
* You can reuse instances of this class if you like. Use the overloads of [Buffer.readUnsafe] and
* [Buffer.readAndWriteUnsafe] that take a cursor and close it after use.
*/
class UnsafeCursor constructor() {
class UnsafeCursor constructor() : Closeable {
@JvmField var buffer: Buffer?

@JvmField var readWrite: Boolean
Expand Down Expand Up @@ -411,6 +411,6 @@ expect class Buffer() : BufferedSource, BufferedSink {
*/
fun expandBuffer(minByteCount: Int): Long

fun close()
override fun close()
}
}
5 changes: 5 additions & 0 deletions okio/src/commonTest/kotlin/okio/UnsafeCursorTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package okio

import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class UnsafeCursorTest {
@Test fun acquireForRead() {
Expand Down Expand Up @@ -84,4 +85,8 @@ class UnsafeCursorTest {

assertEquals("z".repeat(100), buffer.readUtf8())
}

@Test fun testUnsafeCursorIsClosable() {
assertTrue(Closeable::class.isInstance(Buffer.UnsafeCursor()))
}
}
4 changes: 2 additions & 2 deletions okio/src/nonJvmMain/kotlin/okio/Buffer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ actual class Buffer : BufferedSource, BufferedSink {
actual fun readAndWriteUnsafe(unsafeCursor: UnsafeCursor): UnsafeCursor =
commonReadAndWriteUnsafe(unsafeCursor)

actual class UnsafeCursor {
actual class UnsafeCursor : Closeable {
actual var buffer: Buffer? = null
actual var readWrite: Boolean = false

Expand All @@ -315,7 +315,7 @@ actual class Buffer : BufferedSource, BufferedSink {

actual fun expandBuffer(minByteCount: Int): Long = commonExpandBuffer(minByteCount)

actual fun close() {
actual override fun close() {
commonClose()
}
}
Expand Down

0 comments on commit e817480

Please sign in to comment.